0

So, in this code snippet, I am trying to find out the values of $t2, and $t3 in HEX. I get the answer to be $t2 = 0x30 and $t3 to be 0x3C. However, the answer in the back is $t2 = 0x130, $t3 = 0x13C. Can someone explain??

    .data
x:  .byte 1, 2, 3, 4, 5
y:  .word 19, 20, 25, 30, 35

    .text
    addi $t0, $0, 8
    lw $t1, x($t0)
    sll $t2, $t1, 4
    ori $t3, $t2, 12
1
  • we would like to see how you derived at your answers. Commented Dec 22, 2010 at 7:14

1 Answer 1

2

How did you derive your answer? I'm pretty rusty with MIPS and this is based on what I remember from uni.

x points to the byte with data 1. On a little-endian machine, x+5 marks the end of the 4 byte word (with data 19) and x+8 marks the beginning. So 19 (10011) is loaded into $t1, shifted left by 4 to 100110000 (304 or 0x130). Finally, 12 is added to get 0x13C.

Let me know if you don't understand something. And it helps to draw a diagram :)

Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't the line lw $t1, x($t0) make $t1 equal to 3 (i.e 8/4 = 2 and x[2] equals 3). Therefore $t1 is shifted by 4 to give you 110000 or 0x30?
There's no need to divide by 4 - don't forget that MIPS uses byte addressing (i.e. each memory address points to a byte in memory). I made a little diagram here which should help: i41.photobucket.com/albums/e299/Seltzer100/Misc/…
actually this also works on a big-endian system. The assembler aligns "y" on a 32-bit boundary, so the word at address x+8 (from x+8 to x+11) has the numerical value 19. You just need the assembler to use the same convention than the CPU when it executes the lw opcode.
So you are saying that memory is continuous, and it's allowed to go over into y, like that? I thought, x and y are totally separate from each other, and in order to access 19 from y, you have to have something like lw $t1, y($t0). Thanks for the pic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.