Microprocessor/Codes/Practical-6.asm

158 lines
2.6 KiB
NASM
Raw Normal View History

; THIS CODE HAS NOT BEEN TESTED AND IS NOT FULLY OPERATIONAL.
; Problem Statement: Write X86/64 ALP to convert 4-digit Hex number into its equivalent BCD number and 5-digit BCD number into its equivalent HEX number.
; Make your program user friendly to accept the choice from user for: (a) HEX to BCD b) BCD to HEX (c) EXIT.
; Display proper strings to prompt the user while accepting the input and displaying the result. (wherever necessary, use 64-bit registers.)
; Code from Microprocessor (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/Microprocessor/
; BEGINNING OF CODE
%macro print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
%macro accept 2
mov rax,0
mov rdi,0
mov rsi,%1
mov rdx,%2
syscall
%endmacro
section .data
title db 10,"An ALP to convert BCD to HEX and vise-versa.",10
title_len equ $-title
menu db 10,"------ Select an option ------",10,"1 -> HEX to BCD",10,"2 -> BCD to HEX",10,"3 -> Exit",10
menu_len equ $-menu
hex_in db 10,"Enter your 4 digit HEX number: ",10
hex_inlen equ $-hex_in
bcd_out db 10,"The equivalent BCD number is: ",10
bcd_out_len equ $-bcd_out
bcd_in db 10,"Enter your BCD number: ",10
bcd_inlen equ $-bcd_in
hex_out db 10,"The equivalent HEX number is: ",10
hex_out_len equ $-hex_out
section .bss
numascii resb 6 ;for input
opbuff resb 5 ;to
dnumbuff resb 08
section .txt
global _start:
_start:
print title,title_len
print menu,menu_len
accept numascii,2 ;input user choice using accept macro(2 bytes-1 for choice and 1 for enter)
cmp byte[numascii],'1' ;ascii of 1 is 31
jne case2
call hextobcd
case2:
cmp byte[numascii],'2'
jne case3
call bcdtohex
case3:
cmp byte[numascii],'3'
je exit
exit:
mov rax,60
mov rdx,0
syscall
hextobcd:
print hex_in,hex_inlen
accept numascii,5
call packnum
mov rcx,0
mov ax,bx
mov bx,0Ah
h2bup1:
mov rax, 0 ; Clear rax before each division
div bx
push rdx
inc rcx
cmp ax,0
jne h2bup1
h2bup2:
pop rdx
add dl,30h
mov [rdi],dl
inc rdi
dec rcx
jnz h2bup2
packnum:
mov bx,0
mov rcx,4
mov rsi,numascii
up1:
rol bx,04
mov al,[rsi]
cmp al,39h
jbe skip1
sub al,07h
skip1:
sub al,30h
add bl,al
inc rsi
loop up1
ret
bcdtohex:
print bcd_in,bcd_inlen
accept numascii ,6
print hex_out,hex_out_len
mov rcx,05
mov rsi,numascii
mov rax,0
mov rbx,0Ah
again1:
mov rdx,0
mul rbx
mov dl,[rsi]
sub dl,30h
add rax,rdx
inc rsi
loop again1
mov rbx,rax
call dispnum_32
ret
dispnum_32:
mov rdi,dnumbuff
mov rcx,16
up2:
rol rbx,04
mov dl,bl
and dl,0Fh
add dl,30h
cmp dl,39h
jbe dskip1
add dl,07h
dskip1:
mov [rdi],dl
inc rdi
loop up2
print dnumbuff,16
ret
; END OF CODE