Xorl% eax - IA-32 instruction set architecture

I am having difficulty interpreting this exercise;

What exactly does xorl do in this assembly fragment?

C code:

int i = 0;
if (i>=55)
    i++;
else
    i--;

Assembly

xorl ____ , %ebx
cmpl ____ , %ebx
Jel  .L2
____ %ebx
.L2:
____ %ebx
.L3:

What happens on the assembly part?

+4
source share
3 answers

Probably:

xorl %ebx, %ebx

This is the usual idiom for resetting the register to x86. This will match i = 0in C code.


If you are interested, but why? the short answer is that the command has xorfewer bytes than mov $0, %ebx. The long answer includes other subtle reasons .

I leave the rest of the exercise as there is nothing special left.

+11

, C-:

xorl %ebx , %ebx    ; i = 0
cmpl $54, %ebx
jle  .L2            ; if (i <= 54) jump to .L2, otherwise continue with the next instruction (so if i>54... which equals >=55 like in your C code)
addl $2, %ebx         ; >54 (or: >=55)
.L2:
decl %ebx            ; <=54 (or <55, the else-branch of your if) Note: This code also gets executed if i >= 55, hence why we need +2 above so we only get +1 total
.L3:

, () , >= 55:

addl $2, %ebx
decl %ebx

, >= 55 . () < 55:

decl %ebx

addl $2, %ebx, < 55 .

addl $2, ( , ) , , asm, ( 4 5 .L3).


, jel jle .

+2

XORL is used to initialize the register to zero, mainly used for the counter. The ccKep code is valid only if it has increased by the wrong value, i.e. 2 instead of 1. Therefore, the correct version is:

xorl %ebx , %ebx     # i = 0
cmpl $54, %ebx      # compare the two
jle  .L2           #if (i <= 54) jump to .L2, otherwise continue with   the next instruction (so if i>54... which equals >=55 like in your C code)

incl %ebx         #i++
jmp .DONE        # jump to exit position

.L2:
decl %ebx      # <=54 (or <55

.DONE:
-1
source

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


All Articles