In this blog post, we’ll delve into the world of assembly language programming using the 8086 microprocessors. We’ll explore a practical example: creating an assembly program to determine the largest number from a given set of values.
data segment STRING1 DB 08h,14h,05h,0Fh,09h res db ? data ends code segment assume cs:code, ds:data start: mov ax, data mov ds, ax mov cx, 04h mov bl, 00h LEA SI, STRING1 up: mov al, [SI] cmp al, bl jl nxt mov bl, al nxt: inc si dec cx jnz up mov res,bl int 3 code ends end start
Step-by-Step Explanation:
1. Data Segment:
- STRING1: Defines an array of bytes (8-bit values) to store the numbers: 08h, 14h, 05h, 0Fh, 09h.
- res: Reserves a single byte to store the largest number found.
2. Code Segment:
Initialization:
- mov ax, data: Loads the address of the data segment into the AX register.
- mov ds, ax: Sets the DS register to point to the data segment, allowing access to variables.
- mov cx, 04h: Initializes the loop counter CX to 4, as there are 4 numbers to compare.
Finding the Largest Number:
- mov bl, 00h: Initializes the BL register to 0, which will hold the largest number found so far.
- LEA SI, STRING1: Loads the address of the first element of the STRING1 array into the SI register.
Loop:
- mov al, [SI]: Loads the current number from the STRING1 array into the AL register.
- cmp al, bl: Compares the current number (AL) with the largest number found so far (BL).
- jl nxt: If AL is less than BL, jump to the next iteration.
- mov bl, al: If AL is greater than or equal to BL, update BL with the new largest number.
- inc si: Increment the SI register to point to the next number in the array.
- dec cx: Decrement the loop counter.
- jnz up: Jump back to the beginning of the loop if CX is not zero.
Storing the Result:
- mov res, bl: Stores the largest number (in BL) into the ‘res’ variable.
Program Termination:
- int 3: Generates an interrupt 3, which typically halts the program execution.
Overall Process:
- The program initializes the loop counter and a variable to store the largest number.
- It iterates through each number in the array.
- For each number, it compares it with the current largest number.
- If the current number is larger, it updates the largest number.
- After processing all numbers, the largest number is stored in the ‘res’ variable.
- The program terminates.
Output
C:\TASM>masm largest.asm Microsoft (R) Macro Assembler Version 5.00 Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved. Object filename [largest.OBJ]: Source listing [NUL.LST]: Cross-reference [NUL.CRF]: 50846 + 450850 Bytes symbol space free 0 Warning Errors 0 Severe Errors C:\TASM>link largest.obj Microsoft (R) Overlay Linker Version 3.60 Copyright (C) Microsoft Corp 1983-1987. All rights reserved. Run File [LARGEST.EXE]: List File [NUL.MAP]: Libraries [.LIB]: LINK : warning L4021: no stack segment C:\TASM>debug largest.exe -g AX=0B0F BX=0014 CX=0000 DX=0000 SP=0000 BP=0000 SI=0004 DI=0000 DS=0B37 ES=0B27 SS=0B37 CS=0B38 IP=001E NV UP EI PL ZR NA PE CY 0B38:001E CC INT 3 -d 0b37:0000 0B37:0000 08 14 05 0F 09 14 00 00-00 00 00 00 00 00 00 00 ................ 0B37:0010 B8 37 0B 8E D8 B9 04 00-B3 00 8D 36 00 00 8A 04 .7.........6.... 0B37:0020 3A C3 7C 02 8A D8 46 49-75 F4 88 1E 05 00 CC FC :.|...FIu....... 0B37:0030 FE C4 9E FA FE 26 8A 47-0C 2A E4 40 50 8B C3 05 .....&.G.*.@P... 0B37:0040 0C 00 52 50 E8 19 46 83-C4 04 50 8D 86 00 FF 50 ..RP..F...P....P 0B37:0050 E8 6F 70 83 C4 06 B8 CD-05 50 8D 86 00 FF 50 E8 .op......P....P. 0B37:0060 CA 0C 83 C4 04 B8 FF FF-50 8D 86 00 FF 50 8D 46 ........P....P.F 0B37:0070 80 50 E8 4D FA 83 C4 06-0A C0 75 03 E9 7B FF 5E .P.M......u..{.^ -q
Understanding the Memory Dump
The memory dump provided by the -d 0b37:0000
command displays the contents of memory starting from the DS
(data segment) base address, which is 0B37:0000
in this case.
Here is the relevant portion of the memory:
0B37:0000 08 14 05 0F 09 14 00 00-00 00 00 00 00 00 00 00
08 14 05 0F 09
: These are the values ofSTRING1
, matching the array initialized in the data segment.14
: This is the value of the variableres
, where the maximum value is stored.
I tried to write an 8086 assembly program to find the largest number. It worked, but the computer laughed at me in binary 🙂
Please provide the description of each instructions/mnemonics.
Well understood thanks for sharing!
more easy if description is available
data segment
STRING1 DB 08h,14h,05h,0Fh,09h
res db ?
data ends
code segment
assume cs:code, ds:data
start: mov ax, data
mov ds, ax
mov cx, 04h
mov bl, 00h
LEA SI, STRING1
up:
mov al, [SI]
cmp al, bl
jl nxt
mov bl, al
nxt:
inc si
dec cx
jnz up
mov res,bl
int 3
code ends
end start