;====================================================
; COA Assignment 3
; 64-bit Assembly Language Program
; Addition of Two Numbers
;====================================================

;----------------------------------------------------
; WRITE MACRO
;----------------------------------------------------

%macro WRITE 2
    mov rax, 1
    mov rdi, 1
    mov rsi, %1
    mov rdx, %2
    syscall
%endmacro


;----------------------------------------------------
; READ MACRO
;----------------------------------------------------

%macro READ 2
    mov rax, 0
    mov rdi, 0
    mov rsi, %1
    mov rdx, %2
    syscall
%endmacro


;----------------------------------------------------
; EXIT MACRO
;----------------------------------------------------

%macro EXIT 0
    mov rax, 60
    mov rdi, 0
    syscall
%endmacro


;====================================================
; DATA SECTION
;====================================================

section .data

    msg1 db "Enter first number: "
    len1 equ $ - msg1

    msg2 db "Enter second number: "
    len2 equ $ - msg2

    msg3 db "Addition is: "
    len3 equ $ - msg3


;====================================================
; BSS SECTION
;====================================================

section .bss

    a resq 1
    b resq 1
    c resq 1

    char_buff resb 16
    actl resq 1


;====================================================
; TEXT SECTION
;====================================================

section .text

    global _start


;====================================================
; MAIN PROGRAM
;====================================================

_start:

    ;-----------------------------------------------
    ; Read first number
    ;-----------------------------------------------

    WRITE msg1, len1

    READ char_buff, 16

    call accept

    mov [a], rbx


    ;-----------------------------------------------
    ; Read second number
    ;-----------------------------------------------

    WRITE msg2, len2

    READ char_buff, 16

    call accept

    mov [b], rbx


    ;-----------------------------------------------
    ; ADDITION
    ;-----------------------------------------------

    mov rax, [a]

    add rax, [b]

    mov [c], rax


    ;-----------------------------------------------
    ; Display result
    ;-----------------------------------------------

    WRITE msg3, len3

    mov rbx, [c]

    call display


    ;-----------------------------------------------
    ; Exit program
    ;-----------------------------------------------

    EXIT


;====================================================
; ACCEPT PROCEDURE
; Converts ASCII input into hexadecimal number
;====================================================

accept:

    dec rax

    mov [actl], rax

    mov rbx, 0

    mov rsi, char_buff


up:

    shl rbx, 4

    mov rdx, 0

    mov dl, byte [rsi]

    cmp dl, 39h

    jbe sub_30

    sub dl, 07h


sub_30:

    sub dl, 30h

    add rbx, rdx

    inc rsi

    dec qword [actl]

    jnz up

    ret


;====================================================
; DISPLAY PROCEDURE
; Converts hexadecimal number into ASCII
;====================================================

display:

    mov rsi, char_buff

    mov rcx, 16


above:

    rol rbx, 4

    mov dl, bl

    and dl, 0Fh

    cmp dl, 09h

    jbe add_30

    add dl, 07h


add_30:

    add dl, 30h

    mov byte [rsi], dl

    inc rsi

    dec rcx

    jnz above


    WRITE char_buff, 16

    ret

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: