Assignment 8
m1 db 10,"Enter 4-digit Hex number:"
l1 equ
β
π
1
π
2
π
π
1
0
,
"
πΈ
π
π’
π
π£
π
π
π
π
π‘
π΅
πΆ
π·
π
π’
π
π
π
π
π
π
:
"
π
2
π
π
π’
βm1m2db10,"EquivalentBCDnumberis:"l2equ- m2
emsg db 10,You entered Invalid Data!!!
l3 equ
β
π
π
π
π
;
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
β
π
π
π
π‘
π
π
π
.
π
π
π
π
π’
π
π
π
π
π
6
π
π’
π
π
π
π
:
π
π
π’
βemsg;βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββsection.bssbufresb6buf
l
β
en:equ-buf
digitcount resb 1
ans resw 1
char_ans resb 4
;---------------------------------------------------------------------
;macros as per 64-bit convensions
%macro print 2
mov rax,1 ; Function 1 - write
mov rdi,1 ; To stdout
mov rsi,%1 ; String address
mov rdx,%2 ; String size
syscall ; invoke operating system to WRITE
%endmacro
%macro read 2
mov rax,0 ; Function 0 - Read
mov rdi,0 ; from stdin
mov rsi,%1 ; buffer address
mov rdx,%2 ; buffer size
syscall ; invoke operating system to READ
%endmacro
%macro exit 0
mov rax, 60 ; system call 60 is exit
mov rdi,0 ; we want return code 0
syscall ; invoke operating system to exit
%endmacro
section .text
global _start
_start:
print m1,l1
call accept_num
mov ax,bx
mov rbx,10
back:
xor rdx,rdx
div rbx
push dx ;Remainder
inc byte[digitcount]
cmp rax,0h ;quotient
jne back
print m2, l2
print_bcd:
pop dx
add dl,30h ; possible digits are 0-9 so add 30H only
mov [char_ans],dl ; store character in char_ans
print char_ans,1 ; print on screen in reverse order
dec byte[digitcount]
jnz print_bcd
exit
accept_num:
read buf,5 ; buflen = 4 + 1
xor bx,bx
mov rcx,4
mov rsi,buf
next_digit:
shl bx,04
mov al,[rsi]
cmp al,"0" ; "0" = 30h or 48d
jb error ; jump if below "0" to error
cmp al,"9"
jbe sub30 ; subtract 30h if no is in the range "0"-"9"
cmp al,"A" ; "A" = 41h or 65d
jb error ; jump if below "A" to error
cmp al,"F"
jbe sub37 ; subtract 37h if no is in the range "A"-"F"
cmp al,"a" ; "a" = 61h or 97d
jb error ; jump if below "a" to error
cmp al,"f"
jbe sub57 ; subtract 57h if no is in the range "a"-"f"
error: print emsg, l3 ; "You entered Invalid Data!!!"
exit
sub57: sub al,57h ; subtract 57h if no is in the range "a"-"f"
jmp next
sub37: sub al,37h ; subtract 37h if no is in the range "a"-"f"
jmp next
sub30: sub al,30h ; subtract 30h if no is in the range "0"-"9"
next: add bx,ax ; prepare number
inc rsi ; point to next digit
loop next_digit
ret