Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xorl %eax - Instruction set architecture in IA-32

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?

like image 588
Hélder Moreira Avatar asked May 10 '14 00:05

Hélder Moreira


People also ask

What is IA-32 instruction set?

IA-32 (short for "Intel Architecture, 32-bit", sometimes also called i386) is the 32-bit version of the x86 instruction set architecture, designed by Intel and first implemented in the 80386 microprocessor in 1985.

What is XORL?

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.

Which is an Instruction Set Architecture?

An Instruction Set Architecture (ISA) is part of the abstract model of a computer that defines how the CPU is controlled by the software. The ISA acts as an interface between the hardware and the software, specifying both what the processor is capable of doing as well as how it gets done.

What are the three types of Instruction Set Architecture?

There are three main types of instruction sets: Stack. Accumulator. General-purpose register.


2 Answers

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.

like image 192
cnicutar Avatar answered Nov 04 '22 07:11

cnicutar


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.

like image 28
ccKep Avatar answered Nov 04 '22 05:11

ccKep