July 21st, 2024

An interactive guide to x86-64 assembly

This interactive guide explains x86-64 assembly, focusing on data movement, endianness, stack manipulation, memory alignment, and stack growth direction. It includes examples and reading suggestions for deeper understanding.

Read original articleLink Icon
An interactive guide to x86-64 assembly

This article serves as an interactive guide to x86-64 assembly, focusing on moving data within memory. It explains the mov instruction, which transfers data between registers, memory, and vice versa. The concept of endianness is discussed, highlighting how numbers are stored in memory in a reversed order. The article also covers stack manipulation using push and pop instructions, illustrating how the stack pointer rsp moves when elements are added or removed. Memory alignment is explained, emphasizing the importance of aligning data on 8-byte boundaries for performance reasons. The article provides an interactive example showcasing memory visualization and addresses the confusion that can arise due to the stack growing towards lower memory addresses. It also offers further reading suggestions for those interested in delving deeper into x86-64 assembly.

Related

The Byte Order Fiasco

The Byte Order Fiasco

Handling endianness in C/C++ programming poses challenges, emphasizing correct integer deserialization to prevent undefined behavior. Adherence to the C standard is crucial to avoid unexpected compiler optimizations. Code examples demonstrate proper deserialization techniques using masking and shifting for system compatibility. Mastery of these concepts is vital for robust C code, despite available APIs for byte swapping.

Do not taunt happy fun branch predictor

Do not taunt happy fun branch predictor

The author shares insights on optimizing AArch64 assembly code by reducing jumps in loops. Replacing ret with br x30 improved performance, leading to an 8.8x speed increase. Considerations on branch prediction and SIMD instructions are discussed.

Weird things I learned while writing an x86 emulator

Weird things I learned while writing an x86 emulator

The article explores writing an x86 and amd64 emulator for Time Travel Debugging, emphasizing x86 encoding, prefixes, flag behaviors, shift instructions, segment overrides, FS and GS segments, TEB structures, CPU configuration, and segment handling nuances in 32-bit and 64-bit modes.

Run Functions in Another Stack with Zig

Run Functions in Another Stack with Zig

Alessio Marchetti discusses running functions in a separate stack using Zig, covering stack pointer manipulation in assembly code, optimized code generation, and a detailed Fibonacci function example. Emphasizing learning through experimentation without optimization flags.

Writing a BIOS bootloader for 64-bit mode from scratch

Writing a BIOS bootloader for 64-bit mode from scratch

Setting up an x86_64 CPU involves BIOS loading a boot sector, assembler like nasm, and QEMU emulation. Assembly code and GDT creation are crucial for transitioning to protected mode and reaching 64-bit long mode.

Link Icon 3 comments
By @Joker_vD - 7 months
> When we (humans using Hindu-Arabic numerals) represent numbers, we write the most significant value first, and continue in descending order.

Funny trivia: while the Arabs wrote the numbers exactly as we do, they actually wrote them in the opposite order, the least significant digit first, and continued in ascending order, because Arabic script is actually written right-to-left. We the Europeans borrowed the notation as it looked when finished being written, so it had the unfortunate side effect of switching to the unnatural big-endian order.

By @TedHerman - 6 months
So back in the 1970s I had a job working on an IBM mainframe computer. Not multicore, no cache or instruction pipeline that I was aware of. A coworker had the idea of writing a timer interrupt to see what kind of instruction was executing at the instance of the interrupt; eventually it accumulated tens of thousand of samples. What kind of instruction was most frequent, was it addition, multiplication, bit-logic, or what? If I remember correctly, at least 90% of the samples were moving data from one place to another (memory-to-memory, register to memory, memory to register, register to register). Computers don't "calculate" most of the time the way I had previously thought. They mostly just rearrange bits, copying this to that.
By @smartis2812 - 7 months
As somebody how tries to get into Assembly, I love this so much.

Thank you!