开发者

Why number++ uses EAX while number-- uses ECX?

;disas for number++
mov eax, [number]
add eax,1
mov [number],eax
;disas for number--
m开发者_JS百科ov ecx, [number]
sub ecx,1
mov [number],ecx

Why number++ uses EAX while number-- uses ECX ?

What's the convention for dispatching registers ?


Register allocation is up to the compiler. Usually, it'll depend primarily on the surrounding code, not on the operation(s) you carry out.


There is no convention for these operations. Most registers are general purpose, and can be used for common arithmetic operations.

The compiler is just using a register that happens to be free at that point in the code.


Your missing the assembly around those extracts, which is probably the most important part, else you can't tell if other registers where in use or not (or if calling conventions played a role).

With the surrounding pieces its a little easier to tell whether the allocation was partly arbitrary(graph coloring) or a more uniform linear scan etc. The compiler flags will also affect this too, because both your above examples can be don't without the use of registers.


See all of the other answers saying that it is compiler and code dependent, there is no rule.

unsigned int x;

void one ( void )
{
  x++;
}
void two ( void )
{
 x--;
}

with optimizations

one:
    addl    $1, x(%rip)
    ret
two:
    subl    $1, x(%rip)
    ret

same compiler, same code, same day, no optimizations, eax is used for both

one:
    pushq   %rbp
    movq    %rsp, %rbp
    movl    x(%rip), %eax
    addl    $1, %eax
    movl    %eax, x(%rip)
    leave
    ret
two:
    pushq   %rbp
    movq    %rsp, %rbp
    movl    x(%rip), %eax
    subl    $1, %eax
    movl    %eax, x(%rip)
    leave
    ret
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