Skip to content

Commit df52fda

Browse files
committed
Split more ia-32/main
1 parent 5110400 commit df52fda

57 files changed

Lines changed: 1305 additions & 461 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ x86 only for now, but we'd love to try out other architectures.
1313
1. [Assemblers](assemblers.md)
1414
1. [Pros and cons](pros-and-cons.md)
1515
1. [Intel processor history](intel-processor-history.md)
16+
1. [CPU Architecture](cpu-architecture.md)
1617
1. [IA-32](ia-32.md)
1718
1. [IA-32 code](ia-32/)
1819
1. [Segmentation](segmentation.md)
1920
1. [Rings](rings.md)
2021
1. [x86-64](x86-64/)
2122
1. [Calling conventions](calling-conventions.md)
22-
1. [cdecl](cdecl.md)
2323
1. [Containers](containers.md)
2424
1. [ELF](elf.md)
2525
1. [ELF hello world](elf-hello-world.md)

bibliography.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,13 @@
5656

5757
Washington university course that covers branch prediction, cache and other CPU internals
5858

59+
## Libraries
60+
61+
- <https://github.com/aquynh/capstone>
62+
63+
Looks like the best free disassembly library out there.
64+
65+
Binutils cannot be used as a library to disassemble? Sad.
66+
5967
[Itanium C++ ABI]: http://mentorembedded.github.io/cxx-abi/abi.html
6068
[System V ABI AMD64]: http://www.x86-64.org/documentation_folder/abi-0.99.pdf

binutils.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Also contains GDB.
1212

1313
## Makefile
1414

15+
sudo apt-get build-dep gdb
1516
mkdir binutils-gdb
1617
cd binutils-gdb
1718
git clone git://sourceware.org/git/binutils-gdb.git src
@@ -49,7 +50,6 @@ Try it out on Linux:
4950
./ld -o main.out main.o
5051
./main.out
5152

52-
5353
Took 3 minutes in a 2013 computer.
5454

5555
Why `--with-sysroot` is needed <https://sourceware.org/ml/binutils/2007-02/msg00274.html>. Why not make it the default?!

calling-conventions.md

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ GCC allows to specify calling convention explicitly as:
1616

1717
where `cdecl` is the name of the calling convention.
1818

19-
In IA32, [cdecl](cdecl.md) is the most popular in Linux, and stdcall the most popular in Windows, but there exist many others.
19+
In IA32, cdecl is the most popular in Linux, and stdcall the most popular in Windows, but there exist many others.
2020

21-
## Call C function form assembly
22-
23-
TODO link to example code.
21+
## Call C function from assembly
2422

2523
To call a C function from assembly you need to:
2624

@@ -33,17 +31,3 @@ To call a C function from assembly you need to:
3331
For example, `exit` may be called `_exit` on certain compilers such as GCC elf format.
3432

3533
This is not specified by ANSI C, so the only portable alternative is via macros.
36-
37-
- declare the function you want to call as extern.
38-
39-
NASM for example has the `extern` directive.
40-
41-
- compile into an object file and then compile that object file with the C object files.
42-
43-
For example with nasm:
44-
45-
nasm -o a.o a.asm
46-
gcc -c c.c -o c.o
47-
gcc -o main a.o c.o
48-
49-
You can also call stdlib functions if you want, since GCC links to them for you.

compiler-generated/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Let's see what our compilers are compiling to.
66
1. [GCC](gcc.md)
77
1. C
88
1. [hello_world.c](hello_world.c)
9-
1. [function_int_int_int.c](function_int_int_int.c)
9+
1. Functions
10+
1. [function_int_int_int.c](function_int_int_int.c)
1011
1. [return_struct.c](return_struct.c)
1112
1. switch
1213
1. [switch3.c](switch3.c)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
A function that calls another.
3+
*/
4+
5+
#include <stdio.h>
6+
#include <time.h>
7+
8+
int func1(int i, int j, int k) {
9+
int l;
10+
l = i + j + k;
11+
return l;
12+
}
13+
14+
/*
15+
This function calls another function.
16+
17+
In rsp must be moved to:
18+
19+
- store all arguments and local variables.
20+
- align to 0x10 in x86-64
21+
*/
22+
int func0(int i, int j, int k) {
23+
int l;
24+
l = func1(i, j, k);
25+
return l;
26+
}
27+
28+
int main() {
29+
printf("%d\n", func0(1, 2, 3));
30+
return 0;
31+
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
/* Simple function that takes two ints and returns a third. */
1+
/*
2+
Simple function that takes two ints and returns a third.
3+
*/
24

35
#include <stdio.h>
46

57
/*
68
GCC 4.8 -O3 reduces this to a single lea.
9+
10+
At `-O0`, when there is no function call inside this function, `rsp` is not changed.
11+
Such function is called a leaf function.
12+
13+
TODO why does the first variable go to `rbp - 0x14` instead of `rbp - 0x4`?
714
*/
8-
int my_function(int i, int j) {
15+
int func0(int i, int j) {
916
int k;
1017
k = i + j;
1118
return k;
@@ -15,6 +22,6 @@ int main() {
1522
/*
1623
GCC 4.8 -O3 resolves the function call into a constant.
1724
*/
18-
printf("%d\n", my_function(1, 2));
25+
printf("%d\n", func0(1, 2));
1926
return 0;
2027
}

compiler-generated/gcc/builtin_expect.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ int main() {
55
int i;
66
i = !time(NULL);
77
if (__builtin_expect(i, 0))
8-
printf("%d\n", i);
9-
puts("a");
8+
puts("a");
109
return 0;
1110
}

compiler-generated/gcc/builtin_expect_off.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ int main() {
55
int i;
66
i = !time(NULL);
77
if (i)
8-
printf("%d\n", i);
9-
puts("a");
8+
puts("a");
109
return 0;
1110
}

cpu-architecture.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# CPU Architecture
2+
3+
<https://en.wikipedia.org/wiki/Accumulator_%28computing%29>: same as general purpose registers, but specially in the past or current microcontrollers where there was a single GP register!

0 commit comments

Comments
 (0)