Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 84 additions & 21 deletions bun.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
"preview": "vite preview"
},
"dependencies": {
"@shikijs/rehype": "^4.6.0",
"katex": "^0.16.44",
"react": "^19.2.0",
"react-dom": "^19.2.0",
"react-markdown": "^10.1.0",
"react-syntax-highlighter": "^16.1.1",
"rehype-katex": "^7.0.1",
"remark-gfm": "^4.0.1",
"remark-math": "^6.0.0"
"remark-math": "^6.0.0",
"shiki": "^4.6.0"
},
"devDependencies": {
"@eslint/js": "^9.39.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ These notes are based on the post [**Why xor eax, eax?**](https://xania.org/2025

#### Development Environment

```bash
```console
$ lsb_release -d
Description: Ubuntu 24.04.3 LTS

Expand Down Expand Up @@ -49,7 +49,7 @@ Source Code (*.c) ---> | & | ---> | Optimizer | ---> | Generator
For now, we use `main.c` as input (shown below), apply different optimization levels,
and use `llvm-objdump` to analyze the corresponding assembly code.

```bash
```console
$ nvim main.c
```

Expand All @@ -61,11 +61,11 @@ int main() {

###### Use `-O0` as optimization level

```bash
```console
$ rm -f (path filter *.o); gcc -O0 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o
```

```x86asm
```asm
main.o: file format elf64-x86-64

Disassembly of section .text:
Expand All @@ -79,7 +79,7 @@ Disassembly of section .text:
e: c3 retq
```

```bash
```console
$ size main.o
```

Expand All @@ -91,11 +91,11 @@ text data bss dec hex filename
As `-O0`, the compiler generates a stack frame, leading to unnecessary instruction overhead.

###### Use `-O1` as optimization level
```bash
```console
$ rm -f (path filter *.o); gcc -O1 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o
```

```x86asm
```asm
main.o: file format elf64-x86-64

Disassembly of section .text:
Expand All @@ -106,7 +106,7 @@ Disassembly of section .text:
9: c3 retq
```

```bash
```console
$ size main.o
```

Expand All @@ -118,11 +118,11 @@ text data bss dec hex filename
It reduces the output from six instructions to three by removing the stack frame setup.

###### Use `-O2` as optimization level
```bash
```console
$ rm -f (path filter *.o); gcc -O2 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o
```

```x86asm
```asm
main.o: file format elf64-x86-64

Disassembly of section .text:
Expand All @@ -133,7 +133,7 @@ Disassembly of section .text:
6: c3 retq
```

```bash
```console
$ size main.o
```

Expand All @@ -149,15 +149,15 @@ making it smaller than the five bytes `movl $0x0, %eax`.
Hence, you can see the total `.text` size reduces from 90 bytes to 87 bytes.

#### How about we change `gcc` to `clang`?
```bash
```console
$ rm -f (path filter *.o); clang -O1 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o
```

```bash
```console
$ rm -f (path filter *.o); clang -O1 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o
```

```x86asm
```asm
main.o: file format elf64-x86-64

Disassembly of section .text:
Expand All @@ -172,7 +172,7 @@ Additionally, it consists of only two instructions because Clang does not genera

#### Why `eax`, not `rax` ?

```bash
```console
$ nvim get_val.c
```

Expand All @@ -182,11 +182,11 @@ long get_val() {
}
```

```bash
```console
$ rm -f (path filter *.o); clang -O2 -c get_val.c; llvm-objdump -d --x86-asm-syntax=att get_val.o
```

```x86asm
```asm
get_val.o: file format elf64-x86-64

Disassembly of section .text:
Expand All @@ -203,7 +203,7 @@ into the upper 32 bits of the corresponding 64-bit register.

Let's see an example for verifying Zero-Extension with LLDB

```bash
```console
$ nvim main.c
```

Expand All @@ -213,7 +213,7 @@ int main() {
}
```

```bash
```console
$ rm -f (path filter *.out); clang -g -O2 -o app.out main.c; lldb app.out
```

Expand Down Expand Up @@ -258,7 +258,7 @@ The LLDB output confirms that even though `xorl` only targets the lower 32 bits
the hardware automatically cleared the entire 64-bit `%rax` register.

#### Caller/Callee Arguments
```bash
```console
$ nvim main.c
```

Expand All @@ -275,13 +275,13 @@ void f() {
}
```

```bash
```console
$ rm -f (path filter *.o)
$ clang -O2 -c main.c
$ llvm-objdump -d --disassemble-symbols=f --x86-asm-syntax=att main.o
```

```x86asm
```asm
main.o: file format elf64-x86-64

Disassembly of section .text:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Selected technical insights from the YouTube comment section are reproduced at t
Written by me and assisted by AI, proofread by me and assisted by AI.

#### Development Environment
```bash
```console
$ lsb_release -d
Description: Ubuntu 24.04.3 LTS

Expand All @@ -37,7 +37,7 @@ $ echo $SHELL

To understand how `clang` translates `C` addition into `x86-64` machine instructions, we use the following implementation

```bash
```console
$ nvim add.c
```

Expand All @@ -49,11 +49,11 @@ int add(int x, int y) {

## Unoptimized Analysis

```bash
```console
$ rm -f (path filter *.o); clang -O0 -c add.c; llvm-objdump -d --x86-asm-syntax=att add.o
```

```x86asm
```asm
add.o: file format elf64-x86-64

Disassembly of section .text:
Expand All @@ -79,19 +79,19 @@ the compiler cannot translate `a = b + c` directly to a single `add` instruction
value of `b` or `c` before the operation is executed, the compiler needs to use `mov` instruction to
initialize the destination with one of the operands first:

```x86asm
```asm
movl -0x4(%rbp), %eax
addl -0x8(%rbp), %eax
```

Hence, the compiler needs to use two instructions to execute the addition at the -O0 level.

## Optimized Analysis
```bash
```console
$ rm -f (path filter *.o); clang -O2 -c add.c; llvm-objdump -d --x86-asm-syntax=att add.o
```

```x86asm
```asm
add.o: file format elf64-x86-64

Disassembly of section .text:
Expand All @@ -112,11 +112,11 @@ The following assembly code demonstrates these two approaches:
one utilizing the `mov` + `add` instruction sequence,
and the other employing a single `lea` instruction.

```bash
```console
$ nvim add.s
```

```x86asm
```asm
.section .note.GNU-stack, "", @progbits

.section .rodata
Expand Down Expand Up @@ -158,7 +158,7 @@ main:
retq
```

```bash
```console
$ rm -f (path filter *.out); clang -o add.out add.s; ./add.out
Result: 3
Result: 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ My notes focus on reproducing and verifying [Matt Godbolt](https://xania.org/Mat
Written by me and assisted by AI, proofread by me and assisted by AI.

## Development Environment
```bash
```console
$ lsb_release -d
Description: Ubuntu 24.04.3 LTS

Expand Down Expand Up @@ -42,7 +42,7 @@ $ echo $SHELL

We can observe the compiler's optimization by implementing addition through recursion, as shown in the following code:

```bash
```console
$ nvim add.c
```

Expand All @@ -66,18 +66,18 @@ int main(void) {

The following analysis examines the unoptimized assembly code generated with the `-O0` flag.

```bash
```console
$ rm -f (path filter *.out)
$ clang -O0 -target aarch64-linux-gnu --sysroot=/usr/aarch64-linux-gnu -static add.c -o app.out
$ qemu-aarch64 ./app.out
11 = 11
```

```bash
```console
$ llvm-objdump -d --disassemble-symbols=add app.out
```

```armasm
```text
app.out: file format elf64-littleaarch64

Disassembly of section .text:
Expand Down Expand Up @@ -109,7 +109,7 @@ Disassembly of section .text:
```

#### Part 01: Function Prologue
```armasm
```text
4007f0: d10083ff sub sp, sp, #0x20 // Allocate 32 bytes on stack
4007f4: a9017bfd stp x29, x30, [sp, #0x10] // Save Frame Pointer (x29) and Link Register (x30)
4007f8: 910043fd add x29, sp, #0x10 // Set up new Frame Pointer
Expand All @@ -135,7 +135,7 @@ facilitate the Stack Unwinding process during the Function Epilogue.
```

#### Part 02: Parameter Storage
```armasm
```text
4007fc: b81fc3a0 stur w0, [x29, #-0x4] // Store 'x' (w0) into stack
400800: b9000be1 str w1, [sp, #0x8] // Store 'y' (w1) into stack
400804: b9400be8 ldr w8, [sp, #0x8] // Load 'y' from stack into w8
Expand All @@ -146,7 +146,7 @@ Since it is at the `-O0` optimization level,
an additional instruction is used to load `y` from stack memory back into a register (`w8`) for subsequent conditional evaluation.

#### Part 03: Branching
```armasm
```text
400808: 71000108 subs w8, w8, #0x0 // Compare w8 (y) with 0
40080c: 540000a8 b.hi 0x400820 <add+0x30> // If y > 0, jump to recursive case (400820)
400810: 14000001 b 0x400814 <add+0x24> // Else, branch to base case logic
Expand All @@ -159,7 +159,7 @@ if `y > 0`, the Program Counter (`PC`) jumps to the Recursive Case;
otherwise, it jumps to the Base Case.

#### Part 04: The Base Case: `y == 0`
```armasm
```text
400814: b85fc3a0 ldur w0, [x29, #-0x4] // [Base Case] Load 'x' into w0
400818: b90007e0 str w0, [sp, #0x4] // Store 'x' as the potential return value
40081c: 14000008 b 0x40083c <add+0x4c> // Jump to epilogue (return) (Part 06)
Expand All @@ -170,7 +170,7 @@ The compiler then executes a `store` operation from register `W0` to stack memor
return value.

#### Part 05: The Recursive Step: `add(x + 1, y - 1)`
```armasm
```text
400820: b85fc3a8 ldur w8, [x29, #-0x4] // [Recursive Case] Load 'x' into w8
400824: 11000500 add w0, w8, #0x1 // w0 = x + 1 (Preparing 1st argument)
400828: b9400be8 ldr w8, [sp, #0x8] // Load 'y' into w8
Expand All @@ -185,7 +185,7 @@ The `bl` (Branch with Link) instruction then executes the recursive call, redire
Once the recursive call returns, the resulting value in `w0` is stored into stack memory before jumping to the epilogue.

#### Part 06: Function Epilogue
```armasm
```text
40083c: b94007e0 ldr w0, [sp, #0x4] // Load the result from stack into w0
400840: a9417bfd ldp x29, x30, [sp, #0x10] // Restore Frame Pointer and Link Register
400844: 910083ff add sp, sp, #0x20 // Deallocate stack space
Expand All @@ -202,18 +202,18 @@ redirects the Control Flow back to the address stored in the link register.

The following analysis examines the optimized assembly code generated with the `-O2` flag.

```bash
```console
$ rm -f (path filter *.out)
$ clang -O2 -target aarch64-linux-gnu --sysroot=/usr/aarch64-linux-gnu -static add.c -o app.out
$ qemu-aarch64 ./app.out
11 = 11
```

```bash
```console
$ llvm-objdump -d --disassemble-symbols=add app.out
```

```armasm
```text
app.out: file format elf64-littleaarch64

Disassembly of section .text:
Expand Down
Loading
Loading