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.

%macro dispmsg 2
mov eax,04
mov ebx,01
mov ecx,%1
mov edx,%2
int 80h
%endmacro

%macro accept 2
mov eax,03
mov ebx,00
mov ecx,%1
mov edx,%2
int 80h
%endmacro

%macro exit 0
dispmsg thankmsg,thankmsg_len
mov eax,01        ;Exit
mov ebx,00
int 80h
%endm exit

section .data
menumsg db 10,10,”###### Menu for Code Conversion ######”
db 10,13,”1: Hex to BCD”
db 10,13,”2: BCD to Hex”
db 10,13,”3: Exit”
db 10,10,”Please Enter Choice::”
menumsg_len equ $-menumsg
wrchmsg db 10,10,”Wrong Choice Entered….Please try again!!!”
wrchmsg_len equ $-wrchmsg

hexinmsg db 10,10,”Please enter 4 digit hex number::”
hexinmsg_len equ $-hexinmsg

bcdopmsg db 10,10,”BCD Equivalent::”
bcdopmsg_len equ $-bcdopmsg

bcdinmsg db 10,10,”Please enter 5 digit BCD number::”
bcdinmsg_len equ $-bcdinmsg

hexopmsg db 10,10,”Hex Equivalent::”
hexopmsg_len equ $-hexopmsg

thankmsg db 10,13,”Thank you for using Program by Prof. Dhokane Rahul”
thankmsg_len equ $-thankmsg

section .bss
numascii resb 06        ;common buffer for choice, hex and bcd input
opbuff resb 05
dnumbuff resb 08

section .text
global _start
_start:

dispmsg menumsg,menumsg_len
accept numascii,2

cmp byte[numascii],”1″
jne case2
call hex2bcd_proc

jmp _start

case2:   
cmp byte [numascii],”2″
jne case3
call bcd2hex_proc
jmp _start

case3:    cmp byte [numascii],”3″
je ext
dispmsg wrchmsg,wrchmsg_len
jmp _start

ext:
exit

hex2bcd_proc:
dispmsg hexinmsg,hexinmsg_len
accept numascii,5
call packnum
mov ecx,5
mov ax,bx
mov bx,10
h2bup1:   
mov dx,0
div bx
push edx
dec ecx
jnz h2bup1

mov edi,opbuff
mov ecx,5
h2bup2:   
pop edx
add dl,30h
mov [edi],dl
inc edi
dec ecx
jnz h2bup2

dispmsg bcdopmsg,bcdopmsg_len
dispmsg opbuff,5
ret

bcd2hex_proc:
dispmsg bcdinmsg,bcdinmsg_len
accept numascii,6

dispmsg hexopmsg,hexopmsg_len

mov esi,numascii
mov ecx,05
mov eax,0
mov ebx,0ah

b2hup1:   
mov edx,0
mul ebx
mov dl,[esi]
sub dl,30h
add eax,edx
inc esi
dec ecx
jnz b2hup1
mov ebx,eax
call disp32_num
ret

packnum:
mov bx,0
mov ecx,04
mov esi,numascii
up1:
rol bx,04
mov al,[esi]
cmp al,”9″
jbe sub30
cmp al,”F”
jbe sub37
cmp al,”f”
jbe sub57
sub57:    sub al,20h
sub37:    sub al,07h
sub30:    sub al,30h
add bl,al
inc esi
dec ecx
jnz up1
ret

disp32_num:
mov edi,dnumbuff    ;point esi to buffer

mov ecx,08        ;load number of digits to display

dispup1:
rol ebx,4        ;rotate number left by four bits
mov dl,bl        ;move lower byte in dl
and dl,0fh        ;mask upper digit of byte in dl
add dl,30h        ;add 30h to calculate ASCII code
cmp dl,39h        ;compare with 39h
jbe dispskip1        ;if less than 39h adding 07 more
add dl,07h        ;else add 07

dispskip1:
mov [edi],dl        ;store ASCII code in buffer
inc edi            ;point to next byte
dec ecx
jnz dispup1        ;decrement the count of digits to display
;if not zero jump to repeat

dispmsg dnumbuff+3,5
ret

OUTPUT:
swlab@swlab-Veriton-M200-H81:~/MICROPROCESSOR 2017-18$ nasm -f elf h2b.asm
swlab@swlab-Veriton-M200-H81:~/MICROPROCESSOR 2017-18$ ld -m elf_i386 -s -o h2b h2b.o
swlab@swlab-Veriton-M200-H81:~/MICROPROCESSOR 2017-18$ ./h2b

###### Menu for Code Conversion ######
1: Hex to BCD
2: BCD to Hex
3: Exit

Please Enter Choice::1

Please enter 4 digit hex number::ffff

BCD Equivalent::65535

###### Menu for Code Conversion ######
1: Hex to BCD
2: BCD to Hex
3: Exit

Please Enter Choice::2

Please enter 5 digit BCD number::65535

Hex Equivalent::0FFFF

###### Menu for Code Conversion ######
1: Hex to BCD
2: BCD to Hex
3: Exit

Please Enter Choice::3

Thank you for using Program by Prof. Dhokane Rahul

Leave a Reply

Your email address will not be published. Required fields are marked *