# (x86) jumps in assembly

This topic is 2139 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I wanted to optymize some procedure in assembly

(it was something like minmax(a,b,c) small code

to get smallest one and biggest one of the three ints given)

i think the most important way of optymizing codes like

here is to decrease number of jumps, but I wander

what is most costly - are the inconditional jumps costly?

are the conditional jumps fails costly? If i got a jump

'tree; that jumps between 5 conditional ways each one

is 3 conditionals long is thismore costly than having

4 conditonal ways each one 3 ifs long? how to optymize

jumps in x86/x64 asm? what elements do decrease?

##### Share on other sites
The expensive jumps are the conditional jumps that are hard to predict. The CPU uses some heuristics to try to predict if a conditional jump will be taken or not.

But for this particular piece of code you can do everything with conditional moves and no jumps.

##### Share on other sites

The expensive jumps are the conditional jumps that are hard to predict. The CPU uses some heuristics to try to predict if a conditional jump will be taken or not.

But for this particular piece of code you can do everything with conditional moves and no jumps.

where I could find this code, someone would like to show?

afaik gcc generated such way code here

//input ints eax ebx edx

// output eax:  min, ebx:- max

cmp edx, eax

jg L1

swap edx, eax   //here three movs for swap (*)

L1:

cmp ebx, edx

jg  L2

cmp eax, ebx

cmovg eax, ebx

mov ebx, edx

L2::

sorry if i mistake something in rewriting this code (this is taken down from my notes) it has only two conditional jumps so it is quite optymized I think, can it be optymized? (I am notso good in asm)

(*)  mov ecx, edx

mov edx, aex

mov eax, ecx

Edited by fir

##### Share on other sites

Also note that C++11 even has a ready-to-use std::minmax, which is likely as good as the compiler is able to do it, for any type.

##### Share on other sites

Here's what gcc 4.8.1 has to say to this similar code (which doesn't pass non-const refs and uses the default comparator:

#include <algorithm>
int main(int argc, char**)
{
int a = 5; int b = argc;
auto min_max = std::minmax(a, b);

printf("%d%d", min_max.first, min_max.second);
return 0;
}

(using argc solely so compiler doesn't optimize out the minmax)

Output is not much different from what I'd expect (the complete main function):

mov    $0x5,%eax movl$0x409084,(%esp)
mov    %eax,%edx

• 16
• 11
• 9
• 24
• 50