问题
As is obvious from my question history today, I am working on a MIPS project that will reverse a string, in this case "Hello, World". I think I have my loop and string reversal functions correct, but I cannot figure out the swap function itself, or how to output the manipulated string.
I know that the basic swap for something like this would be:
sll $t1, $a1, 2
add $t1, $a0,$t1
lb $t0,0($t1)
lb $t2, 4($t1)
sb $t0,0($t1)
sb $t2, 4($t1)
jr $ra
But I have no idea what that does or how I interpret that to fit my code. If some holy saint out there could explain how that works and how syscalls work I would be eternally grateful. My code below:
.data
string: .asciiz "Hello, World!"
.text
main:
li $v0, 4
la $a0, string #store string in a0
syscall
jal stringreverse
stringreverse:
add $t0,$a0,$zero #base address of string
add $a1, $zero,14 #stores size of string in a1
addi $t2, $a1, -1 #j = length -1
add $t3,$zero,$zero #i = 0
loop:
jal swap
addi $t2,$t2,-1 #j--
addi $t3,$t3,+1 #i++
slt $t7,$t3,$t2 #set t7 to 1 when j is less than i
bne $t7,$zero,loop # if i is greater than j, keep looping
j done
swap: #swaps the sooner element with the later and returns back to the loop
lb $v0,0($t3) # updated to fit my registers t3 =i
lb $v1,0($t2) # updated to fit my registers t2 =j
sb $v1,0($t3)
sb $v0,0($t2)
jr $ra
done:
.....procedure here.....
syscall
回答1:
It looks like you're treating the lb (load byte instruction) like a la (load address instruction).
You probably want to do something more like this:
swap:
lb $temp1, str($t2) #load temp1 address with byte at $t2 in str
lb $temp2, str($t1) #load temp2 address with byte at $t1 in str
sb $temp2, str($t2) #store temp2 byte into $t2'th position in str
sb $temp1, str($t1) #store temp1 byte into $t1'th position in str
jr $ra
with some data like:
.data
str:
.asciiz "your string here"
Hopefully this clears things up a bit for you.
来源:https://stackoverflow.com/questions/49396405/mips-swap-function