How could this assembly be possible?

int sarl_n(int x, char n){
   x <<= 2;
   x >>= n;
   return x;

}

When I get together with "gcc -m32 -S sarl_n.c", it emits this code:

.cfi_startproc
movl    4(%esp), %eax
movsbl  8(%esp), %ecx
sall    $2, %eax
sarl    %cl, %eax    #This is the part which I don't understand
ret
.cfi_endproc

Why does gcc use a "mini-register" %clinstead of a large one %ecx?

EDIT: I used the O2 option to get a shorter build code

+4
source share
1 answer

The reason the next line is (previous version)

sarl    %cl, 8(%ebp)    #This is the part which I don't understand

or (current version)

sarl    %cl, %eax       #This is the part which I don't understand

uses %cl, and not %ecx- this is that the SARopcode only supports case %clas input for variable arithmetic shift (at %cltimes).

Look here and I quote:

SAR r/m32, CL     MC   Valid   Valid   Signed divide* r/m32 by 2, CL times.
+11

Source: https://habr.com/ru/post/1659992/


All Articles