SlideShare a Scribd company logo
1 of 7
Download to read offline
http://www.tutorialspoint.com/assembly_prog ramming /assembly_arithmetic_instructions.htm
Copyright Ā© tutorialspoint.com
ASSEMBLY - ARITHMETIC INSTRUCTIONS
The INC Instruction
The INC instructionis used for incrementing anoperand by one. It works ona single operand that canbe either
ina register or inmemory.
Syntax:
The INC instructionhas the following syntax:
INC destination
The operand destination could be an8-bit, 16-bit or 32-bit operand.
Example:
INC EBX ; Increments 32-bit register
INC DL ; Increments 8-bit register
INC [count] ; Increments the count variable
The DEC Instruction
The DEC instructionis used for decrementing anoperand by one. It works ona single operand that canbe either
ina register or inmemory.
Syntax:
The DEC instructionhas the following syntax:.
DEC destination
The operand destination could be an8-bit, 16-bit or 32-bit operand.
Example:
segment .data
count dw 0
value db 15
segment .text
inc [count]
dec [value]
mov ebx, count
inc word [ebx]
mov esi, value
dec byte [esi]
The ADD and SUB Instructions
The ADD and SUB instructions are used for performing simple addition/subtractionof binary data inbyte, word
and doubleword size, i.e., for adding or subtracting 8-bit, 16-bit or 32-bit operands, respectively.
Syntax:
The ADD and SUB instructions have the following syntax:
ADD/SUB destination, source
The ADD/SUB instructioncantake place between:
Register to register
Memory to register
Register to memory
Register to constant data
Memory to constant data
However, like other instructions, memory-to-memory operations are not possible using ADD/SUB instructions.
AnADD or SUB operationsets or clears the overflow and carry flags.
Example:
The following example asks two digits fromthe user, stores the digits inthe EAX and EBX register,
respectively, adds the values, stores the result ina 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
msg1 db "Enter a digit ", 0xA,0xD
len1 equ $- msg1
msg2 db "Please enter a second digit", 0xA,0xD
len2 equ $- msg2
msg3 db "The sum is: "
len3 equ $- msg3
segment .bss
num1 resb 2
num2 resb 2
res resb 1
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num1
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num2
mov edx, 2
int 0x80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
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'
; add eax and ebx
add eax, ebx
; add '0' to to convert the sum from decimal to ASCII
add eax, '0'
; storing the sum in memory location res
mov [res], eax
; print the sum
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, res
mov edx, 1
int 0x80
exit:
mov eax, SYS_EXIT
xor ebx, ebx
int 0x80
Whenthe 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
The program with hardcoded variables:
section .text
global _start ;must be declared for using gcc
_start: ;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
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
Whenthe above code is compiled and executed, it produces the following result:
The sum is:
7
The MUL/IMUL Instruction
There are two instructions for multiplying binary data. The MUL (Multiply) instructionhandles unsigned data and
the IMUL (Integer Multiply) handles signed data. Bothinstructions affect the Carry and Overflow flag.
Syntax:
The syntax for the MUL/IMUL instructions is as follows:
MUL/IMUL multiplier
Multiplicand inbothcases willbe inanaccumulator, depending uponthe size of the multiplicand and the multiplier
and the generated product is also stored intwo registers depending uponthe size of the operands. Following
sectionexplains MUL instructions withthree different cases:
SN Scenarios
1 When two bytes are multiplied
The multiplicand is inthe AL register, and the multiplier is a byte inthe memory or inanother register.
The product is inAX. High-order 8 bits of the product is stored inAH and the low-order 8 bits are
stored inAL
2 When two one-word values are multiplied
The multiplicand should be inthe AX register, and the multiplier is a word inmemory or another
register. For example, for aninstructionlike MUL DX, youmust store the multiplier inDX and the
multiplicand inAX.
The resultant product is a doubleword, whichwillneed two registers. The high-order (leftmost)
portiongets stored inDX and the lower-order (rightmost) portiongets stored inAX.
3 When two doubleword values are multiplied
Whentwo doubleword values are multiplied, the multiplicand should be inEAX and the multiplier is a
doubleword value stored inmemory or inanother register. The product generated is stored inthe
EDX:EAX registers, i.e., the highorder 32 bits gets stored inthe EDX register and the low order 32-
bits are stored inthe 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 with2, and displays the result:
section .text
global _start ;must be declared for using gcc
_start: ;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
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
Whenthe above code is compiled and executed, it produces the following result:
The result is:
6
The DIV/IDIV Instructions
The divisionoperationgenerates two elements - a quotient and a remainder. Incase of multiplication,
overflow does not occur because double-lengthregisters are used to keep the product. However, incase of
division, overflow may occur. The processor generates aninterrupt if overflow occurs.
The DIV (Divide) instructionis used or unsigned data and the IDIV (Integer Divide) is used or signed data.
Syntax:
The format for the DIV/IDIV instruction:
DIV/IDIV divisor
The dividend is inanaccumulator. Boththe instructions canwork with8-bit, 16-bit or 32-bit operands. The
operationaffects allsix status flags. Following sectionexplains three cases of divisionwithdifferent operand
size:
SN Scenarios
1 When the divisor is 1 byte
The dividend is assumed to be inthe AX register (16 bits). After division, the quotient goes to the AL
register and the remainder goes to the AH register.
2 When the divisor is 1 word
The dividend is assumed to be 32 bits long and inthe DX:AX registers. The high-order 16 bits are in
DX and the low-order 16 bits are inAX. After division, the 16-bit quotient goes to the AX register and
the 16-bit remainder goes to the DX register.
3 When the divisor is doubleword
The dividend is assumed to be 64 bits long and inthe EDX:EAX registers. The high-order 32 bits are
inEDX and the low-order 32 bits are inEAX. 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 with2. The dividend 8 is stored inthe 16-bit AX register and the divisor
2 is stored inthe 8-bit BL register.
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
mov ax,'8'
sub ax, '0'
mov bl, '2'
sub bl, '0'
div bl
add ax, '0'
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
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
Whenthe above code is compiled and executed, it produces the following result:
The result is:
4

