I'm trying to translate a simple C function (that tells you whether a word, made by a char array, is a palindrome) into MIPS 32 but I'm having trouble using getting to load non-multiple-of-4 positions of the array.
Apparently,
`li t0,0(a0)`
loads the first letter (char), and
`li t0,4(a0)`
loads the fifth letter of the array (I thought it would have been the second one). Trying to load the second, as in:
`li t0,1(a0)`
gets me a segmentation fault. So does using shift left logical before loading 0(a0). How do I solve this?
It's a 32-bit platform and it requires aligned access. So if a0
is aligned to 4 bytes, then li t0,0(a0)
will load the first 32-bit value of a0
into t0
. But li t0,1(a0)
will try (and fail) to load a misaligned 32-bit value.
So don't try to load one character at a time. Embrace the fact that MIPS 32 will load 4 characters (32 bits) at a time. You can access single characters within a word using shift and bitwise-and.
Nevermind, here's how I got it to work:
addu a0,a0,t0 #t0 = i, a0 <- a0+i
lb t2,0(a0) #store array[i] in t2
subu a0,a0,t0 #returns a0 to original value
If I want to get to the second letter, I load 1 in t0, for instance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.