Assembly - Arithmetic Instructions
Assembly - Arithmetic Instructions
Syntax
INC destination
Example
Syntax
DEC destination
Example
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 1/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
segment .data
count dw 0
value db 15
segment .text
inc [count]
dec [value]
Syntax
Register to register
Memory to register
Register to memory
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 2/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
Example
The following example will ask two digits from the user, store the digits in the EAX
and EBX register, respectively, add the values, store the result in a memory location
'res' and finally display the result.
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
segment .data
segment .bss
num1 resb 2
num2 resb 2
res resb 1
section .text
global _start ;must be declared for using gcc
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 3/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
mov edx, 2
int 0x80
; moving the first number to eax register and second number to ebx
; and subtracting ascii '0' to convert it into a decimal number
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 4/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
mov edx, 1
int 0x80
exit:
When the above code is compiled and executed, it produces the following result −
Enter a digit:
3
Please enter a second digit:
4
The sum is:
7
mov ecx,sum
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 5/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
section .data
msg db "The sum is:", 0xA,0xD
len equ $ - msg
segment .bss
sum resb 1
When the above code is compiled and executed, it produces the following result −
Syntax
MUL/IMUL multiplier
Multiplicand in both cases will be in an accumulator, depending upon the size of the
multiplicand and the multiplier and the generated product is also stored in two
registers depending upon the size of the operands. Following section explains MUL
instructions with three different cases −
Sr.No. Scenarios
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 6/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
the product is stored in AH and the low-order 8 bits are stored in AL.
Example
MOV AL, 10
MOV DL, 25
MUL DL
...
MOV DL, 0FFH ; DL= -1
MOV AL, 0BEH ; AL = -66
IMUL DL
Example
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 7/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
mov al,'3'
sub al, '0'
mov [res], al
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx,res
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
section .data
msg db "The result is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
When the above code is compiled and executed, it produces the following result −
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 8/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
used to keep the product. However, in case of division, overflow may occur. The
processor generates an interrupt if overflow occurs.
The DIV (Divide) instruction is used for unsigned data and the IDIV (Integer Divide)
is used for signed data.
Syntax
DIV/IDIV divisor
The dividend is in an accumulator. Both the instructions can work with 8-bit, 16-bit
or 32-bit operands. The operation affects all six status flags. Following section
explains three cases of division with different operand size −
Sr.No. Scenarios
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 9/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
After division, the 32-bit quotient goes to the EAX register and the 32-bit
remainder goes to the EDX register.
Example
The following example divides 8 with 2. The dividend 8 is stored in the 16-bit AX
register and the divisor 2 is stored in the 8-bit BL register.
mov [res], ax
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov ecx,res
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 10/11
6/15/24, 3:50 PM Assembly - Arithmetic Instructions
section .data
msg db "The result is:", 0xA,0xD
len equ $- msg
segment .bss
res resb 1
When the above code is compiled and executed, it produces the following result −
https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm 11/11