Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;ALP program to convert HEX to BCD
- ;--------------------------------------------------
- section .data
- msg1 db "Enter 4 digit HEX number:"
- msg1_len equ $-msg1
- msg2 db "Converted into Decimal number:"
- msg2_len equ $-msg2
- emsg db "Invalid BCD number"
- emsg_len equ $-emsg
- ;-----------------------------------------------------------
- section .bss
- buf resb 2
- ans resb 2
- char_ans resb 2
- %macro print 2
- mov rax,1
- mov rdi,1
- mov rsi,%1
- mov rdx,%2
- syscall
- %endmacro
- %macro read 2
- mov rax,0
- mov rdi,0
- mov rsi,%1
- mov rdx,%2
- syscall
- %endmacro
- %macro exit 0
- mov rax,60
- mov rdi,0
- syscall
- %endmacro
- ;----------------------------------------------------------
- section .text
- global _start
- _start:
- Call HEX_BCD
- exit
- HEX_BCD:
- print msg1,msg1_len
- read buf,5
- mov rcx,4
- mov rsi,buf
- mov bx,0
- next_byte:
- shl bx,4
- mov al,[rsi]
- cmp al,'0'
- JB error
- cmp al,'9'
- JBE sub30
- cmp al,'A'
- JB error
- cmp al,'F'
- JBE sub37
- cmp al,'a'
- JB error
- cmp al,'f'
- JBE sub57
- error:
- print emsg,emsg_len
- exit
- sub57 : sub al,20h
- sub37 : sub al,07h
- sub30 : sub al,30h
- Add bx,ax
- INC rsi
- DEC rcx
- JNZ next_byte
- print msg2,msg2_len
- mov ax,bx
- Call Display
- ret
- Display:
- mov rbx,10
- mov rcx,4
- mov rsi,char_ans+3
- back:
- mov rdx,0
- div rbx
- cmp dl,09h
- jbe add30
- add dl,07h
- add30:
- add dl,30h
- mov [rsi],dl
- dec rsi
- dec rcx
- jnz back
- print char_ans,4
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement