Header Ads

ASM Program to Convert Hex to BCD [MP Part1]


Problem Statement: 

Write X86/64 ALP to convert 4 - digit Hex number into its equivalent BCD number and 5 - digit BCD n umber 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 [ Included in Program]
 (c) EXIT. Display proper strings to prompt the user while accepting the input and displaying the result. (wherever necess ary, use 64 - bit registers)

Code:-


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
%macro scall 4                    ;macro to take input and output
        mov rax,%1
        mov rdi,%2
        mov rsi,%3
        mov rdx,%4
        syscall
%endmacro

Section .data
msg1: db "Enter 4 Digit HEX Number: ",0x0A
len1: equ $-msg1
msg2:db "BCD Number : " 
len2: equ $-msg2
cnt :db 00H

Section .bss
ans resw 8
num resb 8

Section .text
global main
main:
 scall 1,1,msg1,len1
scall 0,0,num,8

mov rsi,num
call AtoH


mov rax,rbx
mov rbx,000AH
mov rdi,ans+7
loop:
mov rdx,0
div rbx
add dl,30h
mov [rdi],dl
dec rdi

cmp rax,0
jne loop



scall 1,1,msg2,len2
scall 1,1,ans,8
;add rax,30
;mov word[ans],ax
;dec cx
;jnz ploop


;scall 1,1,ans,8

mov rax,60
mov rdi,0
syscall



AtoH:  ;result hex no is in bx
mov byte[cnt],04H
mov ebx,00H
hup:
rol ebx,04
mov al,byte[rsi]
cmp al,39H
JBE HNEXT
SUB al,07H
HNEXT:
sub al,30H
add bl,al
INC rsi
DEC byte[cnt]
JNZ hup
ret

Output: 

 

No comments:

Powered by Blogger.