More Related Content

What's hot

Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
9840596838
Ā 
8086 assembly language
8086 assembly language8086 assembly language
8086 assembly language
Mir Majid
Ā 
Assembly Language -I
Assembly Language -IAssembly Language -I
Assembly Language -I
Devika Rangnekar
Ā 

What's hot (20)

Lecture5(1)
Lecture5(1)Lecture5(1)
Lecture5(1)
Ā 
Multiplication & division instructions microprocessor 8086
Multiplication & division instructions microprocessor 8086Multiplication & division instructions microprocessor 8086
Multiplication & division instructions microprocessor 8086
Ā 
Unit 3 ā€“ assembly language programming
Unit 3 ā€“ assembly language programmingUnit 3 ā€“ assembly language programming
Unit 3 ā€“ assembly language programming
Ā 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Ā 
Assembly 8086
Assembly 8086Assembly 8086
Assembly 8086
Ā 
Unit 4 assembly language programming
Unit 4   assembly language programmingUnit 4   assembly language programming
Unit 4 assembly language programming
Ā 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
Ā 
Chapter 6 Flow control Instructions
Chapter 6 Flow control InstructionsChapter 6 Flow control Instructions
Chapter 6 Flow control Instructions
Ā 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4
Ā 
Instruction formats-in-8086
Instruction formats-in-8086Instruction formats-in-8086
Instruction formats-in-8086
Ā 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Ā 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
Ā 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
Ā 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Ā 
Instruction set
Instruction setInstruction set
Instruction set
Ā 
Unit 2 assembly language programming
Unit 2   assembly language programmingUnit 2   assembly language programming
Unit 2 assembly language programming
Ā 
[ASM] Lab1
[ASM] Lab1[ASM] Lab1
[ASM] Lab1
Ā 
8086 assembly language
8086 assembly language8086 assembly language
8086 assembly language
Ā 
assembly language programming organization of IBM PC chapter 9 part-1(MULTIPL...
assembly language programming organization of IBM PC chapter 9 part-1(MULTIPL...assembly language programming organization of IBM PC chapter 9 part-1(MULTIPL...
assembly language programming organization of IBM PC chapter 9 part-1(MULTIPL...
Ā 
Assembly Language -I
Assembly Language -IAssembly Language -I
Assembly Language -I
Ā 

Viewers also liked

Arithmetic & logical operations in 8051
Arithmetic & logical operations in 8051Arithmetic & logical operations in 8051
Arithmetic & logical operations in 8051
Jay Patel
Ā 
8051 instruction set
8051 instruction set8051 instruction set
8051 instruction set
Andri Prastiyo
Ā 
8086-instruction-set-ppt
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-ppt
jemimajerome
Ā 

Viewers also liked (9)

Arithmetic instructions
Arithmetic instructionsArithmetic instructions
Arithmetic instructions
Ā 
Arithmetic and logical instructions set
Arithmetic and logical instructions setArithmetic and logical instructions set
Arithmetic and logical instructions set
Ā 
Arithmetic instructions
Arithmetic instructionsArithmetic instructions
Arithmetic instructions
Ā 
1.arithmetic & logical operations
1.arithmetic & logical operations1.arithmetic & logical operations
1.arithmetic & logical operations
Ā 
Arithmetic & logical operations in 8051
Arithmetic & logical operations in 8051Arithmetic & logical operations in 8051
Arithmetic & logical operations in 8051
Ā 
8051 instruction set
8051 instruction set8051 instruction set
8051 instruction set
Ā 
Instruction set of 8085
Instruction set  of 8085Instruction set  of 8085
Instruction set of 8085
Ā 
Arithmetic Logic Unit (ALU)
Arithmetic Logic Unit (ALU)Arithmetic Logic Unit (ALU)
Arithmetic Logic Unit (ALU)
Ā 
8086-instruction-set-ppt
 8086-instruction-set-ppt 8086-instruction-set-ppt
8086-instruction-set-ppt
Ā 

Similar to N_Asm Assembly arithmetic instructions (sol)

Assem -lect-6
Assem -lect-6Assem -lect-6
Assem -lect-6
Dolly Angel
Ā 
Systemsoftwarenotes 100929171256-phpapp02 2
Systemsoftwarenotes 100929171256-phpapp02 2Systemsoftwarenotes 100929171256-phpapp02 2
Systemsoftwarenotes 100929171256-phpapp02 2
Khaja Dileef
Ā 
X86 operation types
X86 operation typesX86 operation types
X86 operation types
Rowena Cornejo
Ā 

Similar to N_Asm Assembly arithmetic instructions (sol) (20)

Assem -lect-6
Assem -lect-6Assem -lect-6
Assem -lect-6
Ā 
It322 intro 3
It322 intro 3It322 intro 3
It322 intro 3
Ā 
X86 assembly nasm syntax
X86 assembly nasm syntaxX86 assembly nasm syntax
X86 assembly nasm syntax
Ā 
32 bit and 64 bit Register manipulation
32 bit and 64 bit Register manipulation32 bit and 64 bit Register manipulation
32 bit and 64 bit Register manipulation
Ā 
Systemsoftwarenotes 100929171256-phpapp02 2
Systemsoftwarenotes 100929171256-phpapp02 2Systemsoftwarenotes 100929171256-phpapp02 2
Systemsoftwarenotes 100929171256-phpapp02 2
Ā 
N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)
Ā 
Introduction of 8086 micro processor .
Introduction of 8086 micro processor .Introduction of 8086 micro processor .
Introduction of 8086 micro processor .
Ā 
Chapter1c
Chapter1cChapter1c
Chapter1c
Ā 
Exp 03
Exp 03Exp 03
Exp 03
Ā 
Intrl 8086 instruction set
Intrl 8086 instruction setIntrl 8086 instruction set
Intrl 8086 instruction set
Ā 
M6800
M6800M6800
M6800
Ā 
X86 assembly & GDB
X86 assembly & GDBX86 assembly & GDB
X86 assembly & GDB
Ā 
Introduction to debugging linux applications
Introduction to debugging linux applicationsIntroduction to debugging linux applications
Introduction to debugging linux applications
Ā 
other-architectures.ppt
other-architectures.pptother-architectures.ppt
other-architectures.ppt
Ā 
Mips
MipsMips
Mips
Ā 
The Stack and Buffer Overflows
The Stack and Buffer OverflowsThe Stack and Buffer Overflows
The Stack and Buffer Overflows
Ā 
X86 operation types
X86 operation typesX86 operation types
X86 operation types
Ā 
An Example MIPS
An Example  MIPSAn Example  MIPS
An Example MIPS
Ā 
Advance ROP Attacks
Advance ROP AttacksAdvance ROP Attacks
Advance ROP Attacks
Ā 
Shellcode Disassembling - Reverse Engineering
Shellcode Disassembling - Reverse EngineeringShellcode Disassembling - Reverse Engineering
Shellcode Disassembling - Reverse Engineering
Ā 

More from Selomon birhane (11)

N_Asm Assembly system calls (sol)
N_Asm Assembly system calls (sol)N_Asm Assembly system calls (sol)
N_Asm Assembly system calls (sol)
Ā 
N_Asm Assembly basic syntax (sol)
N_Asm Assembly basic syntax (sol)N_Asm Assembly basic syntax (sol)
N_Asm Assembly basic syntax (sol)
Ā 
N_Asm Assembly addressing modes (sol)
N_Asm Assembly addressing modes (sol)N_Asm Assembly addressing modes (sol)
N_Asm Assembly addressing modes (sol)
Ā 
N_Asm Assembly arrays (sol)
N_Asm Assembly arrays (sol)N_Asm Assembly arrays (sol)
N_Asm Assembly arrays (sol)
Ā 
N_Asm Assembly recursion (sol)
N_Asm Assembly recursion (sol)N_Asm Assembly recursion (sol)
N_Asm Assembly recursion (sol)
Ā 
N_Asm Assembly strings (sol)
N_Asm Assembly strings (sol)N_Asm Assembly strings (sol)
N_Asm Assembly strings (sol)
Ā 
N_Asm Assembly registers (sol)
N_Asm Assembly registers (sol)N_Asm Assembly registers (sol)
N_Asm Assembly registers (sol)
Ā 
N_Asm Assembly variables (sol)
N_Asm Assembly variables (sol)N_Asm Assembly variables (sol)
N_Asm Assembly variables (sol)
Ā 
N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)N_Asm Assembly macros (sol)
N_Asm Assembly macros (sol)
Ā 
Control system(smarajit ghosh) By sol
Control system(smarajit ghosh) By solControl system(smarajit ghosh) By sol
Control system(smarajit ghosh) By sol
Ā 
Two ports
Two ports Two ports
Two ports
Ā 

Recently uploaded

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
Ā 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
Ā 
CHEAP Call Girls in Pushp Vihar (-DELHI )šŸ” 9953056974šŸ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )šŸ” 9953056974šŸ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )šŸ” 9953056974šŸ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )šŸ” 9953056974šŸ”(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
Ā 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
Ā 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
Ā 

Recently uploaded (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
Ā 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
Ā 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
Ā 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
Ā 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
Ā 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Ā 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
Ā 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
Ā 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
Ā 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
Ā 
CHEAP Call Girls in Pushp Vihar (-DELHI )šŸ” 9953056974šŸ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )šŸ” 9953056974šŸ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )šŸ” 9953056974šŸ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )šŸ” 9953056974šŸ”(=)/CALL GIRLS SERVICE
Ā 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
Ā 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
Ā 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
Ā 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
Ā 
Shapes for Sharing between Graph Data SpacesĀ - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data SpacesĀ - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data SpacesĀ - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data SpacesĀ - and Epistemic Querying of RDF-...
Ā 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
Ā 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Ā 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
Ā 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
Ā 

