Assembly Language is a low-level programming language. The instructions within Assembly Language are similar to machine code instructions.
Arithmetic instructions in Assembly Language are executed in the Arithmetic Logical Unit (ALU) of the computer’s processor.
For this shot, we will be discussing arithmetic instructions in the Assembly Language of the x86 machine architecture.
Assembly Language allows us to perform basic unsigned integer arithmetic operations. These operations are:
add
)sub
)mul
)div
)The syntax for writing addition or subtraction instructions is as follows:
operation destination, source
operation: the intended arithmetic operation, i.e., add
or sub
.
destination: the accumulator register or memory location where we will store our final result after adding or subtracting the value in source register or memory location.
source: the register or memory location that contains the value to be added or subtracted to/from the original content of the destination register.
The destination and source can be a combination of the following:
mov ax, 10 # move 10 to register 'ax'
mov bx, 5 # move 5 to register 'bx'
add ax, bx
mov ax, 10 # move 10 to register 'ax'
add ax, [num1] # access value stored at location num1 and store sum
# again in the register 'ax'
mov ax, 10
add [num1], ax
mov ax, 10
add ax, 5 # add 5 to the register 'ax' value and store the sum in 'ax'
add [num1], 5 # add 5 to the value stored at num1 and replace the value
# there with the sum
We can also apply the above source/destination combinations to multiplication and division where applicable.
The inc
and dec
instructions can be used to inc
rement or dec
rement the contents of their respective operands by one.
The inc
instruction has no effect on the carry flag.
The dec
instruction sets the zero flag if the result of the operation is 0
.
The syntax for writing multiplication or division instructions is as follows:
operation source
mul
performs an unsigned multiplication of the source operand and the accumulator.
If the source operand is a byte (8 bits), we multiply it by the value stored in the register AL
. The result is returned in the registers AH
and AL
. The higher half of the 16-bit result is stored in AH
and the lower half in AL
. This means that if the result is small enough to be represented in 8 bits, AH
would contain 0
.
If the source operand is a word (16 bits), then we multiply it by the value stored in the register AX
and the result is returned in the registers DX
and AX
. The higher half of the 32-bit result is stored in DX
and the lower half in AX
.
div
performs an integer division of the accumulator and the source operand. The result consists of an integer quotient and remainder.
If the source operand is a byte, the 16-bit number stored in AX
is divided by the operand. The 8-bit quotient is stored in AL
and the 8-bit remainder in AH
.
If the source operand is a word, the 32-bit number stored in DX : AX
is divided by the operand. The higher half of the number is stored in DX
and the lower in AX
. The 16-bit quotient is stored in AX
and the remainder in DX
.
The following code shows how we can implement arithmetic instructions in assembly language:
# a program to show arithmetic instructions in AL # addition mov ax, 5 # load number in ax mov bx, 2 # load number in bx add ax, bx # accumulate sum in ax # subtraction mov cx, 10 mov dx, 3 sub cx, dx # accumulate difference in cx # refreshing registers mov ax, 0 mov bx, 0 mov cx, 0 mov dx, 0 # multiplication - 8 bit source mov al, 5 mov bl, 10 mul bl # result in ax # refreshing registers mov ax, 0 mov bx, 0 # multiplication - 16 bit source mov ax, 5 mov bx, 10 mul bx # result in dx:ax # refreshing registers mov ax, 0 mov bx, 0 # divison - 8 bit source mov al, 23 mov bl, 4 div bl # quotient in al, remainder in ah # refreshing registers mov ax, 0 mov bx, 0 # divison - 16 bit source mov ax, 23 mov bx, 4 div bx # quotient in ax, remainder in dx mov ax, 0x4c00 # terminate program int 0x21
RELATED TAGS
CONTRIBUTOR
View all Courses