In: Computer Science
Open jump.asm in MARS. Assemble the program. Notice that in the Basic column, the labels for the two jump instructions are translated into addresses. Explain the following
.a) how the address in the Basic column was calculated for each of the two jump instructions
b) The upper 6 bits of the machine code are 000010 for the jump instructions (opcode=2). But explain why the lower 26 bits of the machine code looks the way it does for each of the two jump instructions. Use jump.asm code below in MARS45.
jump.asm:
j C
A: addiu $t0, $t0, -1
addiu $t0, $t0, -2
B: or $t3, $t1, $t0
C: addi $t1, $t0, 7
j A
addi $t1, $t9, 9
Greetings!!
Machine code for J C instruction is 08100004 and
Machine code for J A instruction is 08100001 as shown below.
Instruction format of J instruction is:
Opcode (6 bits) | Target Address(26 bits) |
Machine code generation:
Actual Target address is shifted right by 2 bits and encoded into the Target Address part of the machine code. This 2 bit right shift is done because the address calculation unit shift the bits 2 position left to obtain the word aligned memory address.
Example 1:
J C instruction jumps to the label C which has an address 0x00400010(0000 0000 0100 0000 0000 0000 0001 0000). This address is right shifted by 2 bits gives:00 0000 0001 0000 0000 0000 0000 0100 and the lower 26 bits (with hex equivalent of 0x0100004) is encoded into the Target Address part of the machine code.
ie 0000 1000 0001 0000 0000 0000 0000 0100 - gives 0x08100004
Example 2:
J A instruction jumps to the label A which has an address 0x00400004(0000 0000 0100 0000 0000 0000 0000 0100). This address is right shifted by 2 bits gives:00 0000 0001 0000 0000 0000 0000 0001 and the lower 26 bits (with hex equivalent of 0x0100001) is encoded into the Target Address part of the machine code.
ie 0000 1000 0001 0000 0000 0000 0000 0001 - gives 0x08100001
Address calculation at the time of execution:
Example 1:
Lower 26 bits of the machine code is shifted 2 bits left(ie 0000 0100 0000 0000 0000 0001 0000) and append 4 bits of the MSB from PC(ie 0000).
ie 0000 0000 0100 0000 0000 0000 0001 0000 = 0x00400010 is the target address of label C.
Example 2:
Lower 26 bits of the machine code is shifted 2 bits left(ie 0000 0100 0000 0000 0000 0000 0100) and append 4 bits of the MSB from PC(ie 0000).
ie 0000 0000 0100 0000 0000 0000 0000 0100 = 0x00400004 is the target address of label A
Hope this helps