Advertisement
Eternoseeker

ass 4 HEX to BCD part2

Jun 5th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 1.39 KB | Source Code | 0 0
  1. ;ALP program to convert HEX to BCD
  2. ;--------------------------------------------------
  3.  
  4. section .data
  5.  
  6. msg1 db "Enter 4 digit HEX number:"
  7. msg1_len equ $-msg1
  8.  
  9. msg2 db "Converted into Decimal number:"
  10. msg2_len equ $-msg2
  11.  
  12. emsg db "Invalid BCD number"
  13. emsg_len equ $-emsg
  14.  
  15. ;-----------------------------------------------------------
  16.  
  17. section .bss
  18.  
  19. buf resb 2
  20. ans resb 2
  21. char_ans resb 2
  22.  
  23. %macro print 2
  24. mov rax,1
  25. mov rdi,1
  26. mov rsi,%1
  27. mov rdx,%2
  28. syscall
  29. %endmacro
  30.  
  31. %macro read 2
  32. mov rax,0
  33. mov rdi,0
  34. mov rsi,%1
  35. mov rdx,%2
  36. syscall
  37. %endmacro
  38.  
  39. %macro exit 0
  40. mov rax,60
  41. mov rdi,0
  42. syscall
  43. %endmacro
  44. ;----------------------------------------------------------
  45.  
  46. section .text
  47. global _start
  48. _start:
  49.  
  50. Call HEX_BCD
  51. exit
  52.  
  53. HEX_BCD:
  54. print msg1,msg1_len
  55. read buf,5
  56. mov rcx,4
  57. mov rsi,buf
  58. mov bx,0
  59.  
  60. next_byte:
  61. shl bx,4
  62. mov al,[rsi]
  63.  
  64. cmp al,'0'
  65. JB error
  66. cmp al,'9'
  67. JBE sub30
  68.  
  69. cmp al,'A'
  70. JB error
  71. cmp al,'F'
  72. JBE sub37
  73.  
  74. cmp al,'a'
  75. JB error
  76. cmp al,'f'
  77. JBE sub57
  78.  
  79. error:
  80. print emsg,emsg_len
  81. exit
  82.  
  83. sub57 : sub al,20h
  84. sub37 : sub al,07h
  85. sub30 : sub al,30h
  86.  
  87. Add bx,ax
  88.  
  89. INC rsi
  90. DEC rcx
  91. JNZ next_byte
  92.  
  93. print msg2,msg2_len
  94. mov ax,bx
  95. Call Display
  96. ret
  97.  
  98. Display:
  99. mov rbx,10
  100. mov rcx,4
  101. mov rsi,char_ans+3
  102.  
  103. back:
  104. mov rdx,0
  105. div rbx
  106.  
  107. cmp dl,09h
  108. jbe add30
  109. add dl,07h
  110.  
  111. add30:
  112. add dl,30h
  113.  
  114. mov [rsi],dl
  115. dec rsi
  116. dec rcx
  117. jnz back
  118.  
  119. print char_ans,4
  120. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement