%macro scall 4
mov rax, %1
mov rdi, %2
mov rsi, %3
mov rdx, %4
syscall
%endmacro
section .data
m1 db 10, "Enter 4 digit hex no."
l1 equ $-m1
m2 db 10, 13, "Equivalent BCD no. is: "
l2 equ $-m2
section .bss
buf resb 6
digitcount resb 1
char_ans resb 4
section .text
global _start
_start:
scall 01, 01, m1, l1
scall 0, 0, buf, 5
call accept_proc
mov ax, bx
call h2bproc
;---------------exit--------------------------
mov rax, 60
mov rdi, 0
syscall
;----------------HEX to BCD-------------------
h2bproc:
mov rbx, 0Ah ; Set divisor to 10 (hex to decimal conversion)
back:
xor rdx, rdx ; Clear rdx for division
div rbx ; Divide by 10
push dx ; Push the remainder on the stack
inc byte [digitcount] ; Increment digit count
cmp rax, 0h ; Check if quotient is zero
jne back ; If not, continue
scall 01, 01, m2, l2 ; Print "Equivalent BCD no. is"
print_bcd:
pop dx ; Pop the remainder from the stack
add dl, 30h ; Convert to ASCII character
mov [char_ans], dl ; Store the character in char_ans
scall 01, 01, char_ans, 1 ; Print the character
dec byte [digitcount] ; Decrement digit count
jnz print_bcd ; If more digits, print next
ret
;-------------------accept procedure-----------------
accept_proc:
xor bx, bx
mov rcx, 4
mov rsi, buf
next_digit:
shl bx, 04
mov al, [rsi]
cmp al, 39h
jbe label1
sub al, 07h
label1:
sub al, 30h
add bx, ax
inc rsi
loop next_digit
ret
To embed this project on your website, copy the following code and paste it into your website's HTML: