Is there anywhere I can find side-by-side examples of dead simple C and x86 programs? The examples I've found so far on the Internet seem to jump straight from "here's Hello World in x86" to "write your own operating system!" I'm having trouble internalizing what has to happen when you do things like call a function.
-
do you mean x86 assembly language? or is there a language called x86? – jcomeau_ictx Apr 23 '11 at 07:52
5 Answers
I would recommend a look at GCC's intermediate assembly output, for example call
gcc -S a.c
then look at a.s
Most of the time, smaller and easier to understand assembly is generated by optimizing, so you would rather use
gcc -O -S a.c

- 14,786
- 7
- 57
- 75
If you mean x86 assembly language, use objdump --disassemble myprog
(on any GNU system) to show the assembly language generated by your C program. If your system doesn't have objdump, you can use ndisasm.

- 37,688
- 6
- 92
- 107
Assuming you mean x86 assembler then with gcc you can use gcc -S yourhelloworldprogram.c
to get assembler output. For Visual Studio you can get assembler output by following this: How do I get the assembler output from a C file in VS2005
I reccommend ddd. You can have the both C sources (if you built with debug symbols) and the machine code showing. You can also step over the code interactively probing register and memory values. A great learning tool.

- 3,425
- 2
- 29
- 48
On gcc you can use the -save-temps -fverbose-asm
options which is better than the -S
option because it still generates the object file and you get also the preprocessor file. The verbose-asm is also important because it adds comments to the assembly output that make the link between the function and variable names of your program and the generated assembly code. Especially when generating with optimization it often is difficult to make the link between the source C and the assembly.

- 11,394
- 1
- 43
- 48