MP6
MP6
Program: Write an 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. (Wherever necessary,
use 64-bit registers).
Section .data
msg: db 0x0A
len: equ $-msg
;****************************************************
Section .bss
;****************************************************
%macro print 2 ;macro for printing
mov rax,0x01
mov rdi,0x01
mov rsi,%1
mov rdx,%2
syscall
%endmacro
;****************************************************
;****************************************************
Section .text
Global _start
_start:
menu:
print msg1,len1
read chc,0x02
cmp byte[chc],0x31
je hex
cmp byte[chc],0x32
je bcd
cmp byte[chc],0x33
je exit
print msg6,len6
jmp menu
hex:
print msg2,len2
read temp,0x06
mov rsi,temp
mov byte[cnta],0x04 ;4 digit Input
call ascii_hex
print msg3,len3
call hextobcd
print msg,len
jmp menu
;****************************************************
bcd:
print msg4,len4
read temp,0x06
mov rsi,temp
mov byte[cnta],0x05 ;5 digit input
call ascii_hex
print msg5,len5
call bcdtohex
print msg,len
jmp menu
;****************************************************
exit:
mov rax,0x3C
mov rdi,0x00
syscall
;****************************************************
hextobcd:
xor eax,eax
xor ecx,ecx
mov ax,word[data]
mov byte[cnt_div],0x05
loop1:
xor edx,edx
mov bx,0x0A
div bx ;Result in ax Remainder in dx
ror ecx,0x04
or cx,dx
dec byte[cnt_div]
jnz loop1
mov dword[result+0x04],ecx
mov byte[cntd],0x08
call disp
ret
;****************************************************
bcdtohex:
xor eax,eax
xor rcx,rcx
xor rbx,rbx
mov byte[cnt_mul],0x05
mov r8,[data]
ror r8,0x10 ;Get MSB at lowest nibble
back2:
mov bx,0x0A
mul bx
mov rcx,r8
and rcx,0xF ;Seperate req digit
add eax,ecx
rol r8,0x04
dec byte[cnt_mul]
jnz back2
mov dword[result+0x04],eax
mov byte[cntd],0x08
call disp
ret
;****************************************************
ascii_hex:
xor ebx,ebx
xor eax,eax
digit2:
mov bl,byte[rsi]
cmp bl,0x39
jbe digit1
sub bl,0x07
digit1:
sub bl,0x30
sal eax,0x04
add al,bl
inc rsi
dec byte[cnta]
jnz digit2
mov dword[data],eax
ret
;****************************************************
disp:
xor rbx,rbx
back:
rol qword[result],0x04
mov bl,byte[result]
and bl,0FH
cmp bl,09H
jbe next
add bl,0x07
next:
add bl,0x30
mov byte[res],bl
print res,1
dec byte[cntd]
jnz back
ret
OUTPUT:
1. HEX to BCD
2. BCD to HEX
3. Exit
Enter choice: 2
Enter 5-digit BCD number: 65520
Equivalent HEX: 0000FFF0
1. HEX to BCD
2. BCD to HEX
3. Exit
Enter choice: 2
Enter 5-digit BCD number: 12345
Equivalent HEX: 00003039
1. HEX to BCD
2. BCD to HEX
3. Exit
Enter choice: 1
Enter 4-digit HEX number: 3039
Equivalent BCD: 00012345
1. HEX to BCD
2. BCD to HEX
3. Exit
Enter choice: 3
(base) kj-comp@kjcomp-OptiPlex-380:~$