Q: Accept 10, 64 bit hexadecimal numbers from the user and print their sum.
Steps involved:-
- Accept the numbers in Ascii format
- Convert the numbers to hexadecimal form.
- Add the numbers to get the sum.
- Convert the sum to Ascii format.
- Display the result.
- OS:- Linux x64
- Assembler:- Nasm
- Debugger:- GDB
;macros begin %macro read 2 mov rax,00 mov rdi,01 mov rsi,%1 mov rdx,%2 syscall %endmacro %macro write 2 mov rax,01 mov rdi,00 mov rsi,%1 mov rdx,%2 syscall %endmacro ;macros end section .data msg1 db "Enter number",10 len1 equ $-msg1 msg2 db "The answer of the addition is",10 len2 equ $-msg2 section .bss innum resb 170 count resb 1 count2 resb 1 asciians resb 16 section .text global _start _start: ;reading of numbers mov r8,innum mov byte[count],10 l1: write msg1,len1 read r8,17 add r8,17 dec byte[count] jnz l1 ;reading of numbers is now complete mov r15,0 mov r14,0 mov byte[count],10 mov r8,innum l2: call a2h add r14,r9 adc r15,0h add r8,1 dec byte[count] jnz l2 ;addition complete mov rax,r15 call h2a write msg2,len2 write asciians,16 mov rax,r14 call h2a write asciians,16 ;exit system call mov rax,60 mov rdi,0 syscall a2h: ;source at r8, answer at r9 mov byte[count2],16 mov r9,0 mov rax,0 loop: rol r9,4 mov rax,0 mov al,[r8] sub al,30h cmp al,9h jbe nocorrection sub al,7h nocorrection: add r9,rax inc r8 dec byte[count2] jnz loop ret h2a: ;source at rax, answer at asciians mov rdi,asciians mov byte[count2],10 loop2: rol rax,04 mov bl,al and bl,0fh cmp bl,09h jbe nocorrection2 add bl,07 nocorrection2: add bl,30h mov byte[rdi],bl inc rdi dec byte[count2] jnz loop2 ret
Sample Output
speedy@comp:~$ nasm -f elf64 asgn1.asm
speedy@comp:~$ ld -o asgn1 asgn1.o
speedy@comp:~$ ./asgn1
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
The answer of the addition is
0000000000000009F49F49F49F49F4A0speedy@comp:~$
speedy@comp:~$ ld -o asgn1 asgn1.o
speedy@comp:~$ ./asgn1
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
Enter number
FEDCBA9876543210
The answer of the addition is
0000000000000009F49F49F49F49F4A0speedy@comp:~$
No comments:
Post a Comment