0

I had a look at the top answer here What is the difference between unconditional branch and unconditional jump (instructions in MIPS)?

It says that branches allow for conditions, and so the format of the instruction is different to a jump which is unconditional.

However, I've seen jumps such as jl and je, which use cmp before to set the conditional flags, and so it seems to me that those jumps would behave the same as a branch, which would lessen the difference of a branch to a jump. So could I consider for instructions such as jl and je as a type of branch instruction in some regard? I fail to see the difference except from the format, but it doesn't seem to be significant if we are considering execution.

I am asking specifically because it has been stated that I need to use a branch, however, every algorithm I see in godbolt seems to mostly consist of jumps, so it seems impossible to get a branch without coding it myself, which isn't something that we have done in this module so I don't have the ability to do so.

Papbad
  • 163
  • 2
  • 13
  • 4
    the linked answer makes it sound like a hard and fast definition but it is not. jump and branch mean the same thing. Some folks use one term, some folks use another, and some folks use both. It just means a change in the direction of the execution. conditional or unconditional. – old_timer Nov 14 '19 at 13:36
  • now where you have a specific instruction set which has specifically named instructions, ones that have both types, then WITHIN that instruction set when comparing those specific instructions, then yes you can have a hard and fast definition. But in general, its like soda vs pop, or creek vs stream, lorry vs truck, etc. – old_timer Nov 14 '19 at 13:37
  • 2
    Logically, a branch is conditional because the execution path splits into multiple options, the branches. A jump just goes to one place. Also note you can easily simulate a jump with a branch with a known true condition if necessary for some silly academic problem. PS: funnily enough, on x86 the branching instructions are called _"Jcc Jump if Condition Is Met"_ – Jester Nov 14 '19 at 13:38
  • Generally yes; you don't always need to differentiate between conditional and unconditional branches. Branch prediction is still needed for unconditional branches (aka jumps), even *direct* (not indirect) branches, because they're not decoded in the same cycle they're fetched. See https://stackoverflow.com/questions/38811901/slow-jmp-instruction – Peter Cordes Nov 14 '19 at 15:42

1 Answers1

6

There's a difference between what "branch" and "jump" mean generally and how a specific CPU architecture might use these words in their instruction mnemonics. The question you linked is specific to the MIPS CPU architecture, and so the answers only address how "branch" and "jump" are used in the names of MIPS instructions. Other CPU architectures can use these words differently when naming there instructions.

The distinction between "branch" and "jump"

Often the terms "branch" and "jump" are used interchangeably, but when they're used distinctively generally a branch instruction is an instruction that changes the program counter (PC) depending on whether the result of some test is true or not. The PC is either set to the destination address specified by the instruction or is left unchanged and the following instruction is executed as normal. A jump instruction simply always sets the PC to the destination. In other words, branches are conditional, jumps are unconditional.

This distinction reflects the ordinary English use of the words, where a "branch in the road" implies a choice of two different alternatives, while a jump doesn't imply a choice.

CPU specific instruction naming conventions

However, in a lot of contexts you see "branch" or "jump" used without distinction, or even given the opposite meaning, reflecting how the specific CPU architecture under discussion names its instructions. Not all CPU instructions sets name their instructions following the definition given above. The MIPS instruction is good example one that does, so that's why the answers in the question you linked use the same definition. MIPS branch instructions, all starting with the letter B, are conditional, while MIPS jump instructions, all starting with the letter J, are unconditional.

However there are no ARM instructions with the word "jump" in their name. Both conditional branch and unconditional jump instructions are named branches and start with the letter B. In fact the plain "branch" instruction is just called B in both the unconditional and conditional forms. Suffixes are added to make the B instruction conditional instead of unconditional, but these same suffixes can be added to almost any instruction to make it conditional.

The ARM instruction set has a very big exception to this, however. Confusingly, because ARM treats the PC register like a general purpose register almost any instruction can set the PC, meaning ARM effectively has a large number of branch/jump instructions without "jump" or "branch" in their names (eg. mov pc, r4 addeq pc, #1234, etc.). Note that modern ARM implementations restrict how the PC register can be used in a well defined manner outside a small set of cases where it makes sense.

The x86 instruction set is just the opposite, there are no instructions with "branch" in their name. Both conditional branches and unconditional jump instructions are named jumps and start with the letter J. The exception here are the instructions used to implement subroutine calls, they use "call" or "return" in their names depending on their function rather than jump.

Other CPU architectures may follow completely different naming conventions, like having "branch" instructions use relative offsets and "jump" instructions use absolute addresses for the destination.

Only context can disambiguate

In a lot of cases which word people use will reflect how the CPU instructions they're using are named. An ARM programmer will often exclusively talk about branches, while an x86 programmer will describe the equivalent instructions as jumps. Knowing the context these words are being used in is often necessary to disambiguate them. For example, a reference to a branch in x86 code is probably talking about a conditional branch using one of the Jcc instructions, and not a JMP or CALL instruction. A similar reference in ARM code could refer to any of the ARM "branch" instructions (B, BL, BX, BLX, BXJ) whether they're conditional or not, or whether they're used to implement subroutine calls.

So if someone is talking about branches and jumps, making a distinction between them, then they probably mean conditional branches and unconditional jumps. In other cases the two worlds might be used interchangeably without intending to make a distinction between them. You'll have consider the context to figure out the intended meaning. Of course, in a lot of cases the conditional/unconditional distinction won't matter anyways.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112