The idea of this class is to understand how C++ is translated into assembly (how assembly is translated into object code is left as an exercise for the student).
So first we will talk about how C is translated into assembly, because C++ is mostly a superset of C, and because it a good way to learn techniques for achieving this understanding.
First lets look at what hello world looks like in assembly.
Loading…
Loading…
Loading…
Loading…
Loading…
Test`helloWorld() at main.cpp:12:
0x100000c80: pushq %rbp
0x100000c81: movq %rsp, %rbp
0x100000c84: leaq 0x2b6(%rip), %rdi ; "Hello, World"
0x100000c8b: popq %rbp
0x100000c8c: jmp 0x100000ec0 ; symbol stub for: puts
How do we do that? We run cc -s -o hello-world.s
hello-world.c
Or we set a breakpoint in XCode and check
Debug -> Debug Workflow -> Always Show Assembly. Or we get the
built objects and we run odump on them. If building with xocde
they will be at
/Users/tibbetts/Library/Developer/Xcode/DerivedData/Test-gklyipaixaqhfmaztcrgfnmrvniq/Build/Intermediates/Test.build/Debug/Test.build/Objects-normal/x86_64
We can also see it with source interpolated by running
cc -g -c -Wa,-adhls=hello-world.listing hello-world.c
The output of that will look like hello-world.listing
Our makefile looks like this:
Loading…
Loading…
64 bit | 32 bit | 16 bit | Second 8bit | 8 bit | Usage |
---|---|---|---|---|---|
%rax | %eax | %ax | %ah | %al | Return value |
%rbx | %ebx | %ax | %bh | %bl | Callee saved |
%rcx | %ecx | %cx | %ch | %cl | 4th argument |
%rdx | %edx | %dx | %dh | %dl | 3rd argument |
%rsi | %esi | %si | %sil | 2nd argument | |
%rdi | %edi | %di | %dil | 1st argument | |
%rbp | %ebp | %bp | %bpl | Basis Pointer, Callee saved | |
%rsp | %esp | %sp | %spl | Stack pointer | |
%r8 | %r8d | %r8w | %r8b | 5th argument | |
%r9 | %r9d | %r9w | %r9b | 6th argument | |
%r10 | %r10d | %r10w | %r10b | Callee saved | |
%r11 | %r11d | %r11w | %r11b | Used for linking | |
%r12 | %r12d | %r12w | %r12b | Unused for C | |
%r13 | %r13d | %r13w | %r13b | Callee saved | |
%r14 | %r14d | %r14w | %r14b | Callee saved | |
%r15 | %r15d | %r15w | %r15b | Callee saved |
Operation prefix | Arguments | Suffixes | Description | Examples |
---|---|---|---|---|
mov | Source, Dest | Sign or Zero extend (s/z),Size(q/l/b) | Move | |
push /pop | Source | Size | Push to stack %rsp | |
lea | Address, Dest | Size | Load effective address | |
inc /dec | Dest | Size | Increment/Decrement | |
neg /not | Dest | Size | Negate, Complement | |
add /sub /imul | Accumulator, Argument | Size | Add/Substract/Multiply | |
and /or /xor | Accumulator, Argument | Size | Bitwise And/Or/Xor | |
sal /shl /sar /shr | Argument, Accumulator | Size | Shift Left/Right, Arith/Logical (only different for right) | |
cmp | Arg2, Arg1 | Size | Numerical comparison by substraction | |
test | Arg1, Arg2 | Size | Bitwise AND and set flags | |
jmp | Address | Basic jump | ||
j* | Address | Why to jump. Equal/Not Equal, Greater/Less, Zero, etc | ||
call | Address | Call subroutine | ||
ret | Return from subroutine |
C declaration | Intel data type | GAS suffix x86-64 | Size (Bytes) |
---|---|---|---|
char | Byte | b | 1 |
short | w | 2 | |
int | Double word | l | 4 |
unsigned | Double word | l | 4 |
long int | Quad word | q | 8 |
unsigned long | Quad word | q | 8 |
char * | Quad word | q | 8 |
float | Single precision | s | 4 |
double | Double precision | d | 8 |
long double | Extended precision | t | 16 |
Loading…
Loading…
Loading…
Loading…
Loading…
call
instruction. The basic idea is that we jump into
that location, run the code, and jump back to where we came from.
We use the stack to store where we came from, so that we can get back. But since the code we call might trash the registers, we also want to save some registers on the stack. And we need to pass arguments, so we put those on the stack too, unless if they don't fit in the registers.
We also pass return values back through a register,
eax
on Intel.
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Since they are of a fixed size, you can put them on the stack easily enough.
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…
Loading…