MIPS - How to find the address value of branch and jump instructions

addressing, mips

Solution

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`

Problem

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

Original source