10/29/24, 11:39 AM What are arithmetic instructions in Assembly Language?
What are arithmetic instructions in Assembly
Language?
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.
Types of arithmetic instructions
Assembly Language allows us to perform basic unsigned integer arithmetic operations. These
operations are:
1. Addition (add)
2. Subtraction (sub)
3. Multiplication (mul)
4. Division (div)
Addition and subtraction
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:
1. Register to Register
mov ax, 10 # move 10 to register 'ax'
https://www.educative.io/answers/what-are-arithmetic-instructions-in-assembly-language 1/4
10/29/24, 11:39 AM What are arithmetic instructions in Assembly Language?
mov bx, 5 # move 5 to register 'bx'
add ax, bx
2. Memory to Register
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'
3. Register to Memory
mov ax, 10
add [num1], ax
4. Constant to Register
mov ax, 10
add ax, 5 # add 5 to the register 'ax' value and store the sum in 'ax'
5. Constant to Memory
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.
Assembly language addition instruction mapped to a high level language
Increment and decrement
The inc and dec instructions can be used to increment or decrement 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.
Multiplication and division
https://www.educative.io/answers/what-are-arithmetic-instructions-in-assembly-language 2/4
10/29/24, 11:39 AM What are arithmetic instructions in Assembly Language?
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.
Variations of the mul instruction
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.
Variations of the div instruction
Code
The following code shows how we can implement arithmetic instructions in assembly language:
# a program to show arithmetic instructions in AL
Ace 1Editor
2
https://www.educative.io/answers/what-are-arithmetic-instructions-in-assembly-language 3/4
10/29/24, 11:39 AM What are arithmetic instructions in Assembly Language?
3 # addition
4 mov ax, 5 # load number in ax
5 mov bx, 2 # load number in bx
6 add ax, bx # accumulate sum in ax
7
8 # subtraction
9 mov cx, 10
10 mov dx, 3
11 sub cx, dx # accumulate difference in cx
12
13 # refreshing registers
14 mov ax, 0
15 mov bx, 0
16 mov cx, 0
17 mov dx, 0
18
19 # multiplication - 8 bit source
20 mov al, 5
21 mov bl, 10
22 mul bl # result in ax
23
24 # refreshing registers
25 mov ax, 0
26 b 0
https://www.educative.io/answers/what-are-arithmetic-instructions-in-assembly-language 4/4