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:
- Addition (
add) - Subtraction (
sub) - Multiplication (
mul) - 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.,
addorsub. -
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'
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.
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
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.
Code
The following code shows how we can implement arithmetic instructions in assembly language:
# a program to show arithmetic instructions in AL# additionmov ax, 5 # load number in axmov bx, 2 # load number in bxadd ax, bx # accumulate sum in ax# subtractionmov cx, 10mov dx, 3sub cx, dx # accumulate difference in cx# refreshing registersmov ax, 0mov bx, 0mov cx, 0mov dx, 0# multiplication - 8 bit sourcemov al, 5mov bl, 10mul bl # result in ax# refreshing registersmov ax, 0mov bx, 0# multiplication - 16 bit sourcemov ax, 5mov bx, 10mul bx # result in dx:ax# refreshing registersmov ax, 0mov bx, 0# divison - 8 bit sourcemov al, 23mov bl, 4div bl # quotient in al, remainder in ah# refreshing registersmov ax, 0mov bx, 0# divison - 16 bit sourcemov ax, 23mov bx, 4div bx # quotient in ax, remainder in dxmov ax, 0x4c00 # terminate programint 0x21
Free Resources