Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(meaningful) cost of the jump instruction?

Tags:

x86

assembly

I'm sorry... (premature) optimization is the root of all evil, but I'd like to know: how much more costly is to have a jmp instruction vs. not have it (branchless code)? I'm after the methodology for knowing how to calculate these things. This is purely exploratory, not practical, I'm trying to find my way into a theoretical problem and my testing code brought this up. Thanks.

like image 638
Dervin Thunk Avatar asked Feb 26 '11 15:02

Dervin Thunk


People also ask

What is the size of a jump instruction?

The 26-bit target address field is transformed into a 32-bit address. This is done at run-time, as the jump instruction is executed. Instructions always start on an address that is a multiple of four (they are word-aligned). So the low order two bits of a 32-bit instruction address are always "00".

How does jump instruction work MIPS?

Jump Instruction The jump instructions load a new value into the PC register, which stores the value of the instruction being executed. This causes the next instruction read from memory to be retrieved from a new location.

What is the example of unconditional jump?

The JMP instruction transfers control unconditionally to another instruction. JMP corresponds to goto statements in high-level languages. Unconditional jumps skip over code that should not be executed, for example, ; Handle one case label1: . . .


2 Answers

You'll have to test it on your architecture if you really want to know.

But in general, on modern processors, there is minimal cost for an unconditional jump. It's basically pretty much free apart from a very small amount of instruction cache overhead. It will probably get executed in parallel with neighbouring instructions so might not even cost you a clock cycle. This is because the jump can be executed by one of several parallel execution units.

Look at it this way - a single read of main memory is probably 100-200 times more expensive.

It's a subset of branch prediction more generally, but there no risk of a branch misprediction so you are safe from having to flush the instruction pipeline, which is the main cost associated with conditional jumps.

like image 78
mikera Avatar answered Sep 19 '22 15:09

mikera


Well, it is better not to jump. In the early days conditional jumps were written so that the most probable condition wouldn't cause a jump. So not needing any instruction at all is obviously faster, but I can't tell you how costly it is compared to other instructions. Maybe you should measure a billion jumps one way or the other...

like image 39
GolezTrol Avatar answered Sep 21 '22 15:09

GolezTrol