BEQ (short for "Branch if EQual") is the mnemonic for a machine language instruction which branches, or "jumps", to the address specified if, and only if the zero flag is set.
BEQ Instruction. The BEQ instruction branches the PC if the first source register's contents and the second source. register's contents are equal. It's syntax is: BEQ $first source register's address, $second source register's address, branch value.
The RISC-V instruction set has six conditional branch instructions, each of which take two source registers and a label indicating where to go. beq (branch if equal) branches when the values in the two source registers are equal.
Calculating the offset: The beq command is at address 1004, but by the time the command is being executed the PC has already been incremented to 1008. So, we calculate the offset from the PC to the desired label (which is at address 1028) as (1028 - 1008) = 20.
I have copied a picture of an assignment I have on MIPS assembly.
I understand (I think) what happens in the code until the line:
beq $11, $0, 3
I understand that the code now makes a PC-RELATIVE branch for the address:
PC+4+3*4
But I don't understand how it comes to happen on this code right here - what is the branch target address? (MARS is simulating a simplified MIPS without branch-delay slots, so that's the next instruction to be executed.)
Row 1: adds 15 to zero, puts it in $a0 register.
Row 2: ANDs $a0 register with 3, puts the result in $a0.
Row 3: ORs $a0 register with 22, puts the result in $a0.
Row 4: shifts $a0 to the left by 5 bits. Result in $a0.
Row 5: if $a0 equals $a0, go to PC+4+6*24 address. The address is Row 7 which is:
slt $11, $10, $9
Which puts the value 0 in $t3 register, because $10=$9.
Now I get to ROW 8:
beq $11, $0, 3
What does row 8 do?
Direct link to my image - please click if you can't read properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With