Related Tags

assembly language

# What are arithmetic instructions in Assembly Language? Khizar Hayat Saani

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)

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'
mov bx, 5  # move 5 to register '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


### 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

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

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

assembly language

CONTRIBUTOR Khizar Hayat Saani 