Data structures: linked list

Contents
The node
Walking the list
Building the list

The node

A linked-list node is a value plus a pointer to the next node, or zero for the end. With qwords, the layout is 8 bytes of value and 8 bytes of pointer:

NODE_VAL equ 0 NODE_NEXT equ 8 NODE_SIZE equ 16

Walking the list

Start at the head pointer and follow NODE_NEXT until it is zero. Each step is one load, so the walk is memory-bound: nodes scattered in memory defeat the prefetcher, which is why arrays usually beat lists.

mov rsi, [head] walk: test rsi, rsi jz done mov rax, [rsi + NODE_VAL] ; ... use rax ... mov rsi, [rsi + NODE_NEXT] jmp walk

Building the list

Allocate nodes with sys_brk (12) or mmap (9), store the value at NODE_VAL and the old head at NODE_NEXT, then store the new node in head. That push-at-front builds the list in one pass, in reverse order.

Next: A tiny HTTP server

Article author: Arthur Isaev

Related articles
Assembler
Assembler course: zero to hero in 50 lessons
What is assembly language
Install NASM and check the version
NASM syntax basics
Your first program
Registers
MOV and data transfer
Number bases and operands
Defining strings and data
Flags and the status register
Conditional jumps
Loops
The stack
Procedures
LEA and addressing modes
Memory layout: sections
Linux syscalls
Arithmetic
Bitwise operations
Arrays
Structures
Macros
Calling C functions
Debugging with GDB
The ELF format
Time and date syscalls
File I/O
Command-line arguments
Environment variables
Floating point (SSE)
SIMD basics
Interrupts
Sockets
A tiny HTTP client
Output formats: bin, elf, elf64
Advanced macros and %include
Linking object files
32-bit vs 64-bit
The System V ABI
Makefiles for assembly
Optimization basics
Error codes and logging
Parsing CSV
Data structures: linked list
A tiny HTTP server
Boot sector: hello in 512 bytes
Capstone: command-line calculator
Capstone: hex editor
Performance: cycles and cache
Modern x86: AVX tour
Hero roadmap: the whole course on one page
Install NASM on Windows 11
Install NASM on Ubuntu
Install NASM on Rocky Linux
Install NASM on macOS
Install NASM on FreeBSD
Assembly in Docker
ARM assembly

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: