7

I am experiencing some difficulties interpreting this exercise;

What does exactly xorl does in this assembly snippet?

C Code:

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

Assembly

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

What's happening on the assembly part?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Hélder Moreira
  • 181
  • 1
  • 1
  • 9
  • Are you sure you posted the 2nd-last line in your asm correctly? (Does the `,` probably belong to the line above `.L2:` ?) – ccKep May 10 '14 at 00:41
  • I am sorry, you are correct, there is an error. The ',' doesn't belong either bellow or above, removed it. – Hélder Moreira May 10 '14 at 00:52
  • 1
    In that case the asm doesn't match the provided C code since there is atleast 1 jump missing (between the line with the 3rd blank and the one with `.L2:`). Or are you allowed to type something like `add 2,` into a single blank? – ccKep May 10 '14 at 00:54
  • pede ajuda ao proença ;) – magamig Apr 12 '16 at 15:02

3 Answers3

18

It's probably:

xorl %ebx, %ebx

This is a common idiom for zeroing a register on x86. This would correspond with i = 0 in the C code.


If you are curious "but why ?" the short answer is that the xor instruction is fewer bytes than mov $0, %ebx. The long answer includes other subtle reasons.

I am leaving out the rest of the exercise since there's nothing idiosyncratic left.

Community
  • 1
  • 1
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Spectacular answer :) – Hélder Moreira May 10 '14 at 00:35
  • Do you mean encoding is shorter, or that instruction itself takes less time to execute? – jackgerrits Jan 22 '15 at 05:06
  • @Jackles: The encoding is shorter, which means fewer I-cache misses in the big picture, but [there are other speed benefits too, for many CPUs](http://stackoverflow.com/questions/33666617/which-is-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and) – Peter Cordes Apr 12 '16 at 20:22
2

This is the completed and commented assembly equivalent to your C code:

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:

So, these are the (arithmetic) instructions that get executed for all numbers >=55:

addl $2, %ebx
decl %ebx

So for numbers >=55, this is equal to incrementing. The following (arithmetic) instructions get executed for numbers <55:

decl %ebx

We jump over the addl $2, %ebx instruction, so for numbers <55 this is equal to decrementing.

In case you're not allowed to type addl $2, (since it's not just the instruction but also an argument) into a single blank there's probably an error in the asm code you've been given (missing a jump between line 4 and 5 to .L3).


Also note that jel is clearly a typo for jle in the question.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
ccKep
  • 5,786
  • 19
  • 31
-1

XORL is used to initialize a register to Zero, mostly used for the counter. The code from ccKep is correct, only that he incremented by a wrong value ie. 2 instead of 1. The correct version is therefore:

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:
Robert Mutua
  • 67
  • 1
  • 5
  • ccKep's code works: It always runs the `dec`, so it needs to `add $2` before falling through to the `dec` for a net change of +1. Read the comment on the `dec` instruction. (And yes, this is probably faster than using a `jmp` to skip the `dec`. The code in the question had a `.L3` where you have `.DONE`, but no slot to put a `jmp`). – Peter Cordes Jul 27 '17 at 04:49
  • The 2nd last paragraph of ccKep's answer even commented on the lack of a space for a `jmp` instruction, and that a `jmp .L3` would allow `inc` instead of `add $2`. Going to have to downvote this for not adding anything useful to the question that isn't already covered by the other answers. – Peter Cordes Jul 27 '17 at 04:52