uni

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

lab5_ex2.asm (1135B)


      1 .eqv SYS_PRINT_STRING   4
      2 .eqv SYS_READ_STRING    8
      3 .eqv SYS_EXIT           10
      4 .eqv SYS_PRINT_CHAR     11
      5 
      6 .data
      7         str:    .space 17 # + 2 bytes to include '\n' and '\0'
      8         revstr: .asciiz "reverse: "
      9 
     10 .text
     11 .globl main
     12 
     13 main:
     14         # read input string
     15         li      $v0, SYS_READ_STRING
     16         la      $a0, str
     17         li      $a1, 17
     18         syscall
     19         
     20         # init strlen counter
     21         li      $t0, 0
     22         
     23 strlen:
     24         lb      $t1, str($t0)
     25         beqz    $t1, strrev
     26         addi    $t0, $t0, 1
     27         j       strlen
     28         
     29         # subtract 2 so that we skip '\n' and '\0' and get to the
     30         # last character in the string
     31         addi    $t0, $t0, -2
     32 
     33 strrev:
     34         # if $t0 is 0 is means we reached str[0]
     35         beqz    $t0, exit
     36         # in the first loop, $t1 is at the last actual character,
     37         # so we'll go backwards to get to the beginning
     38         li      $v0, SYS_PRINT_CHAR
     39         lb      $a0, str($t0)
     40         syscall
     41         addi    $t0, $t0, -1
     42         j       strrev
     43                 
     44 exit:                           
     45         li      $v0, SYS_EXIT
     46         syscall