-1

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?

martincito
  • 71
  • 1
  • 5

2 Answers2

0

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.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thanks for answering, can you give me an example on how you would retrieve the second character within the word? – martincito Sep 30 '17 at 17:10
0

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.

martincito
  • 71
  • 1
  • 5