Open branch.asm in MARS. Assemble the program. Click Settings and make sure “Show Labels window” is checked. You should see a Labels window appear with the A, B, C labels and the addresses they represent. Notice that the Basic column shows the labels in the beq(line 1) and bne (line 9) instructions translated into immediates. Explain the how the immediates were calculated for each of these two instructions
branch.asm:
A: beq $t0, $t1, B
addiu $t0, $t0, -1
addiu $t0, $t0, -2
B: or $t3, $t1, $t0
C: addi $t1, $t0, 7
add $t1, $t0, $t1
ori $t4, $t1, 1
bne $t4, $zero, C
Hope this will help you. If you have any doubt please let me know.
Please go through all the details.
I know it is a bit complicated, if you find any difficulty in this please let me know
----------------------------------------------------------------------------------------------------
Bne and beq instructions are i type of instructions. In which 16 bit address is used as immediate value (sometime also referred as offset value)
Offset simply represents how far it will have to go to execute a next instruction (from the current pc value).
Each instruction is 32 bit in length means multiple of 4 bytes.
Now
Take a line no 1.
A: beq $t0, $t1, B
In which we have to calculate immediate value for B (an offset for B)
This instruction is located at 0x00400000
Formula for this=current address+4=0x00400000+4 =0x00400004 (Which is value of PC, when this current instruction is in execution, because pc holds the value of next instruction to be executed)
Over here immediate represent how far it will have to execute a next instruction from current value of PC.
Now target is located at address 0x0040000C
Take a difference between those address
Difference=0x0040000C-0x00400004=0x00000008
Now to find an immediate value divide this difference by 4 because each word length is 4 bytes.
Hence 0x00000008/4=0x00000002 which is an immediate value (in simple language how many instructions between PC value and target address)
(Please verify with the screenshot)
This immediate field is generally 16 bit in length but to find out address it is converted into 32 bit with sign bit extended hence in basic it has 32 bit address value.
Now
bne $t4, $zero, C
In which we have to calculate immediate value for C (an offset for C)
This instruction is located at 0x0040001C
Formula for this=current address+4=0x0040001C+4 =0x00400020 (Which is value of PC, when this current instruction is in execution, because pc holds the value of next instruction to be executed)
Target address is 0x00400010
Take a difference between those address
Difference=0x00400010-0x00400020= 0x FFFFFFF0
Now to find an immediate value divide this difference by 4 because each word length is 4 bytes.
0x FFFFFFF0/4= 0xFFFFFFFC
Hence immediate value is 0xfffffffc (please verify with screenshot)
Get Answers For Free
Most questions answered within 1 hours.