Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- section .data
- ano db 10, "Assignment No.:4", 10
- db 10, "-------------------------------------------", 10
- db 10, "Conversion of Hex to BCD and BCD to Hex", 10
- db 10, "-------------------------------------------", 10
- ano_len equ $- ano
- menu db 10, "1) HEX to BCD", 10
- db 10, "2) BCD to HEX", 10
- db 10, "3) Exit", 10
- db 10, "Enter your choice:"
- menu_len equ $- menu
- hmsg db 10, "Enter 4 digit Hex number:", 10
- hmsg_len equ $- hmsg
- ebmsg db 10, "The equivalent BCD number is:", 10
- ebmsg_len equ $- ebmsg
- bmsg db 10, "Enter 5 digit BCD number:", 10
- bmsg_len equ $- bmsg
- ehmsg db 10, "The equivalent Hex number is:", 10
- ehmsg_len equ $- ehmsg
- emsg db 10, "Invalid Input!", 10
- emsg_len equ $- emsg
- %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 .bss
- buf resB 6
- char_ans resB 4
- ans resB 12
- ;---------------------------------------------------------------
- section .text
- global _start
- _start:
- Print ano, ano_len
- MENU:
- Print menu, menu_len
- Read buf, 2 ;accept choice i.e. 1 digit + enter
- mov al, [buf] ;contains only digits character
- c1:
- cmp al, '1'
- jne c2
- Print hmsg, hmsg_len
- Print ebmsg, ebmsg_len
- jmp MENU
- c2:
- cmp al, '2'
- jne c3
- Print bmsg, bmsg_len
- Print ehmsg, ehmsg_len
- jmp MENU
- c3:
- cmp al, '3'
- jne invalid
- Exit
- invalid:
- Print emsg, emsg_len
- jmp MENU
- Exit
- ;-------------------------------------------------------
- BCD_HEX:
- Print bmsg, bmsg_len
- Read buf, 6 ;5 digit + 1 Enter
- mov rsi, buf ; points start of buffer
- xor ax, ax ; previous digit = 0
- mov rbp, 5 ; counter
- mov rbx, 10 ; multiplier
- next:
- xor cx, cx ; contains next digit each time
- mul bx ; (ax*bx) + cl
- mov cl, [rsi]
- sub cl, 30h
- add ax, cx
- inc rsi ; point at the next digit
- dec rbp
- jnz next
- mov [ans], ax ; store ax in ans because ax get change in Print Macro
- Print ehmsg, ehmsg_len
- mov ax, [ans]
- call Disp_16 ; Print hex number
- RET
- ;-------------------------------------------------------
- Disp_16:
- MOV RSI, char_ans + 3
- MOV RCX, 4
- MOV RBX, 10
- next_digit:
- mov rdx, 0
- div rbx
- cmp dl, 9
- jbe add30
- add dl, 07h
- add30:
- add dl, 30h
- mov [rsi], dl
- dec rsi
- dec rcx
- jnz next
- Print char_ans, 4
- RET
- ;--------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement