Addition of two 8-bit numbers

data segment
a db 09h
b db 0fh 
c dw ?
data ends


code segment
assume cs:code, ds:data
start:

mov ax, data
mov ds, ax

mov al, a
mov bl, b
add al, bl
mov c, ax

mov ah,4ch
int 21h

code ends 
end start 

Understand the code

Data Segment:

data segment: This section defines the variables.

a db 09h: Declares an 8-bit byte variable a initialized to 09h (hexadecimal).

b db 02h: Declares an 8-bit byte variable b initialized to 02h.

c dw ?: Declares a 16-bit word variable c to store the sum. We use a 16-bit variable to handle potential overflow if the sum exceeds 255 (FFh).

Code Segment:

code segment: This section contains the program instructions. ; assume cs:code,ds:data: Tells the assembler that the cs register points to the code segment and the ds register points to the data segment.

start: The label marking the program’s entry point.

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. This is crucial for accessing the variables a and b.

mov al,a: Loads the value of a (09h) into the al register (the lower 8 bits of ax).

mov bl,b: Loads the value of b (02h) into the bl register (the lower 8 bits of bx).

add al,bl: Adds the contents of bl to al. The result (0Bh) is stored in al.

mov c,ax: Moves the contents of ax (which now contains the sum in its lower byte, al) into the variable c.

int 3: The breakpoint instruction halts execution for debugging purposes.

Last updated