5
5
%macro IO 4
mov rax, %1
mov rdi, %2
mov rsi, %3
mov rdx, %4
syscall
%endmacro
section .data
nline db 10,10
nline_len equ $ - nline
ano db 10,"------------------------------------------------",
db 10, "Conversion From Hex to BCD and BCD to Hex number",
db 10, "-------------------------------------------------", 10
ano_len equ $ - ano
section .bss
buf resb 6
char_ans resb 4
ans resw 1
section .text
global _start
_start:
IO 1, 1, ano, ano_len
Menu:
IO 1, 1, menu, menu_len
IO 0, 0, buf, 2
c1:
cmp al, '1'
jne c2
call hexToBcd
jmp Menu
c2:
cmp al, '2'
jne c3
call bcdHex
jmp Menu
c3:
cmp al, '3'
jne invalid
call exit
invalid:
IO 1, 1, emsg, emsg_len
jmp Menu
hexToBcd:
IO 1, 1, hmsg, hmsg_len
call acceptInput
mov ax, bx
mov bx, 10
xor bp, bp
back:
xor dx, dx
div bx
push dx
inc bp
cmp ax, 0
jne back
IO 1, 1, ebmsg, ebmsg_len
back1:
pop dx
add dl, 30h
mov [char_ans], dl
IO 1, 1, char_ans, 1
dec bp
jnz back1
ret
bcdHex:
IO 1, 1, bmsg, bmsg_len
IO 0, 0, buf, 6
mov rsi, buf
xor ax, ax
mov rbp, 5
mov rbx, 10
next:
xor cx, cx
mul bx
mov cl, [rsi]
sub cl, 30h
add ax, cx
inc rsi
dec rbp
jnz next
mov [ans], ax
IO 1, 1, ehmsg, ehmsg_len
display16:
mov rsi, char_ans + 3
mov rcx, 4
mov rbx, 16
nextDigits:
xor rdx, rdx
div rbx
cmp dl, 9
jbe add30
add dl, 07h
add30:
add dl, 30h
mov [rsi], dl
dec rsi
dec rcx
jnz nextDigits
IO 1, 1, char_ans, 4
ret
acceptInput:
IO 0, 0, buf, 5
mov rcx, 4
mov rsi, buf
xor bx, bx
nextByte:
shl bx, 4
mov al, [rsi]
error:
IO 1, 1, emsg, emsg_len
call exit;
sub57:
sub al, 20h
sub37:
sub al, 07h
sub30:
sub al, 30h
add bx, ax
inc rsi
dec rcx
jnz nextByte
ret
exit:
IO 1,1, nline, nline_len
mov rax, 60
mov rdi, 0
syscall
ret
ret
; Output
--------------------------------------------------------------------------
-------------
; ------------------------------------------------
; Conversion From Hex to BCD and BCD to Hex number
; -------------------------------------------------
; 1.HEX to BCD
; 2.BCD to HEX
; 3.Exit
; Enter your choice: 1