Advertisement
Eternoseeker

BCD HEX ass 4

Apr 20th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASM (NASM) 3.04 KB | Source Code | 0 0
  1. section .data
  2. ano     db 10, "Assignment No.:4",                              10
  3.         db 10, "-------------------------------------------",   10
  4.         db 10, "Conversion of Hex to BCD and BCD to Hex",       10
  5.         db 10, "-------------------------------------------",   10
  6.  
  7. ano_len    equ   $- ano
  8.  
  9. menu    db 10, "1) HEX to BCD", 10
  10.         db 10, "2) BCD to HEX", 10
  11.         db 10, "3) Exit",       10
  12.         db 10, "Enter your choice:"
  13.  
  14. menu_len equ  $- menu
  15.  
  16. hmsg    db 10, "Enter 4 digit Hex number:", 10
  17. hmsg_len   equ  $- hmsg
  18.  
  19. ebmsg   db 10, "The equivalent BCD number is:", 10
  20. ebmsg_len  equ  $- ebmsg
  21.  
  22. bmsg    db 10, "Enter 5 digit BCD number:", 10
  23. bmsg_len   equ  $- bmsg
  24.  
  25. ehmsg   db 10, "The equivalent Hex number is:", 10
  26. ehmsg_len  equ  $- ehmsg
  27.  
  28. emsg   db 10, "Invalid Input!", 10
  29. emsg_len   equ  $- emsg
  30.  
  31. %macro Print 2
  32.     mov rax, 1
  33.     mov rdi, 1
  34.     mov rsi, %1
  35.     mov rdx, %2
  36.     syscall
  37. %endmacro
  38.  
  39. %macro Read 2
  40.     mov rax, 0
  41.     mov rdi, 0
  42.     mov rsi, %1
  43.     mov rdx, %2
  44.     syscall
  45. %endmacro
  46.  
  47. %macro Exit 0
  48.     mov rax, 60
  49.     mov rdi, 0
  50.     syscall
  51. %endmacro
  52. ;---------------------------------------------------------------
  53. section .bss
  54.     buf resB 6
  55.     char_ans resB 4
  56.     ans resB 12
  57. ;---------------------------------------------------------------
  58. section .text
  59.     global _start
  60. _start:
  61.    
  62.     Print ano, ano_len
  63. MENU:
  64.     Print   menu, menu_len
  65.     Read    buf, 2                  ;accept choice i.e. 1 digit + enter
  66.  
  67.     mov     al, [buf]               ;contains only digits character
  68. c1:
  69.     cmp al, '1'
  70.     jne c2
  71.     Print hmsg, hmsg_len
  72.     Print ebmsg, ebmsg_len
  73.     jmp MENU
  74. c2:
  75.     cmp al, '2'
  76.     jne c3
  77.     Print bmsg, bmsg_len
  78.     Print ehmsg, ehmsg_len
  79.     jmp MENU
  80. c3:
  81.     cmp al, '3'
  82.     jne invalid
  83.     Exit
  84.  
  85. invalid:
  86.     Print emsg, emsg_len
  87.     jmp MENU
  88.  
  89. Exit
  90. ;-------------------------------------------------------
  91. BCD_HEX:
  92.     Print bmsg, bmsg_len
  93.     Read buf, 6                 ;5 digit + 1 Enter
  94.  
  95.     mov rsi, buf                ; points start of buffer
  96.     xor ax, ax                  ; previous digit = 0
  97.     mov rbp, 5                  ; counter
  98.     mov rbx, 10                 ; multiplier
  99.  
  100. next:
  101.     xor cx, cx                  ; contains next digit each time
  102.     mul bx                      ; (ax*bx) + cl
  103.     mov cl, [rsi]
  104.     sub cl, 30h
  105.     add ax, cx
  106.  
  107.     inc rsi                     ; point at the next digit
  108.     dec rbp
  109.     jnz next
  110.  
  111.     mov [ans], ax               ; store ax in ans because ax get change in Print Macro
  112.     Print ehmsg, ehmsg_len
  113.  
  114.     mov ax, [ans]
  115.     call Disp_16                ; Print hex number
  116.  
  117. RET
  118. ;-------------------------------------------------------
  119. Disp_16:
  120.     MOV RSI, char_ans + 3
  121.     MOV RCX, 4
  122.     MOV RBX, 10
  123.  
  124. next_digit:
  125.     mov rdx, 0
  126.     div rbx
  127.     cmp dl, 9
  128.     jbe add30
  129.     add dl, 07h
  130.  
  131. add30:
  132.     add dl, 30h
  133.  
  134.     mov [rsi], dl
  135.     dec rsi
  136.     dec rcx
  137.     jnz next
  138.  
  139. Print char_ans, 4
  140. RET
  141. ;--------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement