Advertisement
Guest User

Untitled

a guest
Sep 6th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     .text
  2. main:
  3.     li  $a0, 0x214D # $a0 = 0, change this to test different values
  4.  
  5.     jal hexasc      # call hexasc
  6.     nop         # delay slot filler (just in case) 
  7.  
  8.     move    $a0, $v0    # $a0 = $v0, copy return value to argument register
  9.  
  10.     li  $v0, 11     # syscall with v0 = 11 will print out
  11.     syscall         # ascii character in a0 to the Run I/O window
  12.    
  13.     li  $v0, 10     # system exit code
  14.     syscall         # terminates execution, no garbage memory will be executed
  15.  
  16. hexasc:
  17.     # $a0 = $a0 & 0xF
  18.     # if ($a0 >= 0 && $a0 <= 9 && $a0 is int)
  19.     #   $v0 = 0x30 + $a0
  20.     # else if ($a0 >= 10 && $a0 <= 15)
  21.     #   $v0 = 0x37 + $a0
  22.     # else $v0 = 0  
  23.    
  24.     addi    $t0, $0, 15         # $t0 = 15 (upper bound)
  25.     addi    $t1, $0, 9          # $t1 = 9 (bound between numbers and letters)
  26.     andi    $a0, $a0, 0xF           # 28 zeros concatinated with the last 4 bits of $a0            
  27.    
  28.     bltz    $a0, out_of_domain      # branch to out_of_domain, if $a0 < 0
  29.     bgt     $a0, $t0, out_of_domain     # branch to out_of_domain, if $a0 > 15
  30.     bgt     $a0, $t1, gt9           # branch to gt9, if $a0 > 9
  31.    
  32.     addi    $v0, $a0, 0x30          # convert $a0 to ascii code for 0-9
  33.     jr  $ra            
  34.  
  35. out_of_domain:
  36.     move $v0, $0    # $v0 = 0 (ascii for NUL)
  37.     jr $ra
  38.  
  39. gt9:
  40.     addi $v0, $a0, 0x37 # convert $a0 to ascii code for A-F
  41.     jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement