Header Ads

ASM Program to comvert BCD to HEX [MP Part2]

Problem Statement Part 2: 

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
 (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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
%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 5 Digit BCD Number: ",0x0A
len1: equ $-msg1
msg2: db "Hex Number: ",0x0A
len2:equ $-msg2
cnt :db 00H
cnt2:db 00H

Section .bss
num:resb 6
ans: resb 4
result:resb 8

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

mov rsi,num
call AtoH

mov edx,ebx ;copy conveted number to edx


MOV eax,00H
mov ebx,00H
mov al,dl

AND AL,0FH
mov Bl,01H
MUL Bl
ADD dword[ans],eax
ROR EDX,4

MOV EAX,00H
mov ebx,00H
MOV AL,DL
AND AL,0FH
MOV BL,0AH
MUL BL
ADD DWORD[ans],EAX
ROR EDX,4

MOV EAX,00H
mov ebx,00H
MOV AL,DL
AND AL,0FH
MOV BL,64H
MUL BL
ADD DWORD[ans],EAX
ROR EDX,4

MOV EAX,00H
mov ebx,00H
MOV AL,DL
AND AL,0FH
MOV BX,03E8H
MUL BX
ADD DWORD[ans], EAX
ROR EDX,4

MOV EAX,00H
mov ebx,00H
MOV AL,DL
AND AL,0FH
MOV BX,2710H  ;FIFTH DIGIT (100000 dECIMAL)
MUL BX
ADD DWORD[ans],EAX
 
MOV EDX, DWORD[ans]
MOV RDI,result
call HtoA

scall 1,1,msg2,len2
scall 1,1,result,8
mov rax,60
mov rdi,0
syscall

AtoH:  ;result hex no is in ebx
mov byte[cnt],05H
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

HtoA: ;hex_no to be converted is in dx //result is stored in rdi/user defined variable
mov byte[cnt2],08H
aup:
rol EDX,04
mov cl,dl
and cl,0FH
CMP CL,09H
jbe ANEXT
ADD cl,07H
ANEXT: 
add cl, 30H
mov byte[rdi],cl
INC rdi
dec byte[cnt2]
JNZ aup
ret

Output: 

 

No comments:

Powered by Blogger.