uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

lab6_ex5.asm (1542B)


      1 .eqv SYS_PRINT_STRING   4
      2 .eqv SYS_READ_WORD      5
      3 .eqv SYS_EXIT           10
      4 .eqv SYS_PRINT_CHAR     11
      5 
      6 .data
      7         inputmsg:    .asciiz "Number (1-255): "
      8         bounderrstr: .asciiz "Number must be 1-255\n"
      9         resstr:      .asciiz "Hex: 0x"
     10         hex:         .ascii "0123456789abcdef"
     11         
     12 .text
     13 .globl main
     14 
     15 main:
     16         li      $v0, SYS_PRINT_STRING
     17         la      $a0, inputmsg
     18         syscall
     19         
     20         li      $v0, SYS_READ_WORD
     21         syscall
     22         move    $t0, $v0
     23         
     24         # Bounds check
     25         blt     $t0, 1, bounderr
     26         bgt     $t0, 255, bounderr
     27         
     28         li      $v0, SYS_PRINT_STRING
     29         la      $a0, resstr
     30         syscall
     31         
     32         # Get first 4 bits and shift them 4 bits right
     33         # so that `t2` can have their value. For example
     34         # if the number is 50 (00110010):
     35         #
     36         # (00110010 & 0xf0) >> 4 = 00110000 >> 4 = 00000011 = 3
     37         andi    $t1, $t0, 0xf0
     38         srl     $t2, $t1, 4
     39         
     40         # Print first portion (4 bits)
     41         li      $v0, SYS_PRINT_CHAR
     42         lb      $a0, hex($t2)
     43         syscall
     44         
     45         # Get last 4 bits. Using 50 as an example again
     46         # 00110010 & 0x0f = 00000010 = 2
     47         andi    $t1, $t0, 0x0f
     48         
     49         # Print second portion (4 bits)
     50         lb      $a0, hex($t1)
     51         syscall
     52         
     53         j       exit
     54         
     55 bounderr:
     56         li      $v0, SYS_PRINT_STRING
     57         la      $a0, bounderrstr
     58         syscall
     59 
     60 exit:
     61         li      $v0, SYS_EXIT
     62         syscall