3

I have an exam coming up, and I am completely stuck on this question (see below); even looking at the model answer did not help. I've tried reading our main text for the topic, but still have no idea how to do this. If anyone could provide a step-by-step walk through of the question, I would be very grateful.

"Assuming the first instruction of the MIPS snippet below is located at memory address 0x10001000. What is the value for else and exit in bne and j instruction?"

1   0x10001000:   addi $s0, $0, 5
2   0x10001004:   sub $s2, $0, $s1
3   0x10001008:   beq $s0, $s2, else
4   0x1000100C:   add $s0, $0, $0
5   0x10001010:   add $t0, $s2, $s0
6   0x10001014:   j exit
7   0x10001018:   else: addi $s1, $s0, -1
8   0x1000101C:   exit:

Model Answer:

Else: 0000000000000011 Exit: 00000000000000010000000111

I have included a link to an image of the original question as well. http://i.imgur.com/NgHpZXs.png

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
helencrump
  • 1,351
  • 1
  • 18
  • 27
  • You need to provide the whole context of the question, not just part (f) – gilsho May 18 '14 at 21:39
  • @gilsho The previous parts of the question are unrelated. – helencrump May 18 '14 at 21:57
  • @OliCharlesworth Since I have less than 10 reputation I can't embed images - I've included the code in the actual question now. – helencrump May 18 '14 at 21:58
  • first of all, there is no "bne" instruction in the code you provided, second, it's unclear what you mean by 'value for else', is it the address of the label in memory, is it the value that is returned from the function? – gilsho May 19 '14 at 00:22

1 Answers1

8

First, we'll deal with the branch. Branches are an I-Type instruction, so the branch target is encoded in 16 bits. The easiest way to figure out the immediate field of a branch is to count the number of instructions between the branch instruction and its target. In this case, the else label is 4 instructions after the beq, however, the PC is incremented by 4 during the instruction fetch stage, so the actual immediate field will actually be 3. Of course in binary, this matches up with the sample 0000000000000011.

Next, the jump is a J-Type instruction which encodes the target of the jump using the first 24 bits of its address. However, because the jump target must be word aligned, the last two bits will always be 0 making them unnecessary. Therefore, the jump field of the j instruction matches the sample answer: 0x1000101C >> 2 = 0x4000407 = 00000000000000010000000111

Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28