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

  1. Addition and Subtraction
  2. Multiplication and Division
  3. Increment and Decrement
  4. Handling Overflow and Underflow

  1. Addition and Subtraction

Addition

The ADD instruction is used to add two values. The result is stored in the destination operand.

Syntax:

ADD destination, source

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
    syscall

Subtraction

The SUB instruction is used to subtract one value from another. The result is stored in the destination operand.

Syntax:

SUB destination, source

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

  1. 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:

MUL source

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
    syscall

Division

The DIV instruction is used for unsigned division. It divides the accumulator register by the source operand.

Syntax:

DIV source

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

  1. Increment and Decrement

Increment

The INC instruction increases the value of the operand by 1.

Syntax:

INC destination

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
    syscall

Decrement

The DEC instruction decreases the value of the operand by 1.

Syntax:

DEC destination

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

  1. 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
    syscall

Practical 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
    syscall

Exercise 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
    syscall

Conclusion

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.

© Copyright 2024. All rights reserved