Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Assignment: extend this program to reverse the string "intext"
- # and write the result to "outtext".
- .text
- .set noreorder
- main: la $t1, intext # t1 points to start of intext
- la $t2, outtext # t2 points to start of outtext
- li $t0, 0 # t0 used to count characers
- seek_end: lb $t3, 0($t1) # read character
- beq $t3, 0, seek_end1 # check if 0 (end of string)
- addiu $t0, $t0, 1
- addiu $t1, $t1, 1
- b seek_end
- seek_end1: addu $t2, $t2, $t0 # t2 points to end of outtext
- sb $t3, 0($t2) # write 0 to terminate string
- la $t1, intext # t1 points to start of intext
- la $a0, intext
- move $a1, $t2
- move $a2, $t0
- addiu $a1, $a1, -1 #Fix off-by-1 (temp fix)
- bal rev # call: rev(a0,a1,a2)
- stop: b stop
- #argument 0, första tecknet att kopiera
- #argument 1, destinationsadressen för den karaktären
- #argument 2, hur många tecken som ska kopieras
- rev: addiu $sp, $sp, -4
- sw $31, 0($sp) #push return address to stack
- addiu $sp, $sp, -4
- sw $fp, 0($sp) #push old fp
- move $fp, $sp #move fp to sp
- addiu $sp, $sp, -12 #Make room for 3 local variables
- #Entry Sequence complete
- save: sw $a0, 0($sp) #We save address of first character as a local variable
- sw $a1, 4($sp) #We save the destination address as a local var
- sw $a2, 8($sp) #We save the number of characters to copy as a loc var
- #Our variables are now saved on the stack
- #start of reversing function
- if: bne $a2, $zero, else # if num of char not equal to 0, jump to else
- # otherwise go to then
- then: b exit
- else: addi $a0, $a0, 1 # New read-char address
- addi $a1, $a1, -1 # New destination address
- addi $a2, $a2, -1 # reduce number of chars to copy
- bal rev
- lw $t3, -12($fp) # load address of first letter
- lb $t4, 0($t3) # load the given letter from the address
- lw $t5, -8($fp)
- sb $t4, 0($t5)
- exit: move $sp, $fp # destroy local variables
- lw $fp, 0($sp) # restore last fp (to move down in the stack)
- addi $sp, $sp, 4 # pop word containing old fp
- lw $31, 0($sp) # restore return address
- addi $sp, $sp, 4 # pop word containing return address
- jr $31 # return
- #copy:
- # addi $t2, $t2, -1 # Minskar utstring-index
- # lb $t3, 0($t1) # Laddar byte ur instring till temporärt register t3
- # sb $t3, 0($t2) # Sparar det laddade innehållet från t3 till slutet på utstring
- # addi $t1, $t1, 1 # ökar index på instring
- # addi $t0, $t0, -1 # minskar character count
- # bgtz $t0, copy # branchar om character count > 0
- .data
- intext: .string "!dlroW olleH"
- .align 4
- outtext: .string "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement