Assembly Program: 4-Digit Decimal Number Addition

This assembly program takes two 4-digit decimal numbers as input, adds them, and displays the sum using ASCII characters. The program utilizes Linux syscalls for I/O operations and program termination.

Source code

; This assembly code performs the addition of two 4-digit decimal numbers.

section .text

; Function to convert ASCII characters to numerical digits
convert:
    sub bl, 30h          ; Convert ASCII to numerical value
    mov eax, 0           ; Clear EAX
    mov ecx, 0           ; Clear ECX
    mov al, bl           ; Move the numerical value to AL
    mov byte [count], 3  ; Set count to 3
acon:
    mov edx, 0           ; Clear EDX
    mov ecx, 10          ; Set ECX to 10
    mul cx               ; Multiply EAX by CX
    sub bh, 30h          ; Convert ASCII to numerical value
    shr ebx, 8           ; Shift right to get the high byte
    mov edx, 0           ; Clear EDX
    mov dl, bl           ; Move the numerical value to DL
    add ax, dx           ; Add the contents of AL and DX
    sub byte [count], 1  ; Decrement count
    cmp byte [count], 0  ; Compare count with 0
    jne acon             ; Jump if not equal (loop)

    ret                  ; Return

global _start
_start:
    ; Display "ENTER THE NUMBERS(4 DIGIT)" message
    mov ecx, msg
    mov edx, lmsg
    mov eax, 4
    mov ebx, 1
    int 0x80

    ; Display a newline character
    mov ecx, space
    mov edx, 1
    mov eax, 4
    mov ebx, 1
    int 80h

    ; Read input for the first number
    mov ecx, inp1
    mov edx, 4
    mov eax, 3
    mov ebx, 0
    int 0x80

    ; Read a single character from input
    mov eax, 3
    mov ebx, 0
    mov ecx, temp
    mov edx, 1
    int 80h

    ; Display " + " message
    mov ecx, msgplus
    mov edx, lpl
    mov eax, 4
    mov ebx, 1
    int 80h

    ; Read input for the second number
    mov ebx, 0
    mov ebx, [inp1]
    call convert
    mov word [inp1], ax

    ; Display a space character
    mov ecx, space
    mov edx, 1
    mov eax, 4
    mov ebx, 1
    int 80h

    ; Read input for the second number
    mov ecx, inp2
    mov edx, 4
    mov eax, 3
    mov ebx, 0
    int 0x80

    ; Display "______" message
    mov ecx, msgend
    mov edx, lend
    mov eax, 4
    mov ebx, 1
    int 80h

    ; Convert and store the second input number
    mov ebx, 0
    mov ebx, [inp2]
    call convert
    mov word [inp2], ax

    ; Add the two converted numbers
    mov eax, 0
    mov edx, 0
    mov ax, [inp1]
    mov dx, [inp2]
    add ax, dx

    ; Push the hexadecimal value 29h onto the stack
    push 29h
    mov byte [count], 5

    ; Loop to break down the sum into individual digits
break:
    mov ebx, 10           ; Divide by 10
    mov edx, 0            ; Clear EDX
    div bx                ; Divide EAX by BX
    add dl, 30h           ; Convert remainder to ASCII
    push dx               ; Push ASCII digit onto the stack
    sub byte [count], 1   ; Decrement count
    cmp byte [count], 0   ; Compare count with 0
    jne break             ; Jump if not equal (loop)

    ; Pop digits from the stack and display them
    mov eax, 0
print:
    mov byte [temp], al  ; Store the ASCII digit
    mov ecx, temp
    mov edx, 1
    mov eax, 4
    mov ebx, 1
    int 80h

    ; Pop a digit from the stack
    mov eax, 0
    pop ax
    cmp al, 29h           ; Check if the end marker (ASCII 29h) is reached
    jne print             ; Jump if not equal

    ; Display a newline character
    mov eax, 4
    mov ebx, 1
    mov ecx, newline
    mov edx, 1
    int 80h

    ; Exit the program
    mov eax, 1
    mov ebx, 0
    int 80h

section .data
msg db "ENTER THE NUMBERS(4 DIGIT)", 10
lmsg equ $-msg
newline db 0xa
space db " "
msgplus db "      +", 10
lpl equ $-msgplus
msgend db "______", 10
lend equ $-msgend
inp1 dw 30h
inp2 dw 30h
sum dw 30h
count db 30h
temp db 30h

Code Explanation

The provided assembly code performs the following steps:

  1. Conversion Function: The convert function is defined to convert ASCII characters to numerical digits.

  2. Program Initialization (_start):

    • Displays a message asking the user to input the numbers.

    • Reads the first number, converts it to its numerical form, and stores it.

    • Reads the '+' character.

    • Reads the second number, converts it to its numerical form, and stores it.

    • Computes the sum of the two numbers.

    • Breaks down the sum into individual digits and displays them.

  3. Data Section (.data):

    • Contains messages used in the program.

    • Defines variables to store input numbers, sum, count, and temporary data.

Example

Suppose the user inputs the first number "1234" and the second number "5678".

  1. Display:

    ENTER THE NUMBERS(4 DIGIT)
  2. Input:

    1234
  3. Display:

          +
  4. Input:

    5678
  5. Display:

    ______
  6. Output:

    6912
  7. Newline:

  8. Program Exits.

Usage

  1. Make sure you are running this code on a Linux environment or emulator that supports the syscalls used in the code.

  2. Assemble the code using an appropriate assembler (e.g., NASM) and linker (e.g., LD).

  3. Run the assembled binary to execute the program.

  4. Follow the instructions to input two 4-digit decimal numbers.

  5. The program will display the sum of the numbers in ASCII form.

Note: Assembly language is highly architecture-specific. Ensure you are using the appropriate assembler and platform for this code.


Last updated