Arithmetic Operations in 8086 Assembly Language
So, to learn any programming language. It’s necessary for us to know, about how to do basic operations on numbers.
Such as Addition, Subtraction, Division and Multiplication. These are the 4 things that come to our mind, when we think of numbers and, operations on them.
So, today I am going to show you the code, on how to do Arithmetic operations on two given numbers using Assembly Language and explain it.
Code →
.model small.data n1 dw 0006h n2 dw 0003h addn dw ? subt dw ? mult dw ? divn dw ?.code mov ax,@data mov ds,ax mov ax,n1 add ax,n2 mov addn,ax mov ax, n1 sub ax,n2 mov subt,ax mov ax,n1 mul n2 mov mult,ax mov ax,n1 div n2 mov divn,axmov ah,4chint 21hend
Explanation →
.data n1 dw 0006h n2 dw 0003h addn dw ? subt dw ? mult dw ? divn dw ?
Here, n1 and n2 are two variables, which contain two 16-bit numbers, i.e. 0006h & 0003h respectively. And, addn, subt, mult, divn
are variables which will contain the results of addition, subtraction, multiplication and division respectively.
mov ax,n1add ax,n2mov addn,ax
In the above code snippet, n1
is stored in the 16-bit accumulator register ax
. And, the contents of n2
are added to ax
. Finally, the result present in ax
is moved to the variable addn
.
mov ax, n1sub ax,n2mov subt,ax
Similar to the previous explanation, here also the same steps follow. But at last, the result is stored in the variable subt
.
mov ax,n1mul n2mov mult,ax
Multiplication operation is slightly different from Addition & Subtraction. Here, first n1
is moved to ax
. And, then we do mul n2
: In this operation multiplication of a given operand is always done with the accumulator register. It actually means this mul accumulator, operand(or variable)
. But, the accumulator register ax
is told implicitly, it’s not given specifically for mul
operation. And, the multiplication result is also stored in ax
, which is again stored back in the mult
variable.
mov ax,n1div n2mov divn,ax
The division code snippet is given as above. It also follows the same rule as multiplication. i.e. division happens implicitly with the ax
register. And, then the result is being moved from ax
to divn
variable.
And, that’s the end of my explanation for this code. So if, you liked the code and it’s explanation, feel free to give me a clap 👏. And if you still have more doubts, drop them in the comments section, and I will do my best to answer them.