Asm Lec#6
Asm Lec#6
10
Arithmetic Instructions
The INC Instruction
T he INC instruction is used for incrementing an operand by one. It works on a single operand that can be
SYNTAX:
The INC instruction has the following syntax:
INC destination
EXAMPLE:
INC EBX ; Increments 32-bit register
INC DL ; Increments 8-bit register
INC [count] ; Increments the count variable
SYNTAX:
The DEC instruction has the following syntax:
DEC destination
EXAMPLE:
segment .data
count dw 0
value db 15
segment .text
inc [count]
TUTORIALS POINT
Simply Easy Learning
dec [value]
mov ebx, count
inc word [ebx]
mov esi, value
dec byte [esi]
SYNTAX:
The ADD and SUB instructions have the following syntax:
Register to register
Memory to register
Register to memory
However, like other instructions, memory-to-memory operations are not possible using ADD/SUB instructions. An
ADD or SUB operation sets or clears the overflow and carry flags.
EXAMPLE:
The following example asks two digits from the user, stores the digits in the EAX and EBX register respectively,
adds the values, stores the result in a memory location 'res' and finally displays the result.
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
segment .data
segment .bss
TUTORIALS POINT
Simply Easy Learning
num1 resb 2
num2 resb 2
res resb 1
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
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
mov eax, [number1]
sub eax, '0'
mov ebx, [number2]
sub ebx, '0'
TUTORIALS POINT
Simply Easy Learning
int 0x80
When the above code is compiled and executed, it produces following result:
Enter a digit:
3
Please enter a second digit:
4
The sum is:
7
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov eax,'3'
sub eax, '0'
mov ebx, '4'
sub ebx, '0'
add eax, ebx
add eax, '0'
mov [sum], eax
mov ecx,msg
mov edx, len
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
nwln
mov ecx,sum
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
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 following result:
SYNTAX:
The syntax for the MUL/IMUL instructions is as follows:
MUL/IMUL multiplier
TUTORIALS POINT
Simply Easy Learning
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 MULL instructions with three different cases:
SN Scenarios
The multiplicand is in the AL register, and the multiplier is a byte in the memory or in another register. The
product is in AX. High order 8 bits of the product is stored in AH and the low order 8 bits are stored in AL
1
The multiplicand should be in the AX register, and the multiplier is a word in memory or another register. For
example, for an instruction like MUL DX, you must store the multiplier in DX and the multiplicand in AX.
The resultant product is a double word, which will need two registers. The High order (leftmost) portion gets
2 stored in DX and the lower-order (rightmost) portion gets stored in AX.
When two doubleword values are multiplied, the multiplicand should be in EAX and the multiplier is a
doubleword value stored in memory or in another register. The product generated is stored in the EDX:EAX
registers, i.e., the high order 32 bits gets stored in the EDX register and the low order 32-bits are stored in
3 the EAX register.
EXAMPLE:
MOV AL, 10
MOV DL, 25
MUL DL
...
MOV DL, 0FFH ; DL= -1
MOV AL, 0BEH ; AL = -66
IMUL DL
EXAMPLE:
The following example multiplies 3 with 2, and displays the result:
section .text
TUTORIALS POINT
Simply Easy Learning
global main ;must be declared for using gcc
main: ;tell linker entry point
mov al,'3'
sub al, '0'
mov bl, '2'
sub bl, '0'
mul bl
add 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
nwln
mov ecx,res
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
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 following result:
SYNTAX:
The format for the DIV/IDIV instruction:
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:
SN Scenarios
1 The dividend is assumed to be in the AX register (16 bits). After division, the quotient goes to the AL register
and the remainder goes to the AH register.
TUTORIALS POINT
Simply Easy Learning
When the divisor is 1 word
The dividend is assumed to be 32 bits long and in the DX:AX registers. The high order 16 bits are in DX and the
low order 16 bits are in AX. After division, the 16 bit quotient goes to the AX register and the 16 bit remainder
goes to the DX register.
The dividend is assumed to be 64 bits long and in the EDX:EAX registers. The high order 32 bits are in EDX
and the low order 32 bits are in EAX. 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 thedivisor 2 is
stored in the 8 bit BL register.
section .text
global main ;must be declared for using gcc
main: ;tell linker entry point
mov ax,'8'
sub ax, '0'
mov bl, '2'
sub bl, '0'
div bl
add ax, '0'
TUTORIALS POINT
Simply Easy Learning
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
nwln
mov ecx,res
mov edx, 1
mov ebx,1 ;file descriptor (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number (sys_exit)
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 following result:
TUTORIALS POINT
Simply Easy Learning