Write assembly language program (alp) to add array of n numbers stored in the memory. Dowlload
Answers
Code:
section .data
msg1: dd ‘Enter the number of 2 digit numbers to be added:’,10
len1 : equ $-msg1
msg2: dd ‘Sum is:’,10
len2 : equ $-msg2
errmsg: dd ‘Invalid Number’,10
errlen : equ $-errmsg
newnum: dd ‘Enter the Number Again : ‘,10
newnumlen : equ $-newnum
msg4: dd ‘Enter the numbers:’,10
len4 : equ $-msg4
section .bss
num resb 3
sum resb 1
carry resb 1
count resb 1
temp resb 4
section .text
global _start
_start:
%macro disp 2
mov eax,4
mov ebx,1
mov ecx,%1
mov edx,%2
int 80h
%endmacro
%macro in 2
mov eax,3
mov ebx,0
mov ecx,%1
mov edx,%2
int 80h
%endmacro
disp msg1,len1
in num,3
call ascii2hex
mov [count],bl
summation:
disp msg4,len4
in num,3
call ascii2hex
add byte[sum],bl
adc byte[carry],0
dec byte[count]
jnz summation
disp msg2,len2
mov bl,[sum]
mov bh,[carry]
call hex2ascii
mov eax,1
mov ebx,0
int 80h
ascii2hex:
mov ecx,2 ;to rotate 2 digits
mov bl,0 ;clear bl
mov esi,num ;put the input into esi for oper
up:
rol bl,4 ;rotate bl by 4
mov al,[esi] ;last digit is put into al register
cmp al,39h ;compare the last digit if its a valid digit or not
jbe skip ;skip if its a valid digit
sub al,7h ;if its not subtract 7h to validate it(that is if its number A(41h) to F(46h) in hex )
skip:
sub al,30h ;convert the ascii to hex
add al,bl
mov bl,al
inc esi
loop up
jmp endr
invalid:
disp errmsg,errlen
disp newnum,newnumlen
in num,3
call ascii2hex
endr:
ret
hex2ascii:
mov ecx,4
mov edi,temp
next:
rol bx,4
mov dl,bl
and dl,0Fh
cmp dl,09
jbe label
add dl,07h
label:
add dl,30h
mov [edi],dl
inc edi
dec byte[count]
jnz next
disp temp,4
ret