#!/usr/bin/env python3 """The introduction's chapter on registers (design/motion.md, "A scene: the introduction", II), and a seam between two of its chapters. python3 -B regs.py # 58-regs-pins, 59-regs-parts, 60-regs-call, 61-regs-fold, 62-seam (.svg, .png, -480.png) The data: anim/tools/list.c built as the introduction builds it (clang -O0 -g -mno-red-zone -no-pie -fno-stack-protector -Wl,-z,now), run as `./list` in a directory of its own with the debuginfod cache linked beside it, `env -i DEBUGINFOD_CACHE_PATH=debuginfod PAD=<240 x> setarch -R ztrace record --by insn --stack 1024 --around 256 --disasm 69 -s 64 --from list.c:40 --until list.c:22 -w head -- ./list` (PAD: the scene's environment is longer, its agent preloaded; 240 bytes put rsp at reverse's entry where the scene's recording has it, 7fffffffebc8). Its moments as `ztrace show` numbers them: 38 (push(9) at line 11, `mov edi, 0x10`: the chapter's first), 39 (`call malloc@plt`), 201 (back in push: malloc returned), 214 (reverse's first instruction: chapters I and III), counted on the clock from 32, the recording's first stop at line 40. The stack's words and the heap from `ztrace dbg` at the same places (`x`). Without the scene's agent its malloc is glibc's alone: across the call it changes rax, rcx, rsi and rdi, and r11's word; the scene's recording (agent preloaded) also leaves its own values in r8 to r11, which is why the chapter wants its recording made without it (README, "What the recordings must hold"). Five of the 128 bytes differ from the scene's for the same reason (ebf0, ec00, ec10, ec18, ec28: what start-up left there). """ import subprocess import gen import context as C import intro as I import column as K from context import (PAGE, RAISED, RULE, VELLUM, BRIGHT, DIM, FAINT, GOLD, HEAP, STACK, CODE, DATA, A, S, ROW, TAG, PAD, MONO, BOLD, b, ph, mix, Svg, tw) HERE = gen.HERE READ, VERM = gen.READ, gen.VERM RX, LX, CW = 976, 64, 880 # ---- the process: its mappings (`ztrace show 214`) ------------------------------------------------------------- I.MAPS = [(0x400000, 0x401000, "r--", "list"), (0x401000, 0x402000, "r-x", "list"), (0x402000, 0x403000, "r--", "list"), (0x403000, 0x404000, "r--", "list"), (0x404000, 0x405000, "rw-", "list"), (0x405000, 0x426000, "rw-", "[heap]"), (0x7ffff7c00000, 0x7ffff7c24000, "r--", "libc.so.6"), (0x7ffff7c24000, 0x7ffff7d9f000, "r-x", "libc.so.6"), (0x7ffff7d9f000, 0x7ffff7e15000, "r--", "libc.so.6"), (0x7ffff7e15000, 0x7ffff7e19000, "r--", "libc.so.6"), (0x7ffff7e19000, 0x7ffff7e1b000, "rw-", "libc.so.6"), (0x7ffff7e1b000, 0x7ffff7e23000, "rw-", ""), (0x7ffff7f70000, 0x7ffff7f75000, "rw-", ""), (0x7ffff7f75000, 0x7ffff7fb6000, "r--", "ld.so.cache"), (0x7ffff7fb6000, 0x7ffff7fba000, "r--", "[vvar]"), (0x7ffff7fba000, 0x7ffff7fbc000, "r--", "[vvar_vclock]"), (0x7ffff7fbc000, 0x7ffff7fbe000, "r-x", "[vdso]"), (0x7ffff7fbe000, 0x7ffff7fbf000, "r--", "ld-linux…"), (0x7ffff7fbf000, 0x7ffff7fec000, "r-x", "ld-linux…"), (0x7ffff7fec000, 0x7ffff7ffb000, "r--", "ld-linux…"), (0x7ffff7ffb000, 0x7ffff7ffd000, "r--", "ld-linux…"), (0x7ffff7ffd000, 0x7ffff7ffe000, "rw-", "ld-linux…"), (0x7ffff7ffe000, 0x7ffff7fff000, "rw-", ""), (0x7ffffffde000, 0x7ffffffff000, "rw-", "[stack]")] I.CLUSTERS = {"stack": (0x7ffffffde000, 0x7ffffffff000), "libraries": (0x7ffff7c00000, 0x7ffff7fff000), "program": (0x400000, 0x426000)} ink_of = I.ink_of SYMS = {0x401110: "__do_global_dtors_aux", 0x401150: "push", 0x4011a0: "reverse", 0x401240: "main", 0x7ffff7c27700: "__libc_start_call_main", 0x7ffff7fe0c60: "dl_main"} def name(v): f = max(a for a in SYMS if a <= v) return "%s+%d" % (SYMS[f], v - f) # ---- memory: the stack and the heap at each moment (ztrace dbg's `x`) ----------------------------------------- DUMP = [0x401272, 0, 0, 0x7fffffffec90, 0x7ffff7c27781, 0x7ffff7fe0c60, 0x7fffffffed18, 0x1f7fbe000, 0x401240, 0x7fffffffec50, 0x7ffff7fdf260, 0, 0xc429524e4479b0ef, 0x7fffffffed18, 1, 0x7ffff7ffd000] # 214, from ebc8 RSP_M = 0x7fffffffebc8 MEM = {0x7fffffffebe0: 0x7fffffffec90, 0x7fffffffec90: 0x7fffffffecf0, 0x7fffffffecf0: 0, 0x7fffffffed18: 0x7fffffffeed4, 0x7ffff7e19ac0: 0, 0x7ffff7ffd000: 0x7ffff7ffe3c0, 0x7ffff7ffe3c0: 0, 0x403dc0: 0x401110, 0x405010: 7, 0x405030: 0xc, 0x405040: 0} STRINGS = {0x7fffffffeed4: "./list"} WRITES = { # what each moment's memory holds that the one before did not: push's frame over the 128 bytes, the heap 38: {0x7fffffffebb0: 0x405030, 0x7fffffffebb8: 9, 0x7fffffffebc0: 0x7fffffffebe0, 0x7fffffffebc8: 0x40126d, 0x7ffff7e19ac8: 0x405040}, 201: {0x405050: 0, 0x405060: 0, 0x7ffff7e19ac8: 0x405060}, 202: {0x7fffffffebb0: 0x405050}, 214: {0x405050: 9, 0x405058: 0x405030, 0x7fffffffebc8: 0x401272}} def mem(k): m = dict(MEM) for at in sorted(WRITES): if at <= k: m.update(WRITES[at]) return m def follow(v, m): """A register's or a word's chain, as the recording's telescope follows it: (links, end).""" links = [] while True: ink = ink_of(v) if ink is None: return links, ("int", v) if ink == CODE: return links + [(v, CODE, name(v))], None links.append((v, ink)) if v in STRINGS: return links, ("str", STRINGS[v]) if v not in m or len(links) > 4: return links, None v = m[v] # ---- the registers at each moment (ztrace show) ------------------------------------------------------------------ NAMES = ("rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15") AT = { 38: dict(rax=0x405030, rbx=0, rcx=0x405010, rdx=0, rsi=0x405040, rdi=9, rbp=0x7fffffffebc0, rsp=0x7fffffffebb0, rip=0x40115c, eflags=0x202), 39: dict(rdi=0x10, rip=0x401161), 201: dict(rax=0x405050, rcx=0x21, rsi=0x405060, rdi=4, rip=0x401166, eflags=0x246), 214: dict(rcx=0x405030, rbp=0x7fffffffebe0, rsp=RSP_M, rip=0x4011a0, eflags=0x206), } FIXED = dict(r8=0x7ffff7e19ac0, r9=0x20, r10=0x10, r11=0x7ffff7e19ac8, r12=0x7fffffffed18, r13=1, r14=0x7ffff7ffd000, r15=0x403dc0) QUIET = ("rbx", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15") # the intro never changes these FIRST = 32 # the recording's first stop, at line 40: the clock counts from it TOTAL = 225 - FIRST def vals(k): v = dict(FIXED) for at in sorted(AT): if at <= k: v.update(AT[at]) return v def regs(k, km=None, rip=None): """The registers after moment k (rip: where the mark still is, mid-step), chains through memory as of km.""" v, m = vals(k), mem(k if km is None else km) out = [] for r in NAMES: links, end = follow(v[r], m) if ink_of(v[r]) else ([], ("int", v[r])) out.append((r, links, end)) pc = v["rip"] if rip is None else rip out.append(("rip", [(pc, CODE, name(pc))], None)) return out def flags(k, changed=()): e = vals(k)["eflags"] return [(f, e >> bit & 1, f in changed) for f, bit in (("CF", 0), ("PF", 2), ("AF", 4), ("ZF", 6), ("SF", 7), ("IF", 9), ("DF", 10), ("OF", 11))] PARTS = {"rax": ("eax", "ax", "al"), "rdi": ("edi", "di", "dil"), "rsi": ("esi", "si", "sil")} def registers(g, y, rs, fl, marks, quiet=(), unpack=None, frames=()): """`registers` as context.registers draws it, and `reg`'s `parts`: unpack = (register, value, struck byte index or None, the part framed or None) opens that register's row into its eight bytes, the low ones bracketed by its parts' names, a row each under it (the rows below make room). frames: registers whose value is framed as read (an echo, no chain). Returns each register's row y.""" extra = 3 if unpack else 0 y0 = C.panel(g, RX, y, CW, C.reg_rows(quiet) + extra, "registers", note=[("stack", STACK, False), ("heap", HEAP, False), ("code", CODE, False), ("data", DATA, False), ("rwx", DATA, True), ("read-only", VELLUM, False)]) where = {} full = [r for r in rs if r[0] not in quiet] rows = [] for r in full: rows.append(r) if unpack and r[0] == unpack[0]: rows += [("·part", p, None) for p in PARTS[unpack[0]]] if quiet: qy = y0 + (len(rows) - 1) * ROW g.rect(RX + 2, qy, CW - 4, 2, RULE) for k, (nm, links, end) in enumerate(r for r in rs if r[0] in quiet): xx, yy = RX + 12 + (k % 3) * C.QUIET_PITCH, qy + (k // 3) * ROW where[nm] = yy g.t(xx, b(yy), nm, S, FAINT, MONO) if links: x = C.addr(g, xx + 48, yy, links[0][0], links[0][1]) if len(links) > 1 or end is not None: C.more(g, x, yy) else: C.integer(g, xx + 48, yy, end[1]) rows = rows[:-1] + [None] * ((len(quiet) + 2) // 3) + rows[-1:] for i, r in enumerate(rows): if r is None: continue nm, links, end = r yy = y0 + i * ROW if nm == "·part": part_row(g, yy, links, unpack) continue where[nm] = yy if nm == "rip": g.t(RX + 12, b(yy), nm, S, C.current_row(g, RX + 2, yy, CW - 4, 54), BOLD) else: g.t(RX + 12, b(yy), nm, S, DIM, MONO) if unpack and nm == unpack[0]: parts_bytes(g, yy, unpack) continue if nm in frames: w = (len("%x" % links[0][0]) if links else len(str(end[1]))) * A g.frame(RX + 72 - 6, yy, w + 12, ROW, READ, 2) if links: C.chain(g, RX + 72, yy, links, end, marks.get(nm), room=RX + CW - 12) else: mk = marks.get(nm, {}) if mk.get(0) == "strike": s_ = ("0x%x" % end[1]) if end[1] >= 10 else str(end[1]) x = RX + 72 + len(s_) * A g.t(RX + 72, b(yy), s_, S, C.struck(g, RX + 67, yy, len(s_) * A + 10), BOLD) if 10 <= end[1] <= 0xffff: g.t(x + 8, b(yy), str(end[1]), S, FAINT, MONO) continue C.integer(g, RX + 72, yy, end[1]) if mk.get(0) == "tick": C.held(g, RX + 72, yy, len(str(end[1])) * A) x = RX + CW - 12 - len(fl) * 42 + 10 # the flags, in rip's row for fname, on, changed in fl: if on: g.rect(x, yy + 2, 2 * A + 8, 20, RAISED) g.t(x + 4, b(yy), fname, S, BRIGHT, BOLD) else: g.t(x + 4, b(yy), fname.lower(), S, mix(DIM, C.band(), 0.8), MONO) if changed: # over the chip: a turned corner, the change C.held(g, x + 4, yy, 2 * A) x += 42 return where BYTES_X = RX + 72 # a register's eight bytes, as a dump's: two digits in 24, a pitch of 30 def part_n(part): """How many low bytes a part is: e.. four, ..l one, the rest two.""" return 4 if part.startswith("e") or part.endswith("d") else (1 if part.endswith("l") or part.endswith("b") else 2) def parts_bytes(g, y, unpack): """The register's value as its eight bytes, high byte first as the number reads, so the low ones are on the right; the zero bytes above its highest non-zero one at 45% toward the page (`pad`). The part an instruction names has its bytes framed as read with it (an echo).""" reg, v, hit, framed = unpack bs = [(v >> (8 * (7 - k))) & 0xff for k in range(8)] top = next((k for k in range(8) if bs[k]), 7) if framed: n = part_n(framed) g.frame(BYTES_X + 30 * (8 - n) - 6, y, 30 * n + 6, ROW, READ, 2) for k, by in enumerate(bs): x = BYTES_X + 30 * k if k == hit: g.t(x, b(y), "%02x" % by, S, C.struck(g, x - 5, y, 34), BOLD) else: g.t(x, b(y), "%02x" % by, S, mix(VELLUM, PAGE, 0.45) if k < top else VELLUM, MONO) def part_row(g, y, part, unpack): """A part of a register, a row under it: a bracket under the low bytes it is, serifs up to them, and its name ranged right just before it, so that each name stands at the start of what it names.""" reg, v, hit, framed = unpack n = part_n(part) x0, x1 = BYTES_X + 30 * (8 - n) - 3, BYTES_X + 30 * 7 + 24 + 3 nx = x0 - 10 if part == framed: # named by the instruction: framed with its operand (an echo) g.frame(nx - len(part) * A - 6, y, x1 + 6 - (nx - len(part) * A - 6), ROW, READ, 2) g.t(nx, b(y), part, S, VELLUM if part == framed else DIM, MONO, "end") g.rect(x0, y + 11, x1 - x0, 2, DIM) g.rect(x0, y + 2, 2, 11, DIM) g.rect(x1 - 2, y + 2, 2, 11, DIM) # ---- the program column: segments, live; the code ------------------------------------------------------------- TOP = 80 # the address space's top, as chapter I leaves it BOXES = {"stack": (80, 72), "libraries": (204, 368), "program": (620, 252)} BOUNDS = {"stack": (104, 172), "libraries": (192, 596), "program": (608, 884)} def space(g, pins=None, under=(), hot=(), dim=1.0): """`segments` in the program column, as chapter I leaves it (the whole at x 224, the boxes 108 wide at 368, their names from 540), with the registers' pins once the chapter has given them: pins {address: name} on its band's row (`◂ rip`), under [(address, names)] an `under` pin in its band; hot: the bands framed (an echo).""" apex = I.whole(g, 224, TOP, 864) for nm in ["stack", "libraries", "program"]: lo, hi = I.CLUSTERS[nm] y, h = BOXES[nm] I.magnify(g, *apex[nm], 368, y, h) runs = (I.runs_of if nm == "program" else I.files_of)(lo, hi) yof = I.enlargement(g, 368, y, 108, h, lo, hi, (), [(r[0], r[1]) for r in runs if r[2] is None], hot) I.table(g, 368, 108, yof, runs, 540, *BOUNDS[nm], pins, hot) for a, names in under: if lo <= a < hi: C.pin_up(g, 368 + 54, yof(a), names, ink_of(a), S) PUSH = [(0x401150, 10, "push", "rbp"), (0x401151, 10, "mov", "rbp, rsp"), (0x401154, 10, "sub", "rsp, 0x10"), (0x401158, 10, "mov", "qword ptr [rbp - 8], rdi"), (0x40115c, 11, "mov", "edi, 0x10"), (0x401161, 11, "call", (0x401040, "malloc@plt")), (0x401166, 11, "mov", "qword ptr [rbp - 0x10], rax"), (0x40116a, 12, "mov", "rcx, qword ptr [rbp - 8]")] REVERSE = [(0x4011a0, 17, "push", "rbp"), (0x4011a1, 17, "mov", "rbp, rsp"), (0x4011a4, 17, "sub", "rsp, 0x18"), (0x4011a8, 18, "mov", "qword ptr [rbp - 8], 0"), (0x4011b0, 19, "mov", "rax, qword ptr [rip + 0x2e61]"), (0x4011b7, 19, "mov", "qword ptr [rbp - 0x10], rax")] CODE_Y = 740 # the disassembly's rows where chapter III's panel will hold them def code(g, table, first, states, notes, cur_line): """Six rows of code, no panel round them yet (chapter III's `frame up` gives it one), from `first`. notes: {address: runs}, a run (text, ink) or (text, "struck"), in the annotation column.""" i = [r[0] for r in table].index(first) rows = [(a, ln, mn, [(ops, CODE)] if isinstance(ops, tuple) else [(ops, VELLUM)], states.get(a), None) for a, ln, mn, ops in table[i:i + 6]] C.LX = LX at = C.insn_rows(g, CODE_Y, rows, cur_line) for a, runs in notes.items(): x, y = LX + C.DX_NOTE, at[a] for s_, ink in runs: if ink == "struck": g.t(x, b(y), s_, S, C.struck(g, x - 5, y, len(s_) * A + 10), BOLD) else: g.t(x, b(y), s_, S, ink, BOLD if ink == BRIGHT else MONO) x += len(s_) * A return at # ---- the texts: below, beside on the left, the title; the clock and its label --------------------------------- def below(g, lines): gen.caption(g, lines) def wrap(text): """A bubble's lines, 40 characters of Go 40 a line; code (between backticks) in mono 35.""" parts, mono = [], False for k, piece in enumerate(text.split("`")): if piece: parts.append((piece, "mono" if k % 2 else "sans")) return K.wrap(parts) def bubble(g, top, lines, newest): """A text beside, on the left (the program column, while it holds no more than the code at its foot): Go 40 on 54, the newest on raised paper as wide as its longest line, an older one bare and dim.""" widths = [sum((len(w) * 21 if f == "mono" else tw(w, 40, "sans")) + (tw(" ", 40, "sans") if sp else 0) for w, f, sp in ln) for ln in lines] h = len(lines) * 54 + 26 if newest: g.rect(LX, top, max(widths) + 32, h, RAISED) y = top + 13 + 40 for ln in lines: g.rich(LX + 16, y, [(w + (" " if sp else ""), VELLUM if newest else DIM, "mono" if f == "mono" else "sans") for w, f, sp in ln], 40) y += 54 return h def place(levels, heights, floor, ceil): """Each bubble's top: the newest (last) with its first line's centre on its level; each older one, newest first, at the free place nearest its own level, above or below those placed: none overlaps another.""" tops, placed = [None] * len(levels), [] for j in range(len(levels) - 1, -1, -1): want = min(max(levels[j] - 40, ceil), floor - heights[j]) cands = [want] + [t + h + 12 for t, h in placed] + [t - heights[j] - 12 for t, h in placed] free = [c for c in cands if ceil <= c <= floor - heights[j] and all(c + heights[j] + 12 <= t or c >= t + h + 12 for t, h in placed)] tops[j] = min(free, key=lambda c: abs(c - want)) if free else want placed.append((tops[j], heights[j])) return tops def beside(g, texts, floor=CODE_Y - 12, ceil=84): """texts: (level, text), oldest first; three at most stand, the newest last. ceil: under the shelf.""" ls = [wrap(t) for _, t in texts] hs = [len(l) * 54 + 26 for l in ls] tops = place([lv for lv, _ in texts], hs, floor, ceil) for i, (lines, top) in enumerate(zip(ls, tops)): bubble(g, top, lines, i == len(texts) - 1) CHAPTERS = {1: ("I", "memory"), 2: ("II", "registers"), 3: ("III", "the stack"), 4: ("IV", "code and meaning")} def clock(g, chapter, moment=None, label_ink=FAINT): """The clock: one recording, from its first stop at line 40, counted from chapter II's first registers on; and before its count, ranged right, the chapter's numeral and name (Go 18, faint): where in the video.""" count = "" if moment is not None: g.rect(LX, 899, 1920 - 2 * LX, 2, RULE) g.rect(LX, 898, (1920 - 2 * LX) * (moment - FIRST) / TOTAL, 4, GOLD) count = "moment %d of %d" % (moment - FIRST, TOTAL) g.t(1856, 934, count, S, FAINT, MONO, "end") num, nm = CHAPTERS[chapter] end = 1856 - (len(count) * A + 24 if count else 0) g.t(end, 932, num + " " + nm, 18, label_ink, "sans", "end") def title(g, chapter): """`chapter`'s title, said in the caption's place at a seam: its numeral faint, its name bright, Go Medium 40.""" num, nm = CHAPTERS[chapter] g.rich(gen.MX, gen.CAP_Y + 78, [(num + " ", FAINT, "sansm"), (nm[0].upper() + nm[1:], BRIGHT, "sansm")], 40) # ---- chapter I's last picture, and the dump as it stands in it --------------------------------------------------- DX, DTOP = 1000, 96 # the dump in chapter I's last picture (the state column, x 2 left) def word_text(w): return "%x" % w if ink_of(w) or w < 10 else "0x%x" % w def dump(g, x, top, chains=True, ink=True, fade=1.0): """The 128 bytes as chapter I leaves them: addresses in the stack's ink, the bytes faint, the words to their field's left, three of them followed (I10).""" for r, w in enumerate(DUMP): a, y = RSP_M + 8 * r, top + r * ROW C.addr(g, x, y, a, mix(STACK, PAGE, fade)) for k in range(8): g.t(x + 156 + 30 * k, b(y), "%02x" % ((w >> (8 * k)) & 0xff), S, mix(mix(VELLUM, PAGE, 0.45), PAGE, fade), MONO) wx = x + 438 if chains and r in (0, 3, 4): links, end = follow(w, MEM) if r == 0: links, end = [(w, CODE, name(w))], None if r == 4: links, end = [(w, CODE, "__libc_start_call_mai…")], None # cut at the stage's edge C.chain(g, wx, y, links, end, room=1856) else: ik = (ink_of(w) or VELLUM) if ink else VELLUM if ink_of(w) == CODE and r != 0: ik = CODE g.t(wx, b(y), word_text(w), S, mix(ik, PAGE, fade), MONO) def place_tie(g, quiet=True): """A dump's place: the `tie` bracket on its rows and the link over the stage's top onto the stack's box.""" ink = mix(STACK, PAGE, 0.6) if quiet else STACK C.bracket(g, DX - 12, DTOP + 2, DTOP + 16 * ROW - 2, ink, 3, 6) gen.arrow(g, [(DX - 12, DTOP + 2), (DX - 12, 56), (450, 56), (450, TOP - 1)], ink, 3, r=8, hl=10, hw=10) def chapter_one_last(g, label=1, said=True): space(g) dump(g, DX, DTOP) place_tie(g) if said: below(g, [[("Follow an address and you reach another word: a chain.",)], [("→ points to a word, ← holds a value.",)]]) clock(g, label) # ---- the beats --------------------------------------------------------------------------------------------------- SHELF = [("the 128 bytes", [])] def beat_pins(g): """II3, held: rbp and rsp framed with the stack's band, their pin in it; rip's pin on list's code row (II2), standing; the registers whole, seventeen rows; the 128 bytes on the shelf.""" K.shelf(g, SHELF) space(g, pins={0x40115c: "rip"}, under=[(0x7fffffffebb0, ["rbp", "rsp"])], hot={0x7fffffffebb0}) registers(g, 48, regs(38), flags(38), {"rbp": {0: "read"}, "rsp": {0: "read"}}) below(g, [[("rsp", "mono"), (" and ",), ("rbp", "mono"), (" point into the stack,",)], [("rax", "mono"), (", ",), ("rcx", "mono"), (" and ",), ("rsi", "mono"), (" into the heap.",)]]) clock(g, 2, 38) def beat_parts(g): """II5, struck: rdi's row open into its parts; `mov edi, 0x10` has fired, edi named by its operand (the part and the operand framed together), its low byte armed and struck, the row's annotation with it.""" K.shelf(g, SHELF) at = code(g, PUSH, 0x401154, {0x40115c: "cur"}, {0x40115c: [("rdi ", DIM), ("0x10", "struck"), (" 16", FAINT)], 0x401161: [("malloc", BRIGHT)]}, 11) g.frame(LX + C.DX_OPS - 6, at[0x40115c], 3 * A + 12, ROW, READ, 2) # `edi`, the part the instruction names where = registers(g, 48, regs(38), flags(38), {}, unpack=("rdi", 0x10, 7, "edi")) beside(g, [(where["rip"] + 12, "`rip` holds this row's address: the next to run, in `push(9)`."), (where["rdi"] + 12, "`rdi` holds push's argument, 9. Its low four bytes are `edi`, as `eax` " "are `rax`'s.")]) clock(g, 2, 38) def beat_call(g): """II6, mid-step: `call malloc@plt` stepped over; rax struck with the result and the call's annotation with it (an echo); then rcx, rsi and rdi in a wave; r11's word, and PF and ZF, held; the mark not yet moved.""" K.shelf(g, SHELF) code(g, PUSH, 0x401154, {0x40115c: "trail0", 0x401161: "cur"}, {0x40115c: [("rdi ", DIM), ("0x10", VELLUM), (" 16", FAINT)], 0x401161: [("malloc", BRIGHT), ("(16) = ", VELLUM), ("405050", "struck")]}, 11) rs = regs(201, rip=0x401161) marks = {"rax": {0: "strike"}, "rcx": {0: "strike"}, "rsi": {0: "strike"}, "rdi": {0: "strike"}} where = registers(g, 48, rs, flags(201, ("PF", "ZF")), marks) r11 = where["r11"] # its word changed under it: the corner on that hop alone C.held(g, RX + 72 + 12 * A + 30, r11, 6 * A) beside(g, [(where["rip"] + 12, "`rip` holds this row's address: the next to run, in `push(9)`."), (where["rdi"] + 12, "`rdi` holds push's argument, 9. Its low four bytes are `edi`, as `eax` " "are `rax`'s."), (where["rax"] + 12, "Arguments go in `rdi`, `rsi`…; the result comes back in `rax`.")]) clock(g, 2, 39) def beat_fold(g): """II8, the chapter's last picture: the registers this video does not follow folded three to a row, the 128 bytes back from the shelf under them, where chapter III's stack will stand; reverse's first rows, the mark on push rbp: chapter I's moment.""" code(g, REVERSE, 0x4011a0, {0x4011a0: "cur"}, {}, 17) held = {r: {0: "tick"} for r in ("rcx", "rsi", "rdi", "rbp", "rsp")} # struck in the call and the continue held["rax"] = {0: "tick", "end": "tick"} # and 9 written under it since where = registers(g, 48, regs(214), flags(214, ("ZF",)), held, QUIET) dump(g, DX, 356 + TAG + PAD + 4 * ROW) beside(g, [(where["rax"] + 12, "Arguments go in `rdi`, `rsi`…; the result comes back in `rax`."), (where["rsp"] + 12, "On, into `reverse()`: `rsp` and `rbp` move with the calls."), (where["r8"] + 12, "Registers we will not follow fold, three to a row. We are back where the 128 " "bytes were read.")], ceil=60) clock(g, 2, 214) def seam(g): """The seam from chapter I to II, four instants, each a frame at half size: (1) I's last picture, its text read; (2) the title said over it, the label changed; (3) the dump folding to the shelf under the title; (4) II1's text landed, the first rows of registers arriving while it is read.""" def one(k, fr, about, draw): ox, oy = (k % 2) * 960, (k // 2) * 580 g.rect(ox, oy, 960, 40, RAISED) g.t(ox + 16, oy + 28, "%d" % (k + 1), 22, GOLD, "sansb") g.t(ox + 44, oy + 28, "f %s" % fr, 20, FAINT, "mono") g.t(ox + 52 + (len(fr) + 2) * 12, oy + 28, about, 20, VELLUM, "sansm") g.o.append(f'') g.rect(0, 0, 1920, 1080, PAGE) draw() g.o.append("") def first(): chapter_one_last(g) def second(): space(g) dump(g, DX, DTOP) place_tie(g) title(g, 2) clock(g, 2) def third(): # 204: folded to its tag, halfway along the shelf's height space(g) tx, ty = 64 + (DX - 64) * 0.5, 48 # risen to the shelf's height first, then along it w = 28 + tw("the 128 bytes", 20, "sansm") + 24 g.rect(tx, ty, w, TAG, RAISED) g.frame(tx, ty, w, TAG, RULE, 2) g.frame(tx + 8, ty + 6, 12, 12, FAINT, 2) g.t(tx + 28, ty + 18, "the 128 bytes", 20, DIM, "sansm") title(g, 2) clock(g, 2) def fourth(): # 312 + 3 rows' worth: II1 read, rax rbx rcx in, rdx arriving K.shelf(g, SHELF) space(g) rs = regs(38) y0 = 48 + TAG + PAD for i, (nm, links, end) in enumerate(rs[:4]): fade = 1.0 if i < 3 else 0.5 yy = y0 + i * ROW g.t(RX + 12, b(yy), nm, S, mix(DIM, PAGE, fade), MONO) if links: C.chain(g, RX + 72, yy, links, end, room=RX + CW - 12) else: C.integer(g, RX + 72, yy, end[1], mix(VELLUM, PAGE, fade)) below(g, [[("The CPU keeps sixteen words of its own, named, not",)], [("addressed: the registers.",)]]) clock(g, 2) for k, (fr, about, draw) in enumerate([("30", "I's last picture, its text read", first), ("96", "the title said; the label changes", second), ("204", "the 128 bytes, folded, slide to the shelf", third), ("356", "II1 landed, the registers arriving", fourth)]): one(k, fr, about, draw) g.rect(959, 0, 2, 1160, RULE) g.rect(0, 579, 1920, 2, RULE) def save(name_, draw, small=True, h=1080): gen.W, gen.H = 1920, h g = Svg() draw(g) g.save(name_) subprocess.run(["resvg", HERE + "/" + name_ + ".svg", HERE + "/" + name_ + ".png"], check=True) if small: subprocess.run(["resvg", "-w", "480", HERE + "/" + name_ + ".svg", HERE + "/" + name_ + "-480.png"], check=True) if __name__ == "__main__": save("58-regs-pins", beat_pins) save("59-regs-parts", beat_parts) save("60-regs-call", beat_call) save("61-regs-fold", beat_fold) save("62-seam", seam, small=False, h=1160)