Monday, 22 February 2016

NASM Program to add 2 numbers

section .data
msg1 db "Enter a number: "
len1 equ $-msg1
msg2 db "Enter the second number: "
len2 equ $-msg2
msg3 db "Sum = "
len3 equ $-msg3

section .bss
num1 resb 2
num2 resb 2
res resb 1
r1 resb 1
section .txt
global _start
_start:
mov eax, 4
mov ebx, 1
mov ecx, msg1
mov edx, len1
int 80h

mov eax, 3
mov ebx, 0
mov ecx, num1
mov edx, 2
int 80h

mov eax, 4
mov ebx, 1
mov ecx, msg2
mov edx, len2
int 80h

mov eax, 3
mov ebx, 0
mov ecx, num2
mov edx, 2
int 80h

mov eax, [num1]
sub eax, 30h
mov ebx, [num2]
sub ebx, 30h
add eax, ebx
daa
mov [res], eax

mov eax, 4
mov ebx, 1
mov ecx, msg3
mov edx, len3
int 80h

mov al, [res]
and ax, 00f0h
shr ax, 4
add ax, 30h
mov [r1], ax

mov ecx, r1
mov edx, 1
mov eax, 4
mov ebx, 1
int 80h

mov al, [res]
and ax, 000fh
add ax, 30h
mov [r1], ax

mov ecx, r1
mov edx, 1
mov eax, 4
mov ebx, 1
int 80h

mov eax, 1
mov ebx, 0
int 80h

No comments:

Post a Comment