Advertisement
Guest User

Untitled

a guest
Sep 13th, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Assignment: extend this program to reverse the string "intext"
  2. # and write the result to "outtext".   
  3.  
  4.             .text
  5.             .set noreorder
  6. main:       la    $t1, intext   # t1 points to start of intext
  7.             la    $t2, outtext  # t2 points to start of outtext
  8.        
  9.             li    $t0, 0        # t0 used to count characers
  10. seek_end:   lb    $t3, 0($t1)   # read character
  11.             beq   $t3, 0, seek_end1 # check if 0 (end of string)
  12.             addiu $t0, $t0, 1
  13.             addiu $t1, $t1, 1
  14.             b     seek_end
  15. seek_end1:  addu  $t2, $t2, $t0 # t2 points to end of outtext
  16.             sb    $t3, 0($t2)   # write 0 to terminate string
  17.             la    $t1, intext   # t1 points to start of intext
  18.  
  19.        
  20.             la $a0, intext
  21.         move $a1, $t2
  22.         move $a2, $t0
  23.         addiu $a1, $a1, -1 #Fix off-by-1 (temp fix)
  24.         bal rev # call: rev(a0,a1,a2)
  25.  
  26. stop:       b     stop
  27.  
  28.        #argument 0, första tecknet att kopiera
  29.        #argument 1, destinationsadressen för den karaktären
  30.        #argument 2, hur många tecken som ska kopieras
  31.  
  32. rev:    addiu $sp, $sp, -4
  33.     sw    $31, 0($sp) #push return address to stack
  34.     addiu $sp, $sp, -4
  35.     sw    $fp, 0($sp) #push old fp
  36.     move  $fp, $sp  #move fp to sp
  37.     addiu $sp, $sp, -12 #Make room for 3 local variables
  38.            
  39.    
  40.     #Entry Sequence complete
  41.  
  42. save:   sw    $a0, 0($sp)   #We save address of first character as a local variable
  43.     sw    $a1, 4($sp)   #We save the destination address as a local var
  44.     sw    $a2, 8($sp)   #We save the number of characters to copy as a loc var
  45.  
  46.  
  47.     #Our variables are now saved on the stack
  48.  
  49.     #start of reversing function
  50.  
  51. if: bne   $a2, $zero, else  # if num of char not equal to 0, jump to else
  52.                 # otherwise go to then
  53.  
  54. then:   b     exit
  55.  
  56.  
  57. else:   addi  $a0, $a0, 1 # New read-char address
  58.     addi  $a1, $a1, -1 # New destination address
  59.     addi  $a2, $a2, -1 # reduce number of chars to copy
  60.     bal   rev
  61.  
  62.     lw $t3, -12($fp)    # load address of first letter
  63.     lb $t4, 0($t3)      # load the given letter from the address
  64.     lw $t5, -8($fp)
  65.     sb $t4, 0($t5)
  66.  
  67.  
  68. exit:   move $sp, $fp       # destroy local variables
  69.     lw   $fp, 0($sp)    # restore last fp (to move down in the stack)
  70.     addi $sp, $sp, 4    # pop word containing old fp
  71.     lw   $31, 0($sp)    # restore return address
  72.     addi $sp, $sp, 4    # pop word containing return address
  73.     jr   $31        # return
  74.  
  75.  
  76.  
  77.    
  78. #copy:
  79. #       addi $t2, $t2, -1 # Minskar utstring-index
  80. #       lb $t3, 0($t1) # Laddar byte ur instring till temporärt register t3
  81. #       sb $t3, 0($t2) # Sparar det laddade innehållet från t3 till slutet på utstring      
  82. #       addi $t1, $t1, 1 # ökar index på instring
  83. #       addi $t0, $t0, -1 # minskar character count
  84. #       bgtz $t0, copy # branchar om character count > 0
  85.  
  86.  
  87.  
  88.             .data
  89. intext:     .string "!dlroW olleH"
  90.             .align 4
  91. outtext:    .string "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement