OneCompiler

Assignment 8

100

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