Data structures: linked 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.
This complete program builds three static nodes in .data and walks them, printing the value of each one:
default rel %define NODE_VAL 0 %define NODE_NEXT 8 %define SYS_WRITE 1 %define SYS_EXIT 60 section .data head dq node1 node1: dq 1 dq node2 node2: dq 2 dq node3 node3: dq 3 dq 0 digit db "0", 10 section .text global _start _start: mov rbx, [head] walk: test rbx, rbx jz done mov rax, [rbx + NODE_VAL] add al, "0" mov [digit], al mov rax, SYS_WRITE mov rdi, 1 lea rsi, [digit] mov rdx, 1 syscall mov rbx, [rbx + NODE_NEXT] jmp walk done: mov rax, SYS_EXIT xor rdi, rdi syscall
nasm -f elf64 -o list.o list.asm
ld -o list list.o
./list
echo $?
123
0
The snippets on this page are fragments, not whole programs: they show the node layout and the walk. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What two fields does a linked-list node hold?
Answer
A value and a pointer to the next node
The value is the payload, and the next pointer joins the nodes into a list.
-
What marks the end of the list?
Answer
A zero next pointer
The walk stops when NODE_NEXT loads zero.
-
What is the size of a node with two qwords?
Answer
16 bytes
Two 64-bit fields take sixteen bytes, and NODE_SIZE is 16.
-
Why is walking a list often slower than walking an array?
Answer
The nodes can be scattered in memory
Each next pointer can jump to a new cache line, which the prefetcher cannot predict.
-
Which syscall allocates memory for a new node?
Answer
brk (12) or mmap (9)
brk moves the heap break, and mmap maps a new region.
-
What does push-at-front do to the order of the list?
Answer
It reverses the order of insertion
Each new node becomes the head, so the last inserted node is visited first.
-
What does this program print?
default rel %define NODE_VAL 0 %define NODE_NEXT 8 %define SYS_WRITE 1 %define SYS_EXIT 60 section .data head dq node1 node1: dq 1 dq node2 node2: dq 2 dq node3 node3: dq 3 dq 0 digit db "0", 10 section .text global _start _start: mov rbx, [head] walk: test rbx, rbx jz done mov rax, [rbx + NODE_VAL] add al, "0" mov [digit], al mov rax, SYS_WRITE mov rdi, 1 lea rsi, [digit] mov rdx, 1 syscall mov rbx, [rbx + NODE_NEXT] jmp walk done: mov rax, SYS_EXIT xor rdi, rdi syscallAnswer
123
The list holds 1, 2 and 3 in that order, and each value becomes a digit.
-
What exit status does the list program return?
Answer
0
The walk ends with sys_exit and rdi cleared.
-
What does
mov rbx, [rbx + NODE_NEXT]do?Answer
It loads the next node pointer into rbx
The bracket reads the pointer field, and rbx becomes the next node.
-
Why does the loop test rbx before reading the value?
Answer
A zero pointer means the end of the list
Reading through a zero pointer would fault, so the end must be checked first.
Article author: Arthur Isaev