Reading Compiler Output and Assembly
Source code is what the developer meant; machine code is what the CPU does. When the two disagree, the machine code wins, and security work keeps landing in that gap: a bounds check the optimiser removed, a comparison compiled as signed when the author assumed unsigned, a binary shipped without the hardening flags the build system was supposed to add, a malware sample with no source at all. You don't need to write assembly to work in security, but reading twenty lines of compiler output and recognising a comparison, a branch and a function call is a core skill. The full language is taught in x86-64 Assembly (see its compiler output lesson); reverse engineering at scale comes in the reverse-engineering course.
Definitions (x86-64, Intel syntax)
- Registers:
rdi,rsi,rdx,rcx,r8,r9carry the first six integer arguments (System V ABI on Linux);rax(eaxfor 32 bits) carries the return value;rspis the stack pointer. cmp a, bsets flags froma − b; the next conditional jump reads them.- Signed jumps:
jl/jge(less / greater-or-equal, as signed numbers). Unsigned jumps:jb/jae(below / above-or-equal). The samecmpfollowed by a different jump means a different check. mov eax, DWORD PTR [rdi+rsi*4]: load 4 bytes from addressrdi + rsi*4, i.e.a[i]for anintarray.movsx(writtenmovsxdby some disassemblers for 32 → 64 bits): sign-extend a smaller value (anintindex becomes a 64-bit offset, negative stays negative).
A bounds check, signed and unsigned
The session compiles two versions of "return a[i] if i is in range, else −1". The only difference is the type of i. It prints the optimised assembly without assembler directives.
cat > get.c <<'SRC'
#include <stddef.h>
int get_unsigned(const int *a, size_t n, size_t i) {
if (i >= n) return -1;
return a[i];
}
int get_signed(const int *a, int n, int i) {
if (i >= n) return -1; /* forgot: i < 0 */
return a[i];
}
SRC
gcc -O2 -S -masm=intel -fno-asynchronous-unwind-tables -fcf-protection=none -o - get.c \
| grep -vE '^\s*\.(file|text|p2align|globl|type|size|ident|section)|^\s*