;Roll No.
: 2521
;Title : HEX to BCD and BCD to HEX conversion
.model small
.data
msg1 db 10,13,"Enter HEX No. : $"
msg2 db 10,13,"Enter BCD No. : $"
msg3 db 10,13,"BCD of Entered No. : $"
msg4 db 10,13,"HEX of Entered No. : $"
msg5 db 10,13,"Press : $"
msg6 db 10,13,"1 - HEX to BCD$"
msg7 db 10,13,"2 - BCD to HEX$"
msg71 db 10,13,"3 - Exit$"
msg8 db 10,13,"Enter your choice : $"
msg9 db 10,13,"Invalid Choice!$"
arr dw 2710h,03e8H,0064h,000Ah,0001h
cnt db ?
stkb dw ?
c db ?
.stack
stcc dw 10h dup(00h)
.code
start:
mov ax,@data ;Data Initialization
mov ds,ax
mov ax,stack ;Stack Initialization
mov ss,ax
disp macro msg ;Message display Macro
mov ah,09h
lea dx,msg
int 21h
endm
disp msg5 ;Display The Menu and Choices
disp msg6
disp msg7
disp msg71
disp msg8
mov ah,01h ;Accept the choice
int 21h
cmp al,31h
jne case2
call hextobcd ;Call HEX_to_BCD if choice is 1
jmp endd
case2:
cmp al,32h
jne case3
call bcdtohex ;Call BCD_to_HEX if choice is 2
jmp endd
case3:
cmp al,33h
jne invld
jmp endd ;Exit if choice is 3
invld:
disp msg9 ;Display Invalid Choice
;=============== END OF PROGRAM ========================
endd:
mov ah,4ch
int 21h
;========= HEX to BCD Procedure ================
hextobcd proc near
;------------- READ HEX NO. --------------
disp msg1
mov bx,0000h
mov cx,0404h
upp:
rol bx,cl
mov ah,01h
int 21h
cmp al,39h
jbe downn
sub al,07h
downn:
sub al,30h
add bl,al
dec ch
jnz upp
mov ax,bx
;--------- CONVERT HEX to BCD --------------
mov bx,000Ah
mov cl,00h
pop dx ;Store the existing contents of stack
mov stkb,dx ;in some temporary variable
up:
mov dx,0000h
div bx
add dl,30h
push dx
inc cl
cmp ax,0000h
jne up
;------------- DISPLAY BCD ------------------
disp msg3
up2:
pop dx
mov ah,02h
int 21h
dec cl
cmp cl,00h
jne up2
mov dx,stkb ;Restore the previous contents
push dx ;of stack
RET
endp
;=============== BCD to HEX Procedure ======================
bcdtohex proc near
;------------- READ BCD ---------------
disp msg2
mov cnt,05h
lea si,arr
xor bx,bx
xor dx,dx
uppp:
mov ah,01h
int 21h
sub al,30h
mov ah,00h
mov cx,[si]
mul cx ;Multiply the accepted digit with
;HEX powers of 10
cmp dl,00h
je dwn
mov c,dl
dwn:
inc si
inc si
add bx,ax ;Add the product to BX register
dec cnt
jnz uppp
;-------------- DISPLAY HEX ---------------
disp msg4
mov dl,c
add dl,30h
mov ah,02h
int 21h
xor dx,dx
mov cx,0404h
upp2:
rol bx,cl
mov dx,bx
and dx,000fh
cmp dl,09h
jbe down1
add dl,07h
down1:
add dl,30h
mov ah,02h
int 21h
dec ch
jnz upp2
ret
endp
;================ END OF PROCEDURE ===============
end start
OUTPUT:
Press :
1 - HEX to BCD
2 - BCD to HEX
3 - Exit
Enter your choice : 1
Enter HEX No. : FFFF
BCD of Entered No. : 65535
Press :
1 - HEX to BCD
2 - BCD to HEX
3 - Exit
Enter your choice : 2
Enter BCD No. : 65535
HEX of Entered No. : 0FFFF
Press :
1 - HEX to BCD
2 - BCD to HEX
3 - Exit
Enter your choice : 2
Enter BCD No. : 99999
HEX of Entered No. : 1869F