Reputation: 67
I'm learnin about assembler language right now. The code examples in the book I'm learning from are all compiled on a x86 32bit machine (i think) with gcc. To match my outputs with the one from the book i compiled my simple helloworld.c with "gcc -m32".
#include <stdio.h>
int main(){
int i;
for(i=0;i<10;i++){
printf("Hello World!\n");
}
return 0;
}
The output i get from running "objdump -D a32.out | grep -A20 main.:" looks like this:
0000051d <main>:
51d: 8d 4c 24 04 lea 0x4(%esp),%ecx
521: 83 e4 f0 and $0xfffffff0,%esp
524: ff 71 fc pushl -0x4(%ecx)
527: 55 push %ebp
528: 89 e5 mov %esp,%ebp
52a: 53 push %ebx
52b: 51 push %ecx
52c: 83 ec 10 sub $0x10,%esp
52f: e8 ec fe ff ff call 420 <__x86.get_pc_thunk.bx>
534: 81 c3 cc 1a 00 00 add $0x1acc,%ebx
53a: c7 45 f4 00 00 00 00 movl $0x0,-0xc(%ebp)
541: eb 16 jmp 559 <main+0x3c>
543: 83 ec 0c sub $0xc,%esp
546: 8d 83 f0 e5 ff ff lea -0x1a10(%ebx),%eax
54c: 50 push %eax
54d: e8 5e fe ff ff call 3b0 <puts@plt>
552: 83 c4 10 add $0x10,%esp
555: 83 45 f4 01 addl $0x1,-0xc(%ebp)
559: 83 7d f4 09 cmpl $0x9,-0xc(%ebp)
55d: 7e e4 jle 543 <main+0x26>
But in the book it looks like this:
It misses the first three lines from the output I get and doesn't look anything like it except for the next two lines. Why is that ? And how can i change my compiler/disassembly settings to make it look alike ?
Upvotes: 2
Views: 567
Reputation: 365576
The same version of gcc configured the same way, running with gcc -m32
, should be the same whether it's running on an x86-64 host or an i386 host. Or cross-compiling from an ARM host!
The difference is gcc version and default settings. (@Employed Russian points out that the major one is that -fpie
is on by default in your gcc. Related: 32-bit absolute addresses no longer allowed in x86-64 Linux?) It's kinda bad that PIE is on by default for 32-bit code, too, not just 64, because the extra overhead is much more significant in 32-bit mode (no RIP-relative addressing.)
Besides config stuff like -fno-pie
, different versions of gcc produce different code.
Anyway, if you really want to match what's in the book, try the same version of gcc they used. (Hopefully they mention that? If not, look at the year it was published for a rough hint). You can try every gcc version back as far as 4.4 on http://gcc.godbolt.org/.
Here's a link to your sample code on Godbolt with compiler asm output and disassembly of the binary in two separate output panes, both with optimization disabled.
Note that Matt Godbolt configures gcc without the default-pie
config, but you could use -fpie
to get asm like you get on your desktop.
Note that with un-optimized code, it's more dependent on gcc's internals than if you'd enabled optimization. i.e. -O0
kind of gives you a peek under the hood of how gcc "thinks about" your program, because it doesn't even try to optimize. (Plus it spills/reloads between every statement to support changing variables with a debugger; this is part of why it's so slow: it's purposely strapping a ball and chain to your code. All this extra storing / reloading makes it harder to read; I usually look at -O2
or -O3
output. See also How to remove "noise" from GCC/clang assembly output?)
Upvotes: 3
Reputation: 213897
Your compiler is configured to produce position-independent binary by default, and that causes significant divergence from the book. (This is a fairly recent GCC development).
You will get much closer to the book if you disable this:
gcc -fno-pie -no-pie -m32 hello.c
As others have stated, there is no guarantee that you will get identical code to what's in the book, unless you get exact same compiler built in exactly the same configuration.
Upvotes: 4