To understand x86 Assembly addition you must have the basic understanding of the following topics:
After understanding of above topics, it will be easy to learn basic addition in 8086 architecture. The below examples work Tasm assembler perfectly. If you use any other assembler change the assembler directives accordingly.
x86 Assembly Addition of 16-bit Numbers
Q: Write a program, take two 16-bit numbers of your choice, add them and display the results.
.Model small .stack 100h .data a dw 02h b dw 08h .code MAIN PROC mov ax,@data mov ds,ax mov ax,a mov bx,b add ax,bx add ax,30h mov ah,02 mov dx,ax int 21h main endp end main
Explanation
In the above addition program, we used AX, BX registers because they are 16-bit register and we are adding 16-bit numbers we have to use X registers. We use DW which stand for “data word” because data word is used for 16-bit variables. In data segment, we have initialized two variables a and b, in code segment we moved data from memory to register AX and BX and then we performed addition by using ADD opcode.
We have added 30h to the addition result in register AX because in 8086 architecture assembly data store in hexadecimal format, so to show it to the use in the decimal format we added 30h.
As we have 02h and 08h so when it is added it becomes 10h, to show it to use as 10 we added 30h so the total result became 40h, which is equal to decimal 10.
x86 Assembly Addition of 18-bit Numbers
Q: Write a program, take two 8-bit numbers of your choice, add them and display the results.
.Model small .stack 100h .data a db 02h b db 08h .code MAIN PROC mov ax,@data mov ds,ax mov al,a mov bl,b add al,bl add al,30h mov ah,02 mov dl,al int 21h main endp end main
Explanation
In this program, we use all 8-bit registers i.e AL, BL, Dl. In the data section, we used variable type DB means “data byte”. It is used for an 8-bit variable. All the other method is similar to addition process for 16-bit number as we discussed above.
Hope the tutorial will help you understand the basic addition program in assembly language. Comment to show me that you are alive!