In this section, we will delve into the arithmetic instructions in Assembly language. Arithmetic instructions are fundamental as they allow you to perform basic mathematical operations such as addition, subtraction, multiplication, and division. Understanding these instructions is crucial for manipulating data and performing calculations in your programs.
Key Concepts
- Addition and Subtraction
- Multiplication and Division
- Increment and Decrement
- Handling Overflow and Underflow
- Addition and Subtraction
Addition
The ADD instruction is used to add two values. The result is stored in the destination operand.
Syntax:
Example:
section .data
num1 db 10
num2 db 20
result db 0
section .text
global _start
_start:
mov al, [num1] ; Load num1 into AL
add al, [num2] ; Add num2 to AL
mov [result], al ; Store the result in 'result'
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscallSubtraction
The SUB instruction is used to subtract one value from another. The result is stored in the destination operand.
Syntax:
Example:
section .data
num1 db 30
num2 db 10
result db 0
section .text
global _start
_start:
mov al, [num1] ; Load num1 into AL
sub al, [num2] ; Subtract num2 from AL
mov [result], al ; Store the result in 'result'
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscall
- Multiplication and Division
Multiplication
The MUL instruction is used for unsigned multiplication. It multiplies the accumulator register (AL, AX, or EAX) by the source operand.
Syntax:
Example:
section .data
num1 db 5
num2 db 4
result dw 0
section .text
global _start
_start:
mov al, [num1] ; Load num1 into AL
mul byte [num2] ; Multiply AL by num2
mov [result], ax ; Store the result in 'result'
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscallDivision
The DIV instruction is used for unsigned division. It divides the accumulator register by the source operand.
Syntax:
Example:
section .data
num1 db 20
num2 db 4
quotient db 0
remainder db 0
section .text
global _start
_start:
mov al, [num1] ; Load num1 into AL
xor ah, ah ; Clear AH for division
div byte [num2] ; Divide AX by num2
mov [quotient], al ; Store the quotient
mov [remainder], ah ; Store the remainder
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscall
- Increment and Decrement
Increment
The INC instruction increases the value of the operand by 1.
Syntax:
Example:
section .data
num db 10
section .text
global _start
_start:
inc byte [num] ; Increment the value of num by 1
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscallDecrement
The DEC instruction decreases the value of the operand by 1.
Syntax:
Example:
section .data
num db 10
section .text
global _start
_start:
dec byte [num] ; Decrement the value of num by 1
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscall
- Handling Overflow and Underflow
When performing arithmetic operations, it's important to handle overflow and underflow conditions. The CPU sets specific flags in the status register to indicate these conditions.
- Overflow Flag (OF): Set when an arithmetic operation produces a result too large to fit in the destination operand.
- Carry Flag (CF): Set when an unsigned arithmetic operation generates a carry out of the most significant bit.
- Zero Flag (ZF): Set when the result of an arithmetic operation is zero.
Example:
section .data
num1 db 255
num2 db 1
result db 0
section .text
global _start
_start:
mov al, [num1] ; Load num1 into AL
add al, [num2] ; Add num2 to AL
jo overflow ; Jump if overflow occurs
mov [result], al ; Store the result in 'result'
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscall
overflow:
; Handle overflow
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscallPractical Exercises
Exercise 1: Simple Addition
Write an Assembly program to add two numbers and store the result.
Solution:
section .data
num1 db 15
num2 db 25
result db 0
section .text
global _start
_start:
mov al, [num1] ; Load num1 into AL
add al, [num2] ; Add num2 to AL
mov [result], al ; Store the result in 'result'
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscallExercise 2: Multiplication with Overflow Check
Write an Assembly program to multiply two numbers and check for overflow.
Solution:
section .data
num1 db 100
num2 db 3
result dw 0
section .text
global _start
_start:
mov al, [num1] ; Load num1 into AL
mul byte [num2] ; Multiply AL by num2
jo overflow ; Jump if overflow occurs
mov [result], ax ; Store the result in 'result'
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscall
overflow:
; Handle overflow
; Exit the program
mov eax, 60 ; syscall: exit
xor edi, edi ; status: 0
syscallConclusion
In this section, we covered the basic arithmetic instructions in Assembly language, including addition, subtraction, multiplication, and division. We also discussed increment and decrement operations and how to handle overflow and underflow conditions. These fundamental operations are essential for performing calculations and manipulating data in your Assembly programs. In the next section, we will explore logical instructions, which are crucial for making decisions and controlling the flow of your programs.
Assembly Programming Course
Module 1: Introduction to Assembly Language
- What is Assembly Language?
- History and Evolution of Assembly
- Basic Concepts and Terminology
- Setting Up the Development Environment
Module 2: Assembly Language Basics
- Understanding the CPU and Memory
- Registers and Their Functions
- Basic Syntax and Structure
- Writing Your First Assembly Program
Module 3: Data Representation and Instructions
Module 4: Control Flow
Module 5: Advanced Assembly Concepts
- Interrupts and System Calls
- Macros and Conditional Assembly
- Inline Assembly in High-Level Languages
- Optimizing Assembly Code
Module 6: Assembly for Different Architectures
Module 7: Practical Applications and Projects
- Writing a Simple Bootloader
- Creating a Basic Operating System Kernel
- Interfacing with Hardware
- Debugging and Profiling Assembly Code