N_Asm Assembly arithmetic instructions (sol)

  • 1. http://www.tutorialspoint.com/assembly_prog ramming /assembly_arithmetic_instructions.htm Copyright Ā© tutorialspoint.com ASSEMBLY - ARITHMETIC INSTRUCTIONS The INC Instruction The INC instructionis used for incrementing anoperand by one. It works ona single operand that canbe either ina register or inmemory. Syntax: The INC instructionhas the following syntax: INC destination The operand destination could be an8-bit, 16-bit or 32-bit operand. Example: INC EBX ; Increments 32-bit register INC DL ; Increments 8-bit register INC [count] ; Increments the count variable The DEC Instruction The DEC instructionis used for decrementing anoperand by one. It works ona single operand that canbe either ina register or inmemory. Syntax: The DEC instructionhas the following syntax:. DEC destination The operand destination could be an8-bit, 16-bit or 32-bit operand. Example: segment .data count dw 0 value db 15 segment .text inc [count] dec [value] mov ebx, count inc word [ebx] mov esi, value dec byte [esi] The ADD and SUB Instructions The ADD and SUB instructions are used for performing simple addition/subtractionof binary data inbyte, word and doubleword size, i.e., for adding or subtracting 8-bit, 16-bit or 32-bit operands, respectively. Syntax: The ADD and SUB instructions have the following syntax: ADD/SUB destination, source
  • 2. The ADD/SUB instructioncantake place between: Register to register Memory to register Register to memory Register to constant data Memory to constant data However, like other instructions, memory-to-memory operations are not possible using ADD/SUB instructions. AnADD or SUB operationsets or clears the overflow and carry flags. Example: The following example asks two digits fromthe user, stores the digits inthe EAX and EBX register, respectively, adds the values, stores the result ina 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 msg1 db "Enter a digit ", 0xA,0xD len1 equ $- msg1 msg2 db "Please enter a second digit", 0xA,0xD len2 equ $- msg2 msg3 db "The sum is: " len3 equ $- msg3 segment .bss num1 resb 2 num2 resb 2 res resb 1 section .text global _start ;must be declared for using gcc _start: ;tell linker entry point mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, msg1 mov edx, len1 int 0x80 mov eax, SYS_READ mov ebx, STDIN mov ecx, num1 mov edx, 2 int 0x80 mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, msg2 mov edx, len2 int 0x80 mov eax, SYS_READ mov ebx, STDIN mov ecx, num2 mov edx, 2 int 0x80
  • 3. mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, msg3 mov edx, len3 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' ; add eax and ebx add eax, ebx ; add '0' to to convert the sum from decimal to ASCII add eax, '0' ; storing the sum in memory location res mov [res], eax ; print the sum mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, res mov edx, 1 int 0x80 exit: mov eax, SYS_EXIT xor ebx, ebx int 0x80 Whenthe 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 The program with hardcoded variables: section .text global _start ;must be declared for using gcc _start: ;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 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
  • 4. Whenthe above code is compiled and executed, it produces the following result: The sum is: 7 The MUL/IMUL Instruction There are two instructions for multiplying binary data. The MUL (Multiply) instructionhandles unsigned data and the IMUL (Integer Multiply) handles signed data. Bothinstructions affect the Carry and Overflow flag. Syntax: The syntax for the MUL/IMUL instructions is as follows: MUL/IMUL multiplier Multiplicand inbothcases willbe inanaccumulator, depending uponthe size of the multiplicand and the multiplier and the generated product is also stored intwo registers depending uponthe size of the operands. Following sectionexplains MUL instructions withthree different cases: SN Scenarios 1 When two bytes are multiplied The multiplicand is inthe AL register, and the multiplier is a byte inthe memory or inanother register. The product is inAX. High-order 8 bits of the product is stored inAH and the low-order 8 bits are stored inAL 2 When two one-word values are multiplied The multiplicand should be inthe AX register, and the multiplier is a word inmemory or another register. For example, for aninstructionlike MUL DX, youmust store the multiplier inDX and the multiplicand inAX. The resultant product is a doubleword, whichwillneed two registers. The high-order (leftmost) portiongets stored inDX and the lower-order (rightmost) portiongets stored inAX. 3 When two doubleword values are multiplied Whentwo doubleword values are multiplied, the multiplicand should be inEAX and the multiplier is a doubleword value stored inmemory or inanother register. The product generated is stored inthe EDX:EAX registers, i.e., the highorder 32 bits gets stored inthe EDX register and the low order 32- bits are stored inthe EAX register.
  • 5. 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 with2, and displays the result: section .text global _start ;must be declared for using gcc _start: ;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 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 Whenthe above code is compiled and executed, it produces the following result: The result is: 6 The DIV/IDIV Instructions The divisionoperationgenerates two elements - a quotient and a remainder. Incase of multiplication, overflow does not occur because double-lengthregisters are used to keep the product. However, incase of division, overflow may occur. The processor generates aninterrupt if overflow occurs. The DIV (Divide) instructionis used or unsigned data and the IDIV (Integer Divide) is used or signed data. Syntax: The format for the DIV/IDIV instruction: DIV/IDIV divisor The dividend is inanaccumulator. Boththe instructions canwork with8-bit, 16-bit or 32-bit operands. The operationaffects allsix status flags. Following sectionexplains three cases of divisionwithdifferent operand
  • 6. size: SN Scenarios 1 When the divisor is 1 byte The dividend is assumed to be inthe AX register (16 bits). After division, the quotient goes to the AL register and the remainder goes to the AH register. 2 When the divisor is 1 word The dividend is assumed to be 32 bits long and inthe DX:AX registers. The high-order 16 bits are in DX and the low-order 16 bits are inAX. After division, the 16-bit quotient goes to the AX register and the 16-bit remainder goes to the DX register. 3 When the divisor is doubleword The dividend is assumed to be 64 bits long and inthe EDX:EAX registers. The high-order 32 bits are inEDX and the low-order 32 bits are inEAX. 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 with2. The dividend 8 is stored inthe 16-bit AX register and the divisor 2 is stored inthe 8-bit BL register. section .text global _start ;must be declared for using gcc _start: ;tell linker entry point mov ax,'8'
  • 7. sub ax, '0' mov bl, '2' sub bl, '0' div bl add ax, '0' 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 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 Whenthe above code is compiled and executed, it produces the following result: The result is: 4