MIPS branch instead of jump

Nadim Hussami

What is the reason for using a branch with a predetermined condition, such as this:

beq $0, $0, TEST

Instead of just using a jump like this?

j TEST

Branches are used to test for conditions, but in this case, the branch has been used with a condition that will always be true. So why use it over a jump? Is it somehow faster?

I came across this answer, but I don't know if it is indeed the correct answer to my question because I don't know anything about relocation yet.

Michael

The J instruction is encoded as follows:

----------------------------
| opcode |   instr_index   |
----------------------------

instr_index is the least significant 28 bits of the target address, shifted right by 2 to give you a 26-bit value. And the address you'll end up jumping to is (PC & 0xF0000000) | (instr_index << 2)

The BEQ variant is encoded as follows:

-----------------------------
| opcode | 0 | 0 |  offset  |
-----------------------------

And the address you'll end up branching to is PC + sign_extend(offset << 2).

(Note, in both cases PC is the address of the instruction immediately after the branch/jump instruction, not the branch/jump instruction itself).

Now, let's say that you have the following code:

main:
j foo
nop
foo:

And that this code is originally intended to be loaded at address 0x00400024. The address of foo will then be 0x0040002c, and the J instruction will be encoded as (2 << 26) | (0x0040002c >> 2) = 0x0810000b.

But then you decide that you want to relocate this code (copy it to some other location in memory and execute it at that location). Let's say that your code is now executing at 0x00410000. In this case we would want the J instruction to jump to 0x00410008, but it will still be jumping to 0x0040002c ((0x00410004 & 0xF0000000) | (0x010000b << 2) = 0x0040002c).

If on the other hand you had used BEQ you would have the instruction word 0x10000001 ((4 << 26) | (4 >> 2)). Even if the code is relocated you will still end up branching to the correct address: 0x00410004 + (1 << 2) = 0x00410008.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Branch instructions and Jump instruction in Mips

From Dev

MIPS jump and branch instructions range

From Dev

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

From Dev

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

From Dev

JUMP instruction in MIPS

From Dev

MIPS Branch Instructions

From Dev

BNE branch in MIPS assembly

From Dev

MIPS Branch Instruction - Getting Branch Location

From Dev

MIPS critical path of a branch instruction

From Dev

MIPS jump instruction and consequence of the absolute value

From Dev

Two sequential branch instructions in MIPS assembly?

From Dev

Two sequential branch instructions in MIPS assembly?

From Dev

MIPS what hex word encodes branch instruction?

From Dev

MIPS Assembly Return to Call in a branch statement

From Dev

MIPS/UP16 Branch instructions

From Dev

Close Branch instead of merging?

From Dev

Why are branch instructions faster than jump instructions?

From Dev

git branch from branch instead of from master

From Dev

Emit only long jump instructions instead of short jump instructions for LLVM

From Dev

MIPS assembler complains about "Branch out of range" with PIC

From Dev

Copy twice causes branch inside a branch instead of error

From Dev

GIT: Branched off feature branch instead of development branch

From Dev

MIPS: Target of jump differs in high-order 4 bits from instruction pc 0x400014

From Dev

BitBucket - Add to develop branch directly instead of master

From Dev

Git - Accidentally pushed to upstream instead of my branch

From Dev

Git - Accidentally pushed to upstream instead of my branch

From Dev

BitBucket - Add to develop branch directly instead of master

From Dev

Python conditional expression with one branch instead of two

From Dev

views constrained to banner view jump suddenly instead of slowly animating

Related Related

  1. 1

    Branch instructions and Jump instruction in Mips

  2. 2

    MIPS jump and branch instructions range

  3. 3

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

  4. 4

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

  5. 5

    JUMP instruction in MIPS

  6. 6

    MIPS Branch Instructions

  7. 7

    BNE branch in MIPS assembly

  8. 8

    MIPS Branch Instruction - Getting Branch Location

  9. 9

    MIPS critical path of a branch instruction

  10. 10

    MIPS jump instruction and consequence of the absolute value

  11. 11

    Two sequential branch instructions in MIPS assembly?

  12. 12

    Two sequential branch instructions in MIPS assembly?

  13. 13

    MIPS what hex word encodes branch instruction?

  14. 14

    MIPS Assembly Return to Call in a branch statement

  15. 15

    MIPS/UP16 Branch instructions

  16. 16

    Close Branch instead of merging?

  17. 17

    Why are branch instructions faster than jump instructions?

  18. 18

    git branch from branch instead of from master

  19. 19

    Emit only long jump instructions instead of short jump instructions for LLVM

  20. 20

    MIPS assembler complains about "Branch out of range" with PIC

  21. 21

    Copy twice causes branch inside a branch instead of error

  22. 22

    GIT: Branched off feature branch instead of development branch

  23. 23

    MIPS: Target of jump differs in high-order 4 bits from instruction pc 0x400014

  24. 24

    BitBucket - Add to develop branch directly instead of master

  25. 25

    Git - Accidentally pushed to upstream instead of my branch

  26. 26

    Git - Accidentally pushed to upstream instead of my branch

  27. 27

    BitBucket - Add to develop branch directly instead of master

  28. 28

    Python conditional expression with one branch instead of two

  29. 29

    views constrained to banner view jump suddenly instead of slowly animating

HotTag

Archive