Just Enough Assembly for Compiler Explorer - Anders Schau Knatten - CppCon 2021

Learn assembly language basics: registers, data types, memory addressing, instructions, labels, functions, calling conventions, compilation, and debugging. Explore assembly code with Compiler Explorer.

Key takeaways
  1. Registers:
    • 16 general-purpose registers (rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp, r8, r9, r10, r11, r12, r13, r14, r15)
    • Each register can hold 8, 16, 32, or 64 bits of data, depending on the instruction.
  2. Data Types:
    • Assembly doesn’t have built-in data types like C or C++.
    • Data is represented as bytes in memory, and the interpretation of those bytes depends on the context.
  3. Memory Addressing:
    • Memory is addressed using the stack pointer (rsp) and the base pointer (rbp).
    • The stack grows downward from the top of the memory, and the base pointer points to the current stack frame.
  4. Instructions:
    • Assembly instructions are typically one or two lines long and consist of an opcode and one or more operands.
    • Common instructions include mov (move data), add (add two numbers), sub (subtract two numbers), cmp (compare two numbers), jmp (jump to a label), and call (call a function).
  5. Labels:
    • Labels are used to mark locations in the code that can be jumped to.
    • Labels are typically defined using the .L<label> directive.
  6. Functions:
    • Functions are defined using the .globl directive followed by the function name.
    • Functions can take arguments and return values using the stack.
  7. Calling Conventions:
    • The calling convention specifies how arguments are passed to functions and how the return value is returned.
    • The systemd-64 ABI is a common calling convention for x86-64 systems.
  8. Compilation:
    • Assembly code is compiled into machine code using an assembler.
    • The assembler translates the assembly instructions into binary code that can be executed by the CPU.
  9. Debugging:
    • Assembly code can be debugged using a debugger such as GDB.
    • Debuggers allow you to step through the code line by line and inspect the values of registers and memory locations.
  10. Compiler Explorer:
    • Compiler Explorer is a website that allows you to compile and run assembly code online.
    • Compiler Explorer provides a variety of features, including syntax highlighting, error checking, and a built-in debugger.