Write X86/64 ALP to convert 4-digit Hex number into its equivalent BCD number and 5-digit BCD number into its equivalent HEX number.
Second Year Computer Engineering Microprocessor Programs:
Microprocessor Lab:
Practicle 3:Write X86/64 ALP to convert 4-digit Hex number into its equivalent BCD number and 5-digit BCD number into its equivalent HEX number. Make your program user friendly to accept the choice from user for:
a) HEX to BCD
b) BCD to HEX
c) EXIT.
Display proper strings to prompt the user while accepting the input and displaying the result.
----------------------------------------------------------------------------------------------------------------------------------
%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro read 2
mov rax,0 ; sys_inp
mov rdi,0 ; sys_std_ipd
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .data
msg db 10,10, "***Menu***"
db 10,10, "1.Hex to BCD"
db 10,10, "2.BCD to Hex"
db 10,10, "3.Exit"
db 10,10, "Please enter your choice:"
msglen equ $-msg
msg1 db "Wrong choice",10
msg1len equ $-msg1
hexmsg db "enter 4 digit hex. no.:",10
hexmsglen equ $-hexmsg
hexmsg1 db "its Equ BCD is:",10
hexmsg1len equ $-hexmsg1
bcdmsg db "Enter 5 digit bcd no.:",10
bcdmsglen equ $-bcdmsg
bcdmsg1 db "Its equ. hex no. is:",10
bcdmsg1len equ $-bcdmsg1
section .bss
numascii resb 06
opbuff resb 05
dnumbuff resb 08
section .txt
global _start
_start:
print msg,msglen
read numascii,2
cmp byte[numascii],"1"
jne case2
call hex2bcd
jmp _start
case2:
cmp byte[numascii],"2"
jne case3
call bcd2hex
jmp _start
case3:
cmp byte[numascii],"3"
je exit
print msg1,msg1len
jmp _start
exit:
mov rax,60
mov rdi,0
syscall
hex2bcd:
print hexmsg,hexmsglen
read numascii,05
call packnum
mov rcx,0
mov ax,bx
mov bx,10h
up2:
mov rdx,0
div bx
push rdx
inc rcx
cmp ax,0
jne h2bup1
mov rdi,opbuff
h2bup1:
pop rdx
add dl,30h
mov [rdi],dl
inc rdi
loop h2bup1
packnum:
mov bx,0
mov rcx,4
mov rsi,numascii
up1:
rol bx,04
mov al,[rsi]
cmp al,39h
jbe skip1
sub al,07h
skip1:
sub al,30h
add bl,al
loop up1
ret
bcd2hex:
print bcdmsg,bcdmsglen
read numascii,06
print bcdmsg1,bcdmsg1len
mov rcx,05
mov rsi,numascii
mov rax,0
mov rbx,0Ah
again1:
mov rdx,0
mul rbx
mov dl,[rsi]
sub dl,30h
add rax,rdx
inc rsi
loop again1
mov rbx,rax
call dispnum
ret
dispnum:
mov rdi,dnumbuff
mov rcx,08
up3:
rol ebx,04
mov dl,bl
and dl,0Fh
add dl,30h
cmp dl,39h
jbe dskip1
add dl,07h
dskip1:
mov [rdi],dl
inc rdi
loop up3
print dnumbuff+4,4
ret
----------------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment