Wednesday, 23 December 2015

32 Bit:-Addition of 10 User Input Hexadecimal Number using Assembly Language x86 (With detailed comments)

Q: Accept 10, 32 bit hexadecimal numbers from the user and print their sum.

Detailed Comments :- Thanks to the free time in the lab. :-)


Assembly Language

Steps involved:-


  1. Accept the numbers in Ascii format
  2. Convert the numbers to hexadecimal form.
  3. Add the numbers to get the sum.
  4. Convert the sum to Ascii format.
  5. Display the result. 


 Code

  • OS:- Linux x64,x86
  • Assembler:- Nasm
  • Debugger:- GDB
;;;;;;;;Program by Abhishek Munagekar;;;;;;;;;;;;;;;;;;
;;;;;;;;For more visit www.prgwonders.blogspot.in;;;;;;
;;;;;;;;Facebook Page:-www.facebook.com/prgwonders;;;;;;
section .data
msg1 db "Enter the number",10  ;this message will be displayed for each number
len1 equ $-msg1
msg3 db "the addition of the numbers is ",10
len3 equ $-msg3

section .bss

result resb 8    ;to store the result in ascii format
sum resb 4    ;to store the sum in hex format
carry resb 4    ;to store the carry in hex format
count resb 1    ;used as counter to implement for loops
count2 resb 1
innum resb 90    ;used to store the input numbers


section .text
global _start:
_start:

mov byte[count],10   ;initialize the registers for accepting
mov esi,innum    ;the values 

mainloop:    ; the loop now begins
mov eax,04h     ;write system call for msg1
mov ebx,01h
mov ecx,msg1
mov edx,len1
int 80h
;;;;;;;;;; esi has been used as pointer;;;;;;;;;;;;;;;;;;;;;;;;;;

mov eax,03h     ;read system call for numbers
mov ebx,01h
mov ecx,esi
mov edx,9    ;9=8+1, 8 for digits and 1 for the enter that you must press
int 80h

add esi,9
dec byte[count]
jnz mainloop


;;;;;;;;;;input stage complete;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;conversion begins;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

mov byte[count2],10
mov esi,innum    ;initialize the register for this process

mov ecx,0h      ;to store a single hex number
mov dword[sum],0h     ;ecx to store the addition
mov dword[carry],0h     ;to store the carry



mainloop2:    ;this is the loop for 10 nos 
mov byte[count],8    ;conversion and addition for one num

s:
rol ecx,4    ;shifts by 4 bits
mov eax,0h
mov al,[esi]
sub al,30H    ;core conversion logic for ascii to hex
cmp al,9
jbe l
sub al,7
l:
inc esi
add ecx,eax    ;add each digit to ecx so as to generate the final number
     ;ecx will contain the hex equivalent of the numbers

dec byte[count]
jnz s
add dword[sum],ecx   ;computation of the sum
adc dword[carry],0h     ;carry calculating
mov ecx,0h    ;set ecx to zero so that it can be used for the nex number
add esi,1
dec byte[count2]
jnz mainloop2

;;;;;;;;;;;conversion and addition is now complete;;;;;;;;;;;
;;;;;;;;;;;Yay time to print the result;;;;;;;;;;;;;;;;;;;;;;
  
mov eax,dword[carry]   

mov esi ,result     ;conversion for carry
mov byte[count],8
l8: rol eax, 04H
mov bl, al 
and bl, 0FH     ;Masking the higher bits
cmp bl, 09     ;Compare with nine
JBE m8      ;If below or equal jump to label m
add bl, 07H     ;else add correction factor
m8: add bl, 30H
mov [esi], bl     ;store ASCII value in esi
inc esi
Dec byte[count]
JNZ l8

mov eax,04h     ;write system call for msg3
mov ebx,01h
mov ecx,msg3
mov edx,len3
int 80h

mov eax,04h     ;write system call for carry
mov ebx,01h
mov ecx,result
mov edx,8
int 80h


mov eax,dword[sum]
mov esi ,result
mov byte[count],8
l9: rol eax, 04H
mov bl, al 
and bl, 0FH     ;Masking the higher bits
cmp bl, 09     ;Compare with nine
JBE m9      ;If below or equal jump to label m
add bl, 07H     ;else add correction factor
m9: add bl, 30H
mov [esi], bl     ;store ASCII value in esi
inc esi
Dec byte[count]
JNZ l9

mov eax,04h     ;write system call for answer
mov ebx,01h
mov ecx,result
mov edx,8
int 80h

mov eax,1h    ;exit system call
mov ebx,0
int 80h

Sample Output 

[secomp@localhost asgn1_32]$ nasm -f elf asgn1_32.asm
[secomp@localhost asgn1_32]$ ld -m elf_i386 -o asgn1_32 asgn1_32.o
[secomp@localhost asgn1_32]$ ./asgn1_32
Enter the number
12341234
Enter the number
12341234
Enter the number
12341234
Enter the number
12341234
Enter the number
12341234
Enter the number
12341234
Enter the number
12341234
Enter the number
12341234
Enter the number
12341234
Enter the number
12341234
the addition of the numbers is
00000000B608B608

No comments:

Post a Comment