Problem Statement: Write X86/64 Assembly language program (ALP) to add array of N hexadecimal numbers stored in the
memory. Accept input from the user.
%macro print 2 ;macro to print
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro accept 2 ;macro to accept
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
SECTION .data
msg1: db "Enter quantity of numbers to add: "
len1: equ $-msg1
msg2: db "Enter the hexadecimal numbers: ",0xA
len2: equ $-msg2
msg3: db "The sum is: "
len3: equ $-msg3
msg4: db " ",0xA
len4:equ $-msg4
SECTION .bss
input: resb 100
result: resb 4
count1: resb 2
count2: resb 2
count: resb 2
num: resb 3
SECTION .text
global main
main:
print msg1,len1 ;calling macro to print
accept num,3 ;calling macro to accept
call convert ;calling procedure to convert ASCII to hex
mov byte[count1],bl
mov byte[count2],bl
print msg2,len2
mov r9,input
array: ;accept numbers and store it in the array
accept num,3
call convert
mov [r9],bl
inc r9
dec byte[count1]
jnz array
mov r9,input
mov al,00
mov ah,00
call add ;calling procedure to add the numbers
print msg3,len3
print result,4
print msg4,len4
mov rax,60
mov rdi,1
syscall
convert: ;procedure to convert ASCII to hex
mov ch,02
mov bl,00
mov r10,0
loop:
mov al,[num+r10]
cmp al,61H ;comparing for lower case letters
jb next
sub al,57H
jmp next2
next:
cmp al,41h ;comparing for upper case letters
jb next1
sub al,37H
jmp next2
next1:
sub al,30H ;comparing for numbers
next2:
shl bl,04H
add bl,al
inc r10
dec ch
jnz loop
ret
add: ;procedure to add and convert hex to ASCII
mov bl,[r9]
add ax,bx ;to add numbers in the array
inc r9
dec byte[count2]
jnz add
mov r10,result
mov byte[count],4
l1: ;to convert to ASCII
rol ax,04H
mov dl,al
and dl,0FH
cmp dl,09H
jbe l2
add dl,07H
l2:
add dl,30H
mov[r10],dl
inc r10
dec byte[count2]
jnz l1
ret
%macro print 2 ;macro to print
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro accept 2 ;macro to accept
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
SECTION .data
msg1: db "Enter quantity of numbers to add: "
len1: equ $-msg1
msg2: db "Enter the hexadecimal numbers: ",0xA
len2: equ $-msg2
msg3: db "The sum is: "
len3: equ $-msg3
msg4: db " ",0xA
len4:equ $-msg4
SECTION .bss
input: resb 100
result: resb 4
count1: resb 2
count2: resb 2
count: resb 2
num: resb 3
SECTION .text
global main
main:
print msg1,len1 ;calling macro to print
accept num,3 ;calling macro to accept
call convert ;calling procedure to convert ASCII to hex
mov byte[count1],bl
mov byte[count2],bl
print msg2,len2
mov r9,input
array: ;accept numbers and store it in the array
accept num,3
call convert
mov [r9],bl
inc r9
dec byte[count1]
jnz array
mov r9,input
mov al,00
mov ah,00
call add ;calling procedure to add the numbers
print msg3,len3
print result,4
print msg4,len4
mov rax,60
mov rdi,1
syscall
convert: ;procedure to convert ASCII to hex
mov ch,02
mov bl,00
mov r10,0
loop:
mov al,[num+r10]
cmp al,61H ;comparing for lower case letters
jb next
sub al,57H
jmp next2
next:
cmp al,41h ;comparing for upper case letters
jb next1
sub al,37H
jmp next2
next1:
sub al,30H ;comparing for numbers
next2:
shl bl,04H
add bl,al
inc r10
dec ch
jnz loop
ret
add: ;procedure to add and convert hex to ASCII
mov bl,[r9]
add ax,bx ;to add numbers in the array
inc r9
dec byte[count2]
jnz add
mov r10,result
mov byte[count],4
l1: ;to convert to ASCII
rol ax,04H
mov dl,al
and dl,0FH
cmp dl,09H
jbe l2
add dl,07H
l2:
add dl,30H
mov[r10],dl
inc r10
dec byte[count2]
jnz l1
ret
Output
amodi@ubuntu:~/MIL/Assign1$ nasm -f elf64 -l add64.lst
add64.asm
amodi@ubuntu:~/MIL/Assign1$ ld -o add64 add64.o
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
amodi@ubuntu:~/MIL/Assign1$ ./add64
Enter quantity of numbers to add: 06
Enter the hexadecimal numbers:
11
22
33
AA
CC
DF
The sum is: 02BB
amodi@ubuntu:~/MIL/Assign1$ ld -o add64 add64.o
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
amodi@ubuntu:~/MIL/Assign1$ ./add64
Enter quantity of numbers to add: 06
Enter the hexadecimal numbers:
11
22
33
AA
CC
DF
The sum is: 02BB
No comments:
Post a Comment