---
title: "A computer of your own: the processor"
heading: Turning D10's single-cycle core into a processor that runs real compiled C, the instructions a C compiler actually emits, the memory map and calling convention the code assumes, little-endian memory and sign extension, and a Python core checked by running a prime sieve and a CRC
eyebrow: D13 · Lesson 11 · A computer of your own
xp: 40
---

::: hook
In D10 you built a single-cycle RISC-V core for a dozen instructions and a hand-written program. A computer runs other people's programs, which in practice means whatever a C compiler produces. This project takes the course's own C compiler, the one behind C12's board, compiles small programs with it, and builds the processor that runs them: first the core (this lesson), then its input and output (lesson 12), then a measurement of where its time goes (lesson 13).
:::

::: play
- The sieve of Eratosthenes in C, compiled by the course's compiler at -O1, running on the course's RISC-V simulator. Step it and watch the C line and the instruction it became.
- `print(i)` is a store to the address 0x2_0010, and `composite` is an array at 0x1000 in data memory: watch its bytes flip to 1 as the multiples are crossed out.
- Count the kinds of instruction in the listing: compiled C uses a small part of the instruction set, and that's all your core has to run.
:::

```sim cpu
c:
#include "board.h"

unsigned char composite[100];

int main(void) {
    int count = 0;
    for (int i = 2; i < 100; i++) {
        if (!composite[i]) {
            print(i);
            count++;
            for (int j = i + i; j < 100; j += i) composite[j] = 1;
        }
    }
    print(count);
    return 0;
}
asm:
    .text
_start:
    call main
    ebreak

main:
    li a0, 0                #@6
    li a1, 2                #@7
.L1:
    li t0, 100              #@7
    bge a1, t0, .L3         #@7
    la t0, composite        #@8
    add t0, t0, a1          #@8
    lbu t0, 0(t0)           #@8
    bnez t0, .L4            #@8
    lui t0, 0x20            #@9
    sw a1, 16(t0)           #@9
    addi a0, a0, 1          #@10
    add a2, a1, a1          #@11
.L5:
    li t0, 100              #@11
    bge a2, t0, .L7         #@11
    li t0, 1                #@11
    la t1, composite        #@11
    add t1, t1, a2          #@11
    sb t0, 0(t1)            #@11
.L6:
    add a2, a2, a1          #@11
    j .L5                   #@11
.L7:
.L4:
.L2:
    addi a1, a1, 1          #@7
    j .L1                   #@7
.L3:
    lui t0, 0x20            #@14
    sw a0, 16(t0)           #@14
    li a0, 0                #@15
.Lret_main:
    ret                     #@16

    .data
composite:
    .space 100
show: a0, a1, a2
hex: t0
memory: 0x1000..0x1064
out: on
speed: 200
```
::: predict
## The compiled sieve and CRC programs together use how many different RISC-V instructions?

- [ ] 4
- [x] About 15
- [ ] About 40, the whole base instruction set
- [ ] Over 100

::: reveal
The sieve uses 12 (add, addi, auipc, bge, bne, ebreak, jal, jalr, lbu, lui, sb, sw) and the CRC 15; with the Fibonacci program of lesson 12, 19 in all, under half of RV32I's 40 or so. Compilers lean on a few general instructions, which is the RISC idea D10 and C8 describe.
:::
:::

::: nameit
## What compiled C expects

- **Instructions:** the integer ALU operations with registers and immediates, loads and stores of bytes and words, conditional branches, `jal` and `jalr` for calls and returns, and `lui`/`auipc` to build addresses. `ebreak` ends the run.
- **Memory map:** program at 0, data at 0x1000, stack growing down from 0x1_0000, devices at 0x2_0000 (lesson 12).
- **Calling convention:** sp starts at 0x1_0000; `call f` is `auipc ra` + `jalr ra`, saving the return address; `ret` is `jalr x0, 0(ra)`; arguments and results go in a0 to a7.
- **Startup:** `_start: call main; ebreak`, so returning from `main` stops the machine with main's result in a0.

## Details that bite

- Memory is **little-endian**: the word 0x1234_5678 at address A has 0x78 at A and 0x12 at A + 3.
- Immediates are **sign-extended**: a 12-bit field of 0xFFF is −1, not 4095. Branch and jump offsets are relative to the instruction's own address.
- Registers hold 32-bit two's-complement values: wrap results to 32 bits, and compare unsigned (`bgeu`, `sltu`) on the value modulo $2^{32}$.
- `x0` reads as 0 whatever is written to it.

## Checking it

The course's simulator runs the same machine code, so your core must match it exactly: the same final memory, the same values written, the same instruction count.
:::

::: example
## What instruction is 0x00C0_80E7?

::: solution
1. **Opcode.** The low 7 bits are 0x67, `jalr`.
2. **Registers.** Bits 11 to 7 give rd = 1 (ra), and bits 19 to 15 give rs1 = 1 (ra).
3. **Immediate.** Bits 31 to 20 give the immediate 12, so the instruction is `jalr ra, 12(ra)`.
4. **Where it fits.** It's the second half of `call main`: the `auipc ra, 0` before it put 0 in ra, and this jumps to address 12 while saving 8 as the return address, the `ebreak`.

**Answer:** `jalr ra, 12(ra)`.
:::
:::

::: example
## How long does the sieve take on a single-cycle core at 50 MHz?

::: solution
1. **Cycles.** It executes 2339 instructions, one per cycle.
2. **Time at 50 MHz.**
   $$\begin{aligned} t &= \frac{2339}{50\ \text{MHz}} \\ &= 46.8\ \mu\text{s} \end{aligned}$$
3. **Next.** Lesson 13 asks how a pipelined core would do.

**Answer:** 46.8 µs.
:::
:::

::: check
kind: python

## Build a RISC-V core that runs the compiled sieve and CRC programs.

```python starter
MEM_SIZE = 0x30000
TEXT, DATA, STACK = 0x0, 0x1000, 0x10000

def sext(v, bits):
    # Sign-extend the low `bits` bits of v.
    v &= (1 << bits) - 1
    return v - (1 << bits) if v >> (bits - 1) else v

R_OPS = {(0, 0): 'add', (0, 0x20): 'sub', (1, 0): 'sll', (2, 0): 'slt', (3, 0): 'sltu',
         (4, 0): 'xor', (5, 0): 'srl', (5, 0x20): 'sra', (6, 0): 'or', (7, 0): 'and'}
I_OPS = {0: 'addi', 2: 'slti', 3: 'sltiu', 4: 'xori', 6: 'ori', 7: 'andi'}
LOADS = {0: 'lb', 1: 'lh', 2: 'lw', 4: 'lbu', 5: 'lhu'}
STORES = {0: 'sb', 1: 'sh', 2: 'sw'}
BRANCHES = {0: 'beq', 1: 'bne', 4: 'blt', 5: 'bge', 6: 'bltu', 7: 'bgeu'}

def decode(w):
    # One 32-bit instruction word to (name, rd, rs1, rs2, imm), with imm already sign-extended.
    op, rd, f3 = w & 0x7F, (w >> 7) & 31, (w >> 12) & 7
    rs1, rs2, f7 = (w >> 15) & 31, (w >> 20) & 31, w >> 25
    if op == 0x33:
        return R_OPS[(f3, f7)], rd, rs1, rs2, 0
    if op == 0x13:
        if f3 == 1:
            return 'slli', rd, rs1, 0, rs2
        if f3 == 5:
            return ('srai' if f7 else 'srli'), rd, rs1, 0, rs2
        return I_OPS[f3], rd, rs1, 0, sext(w >> 20, 12)
    if op == 0x03:
        return LOADS[f3], rd, rs1, 0, sext(w >> 20, 12)
    if op == 0x23:
        return STORES[f3], 0, rs1, rs2, sext((f7 << 5) | rd, 12)
    if op == 0x63:
        imm = ((w >> 31) << 12) | (((w >> 7) & 1) << 11) | (((w >> 25) & 63) << 5) | (((w >> 8) & 15) << 1)
        return BRANCHES[f3], 0, rs1, rs2, sext(imm, 13)
    if op == 0x37:
        return 'lui', rd, 0, 0, sext(w & 0xFFFFF000, 32)
    if op == 0x17:
        return 'auipc', rd, 0, 0, sext(w & 0xFFFFF000, 32)
    if op == 0x6F:
        imm = ((w >> 31) << 20) | (((w >> 12) & 255) << 12) | (((w >> 20) & 1) << 11) | (((w >> 21) & 1023) << 1)
        return 'jal', rd, 0, 0, sext(imm, 21)
    if op == 0x67:
        return 'jalr', rd, rs1, 0, sext(w >> 20, 12)
    if w == 0x00100073:
        return 'ebreak', 0, 0, 0, 0
    raise ValueError(f'unknown instruction {w:#010x}')

# The sieve of Eratosthenes and CRC-16 programs from this lesson, compiled by the course's C
# compiler (-O1). The sieve's array starts zeroed at DATA; the CRC's data is its message.
SIEVE = [
    0x00000097, 0x00c080e7, 0x00100073, 0x00000513, 0x00200593, 0x06400293,
    0x0455da63, 0x00001297, 0xfe428293, 0x00b282b3, 0x0002c283, 0x02029c63,
    0x000202b7, 0x00b2a823, 0x00150513, 0x00b58633, 0x06400293, 0x02565063,
    0x00100293, 0x00001317, 0xfb430313, 0x00c30333, 0x00530023, 0x00b60633,
    0xfe1ff06f, 0x00158593, 0xfadff06f, 0x000202b7, 0x00a2a823, 0x00000513,
    0x00008067,
]
SIEVE_DATA = bytes(100)
CRC = [
    0x00000097, 0x080080e7, 0x00100073, 0x000105b7, 0xfff58593, 0x00054283,
    0x06028063, 0x00050293, 0x00150513, 0x0002c283, 0x00829293, 0x0055c5b3,
    0x00000613, 0x00800293, 0x02565e63, 0x000082b7, 0x0055f2b3, 0x00028c63,
    0x00159293, 0x00001337, 0x02130313, 0x0062c5b3, 0x0080006f, 0x00159593,
    0x000102b7, 0xfff28293, 0x0055f5b3, 0x00160613, 0xfc5ff06f, 0xfa1ff06f,
    0x00058513, 0x00008067, 0xff010113, 0x00112623, 0x00912423, 0x00001517,
    0xf7450513, 0x00000097, 0xf78080e7, 0x00050493, 0x000202b7, 0x0092a823,
    0x0ff4f293, 0x00020337, 0x00532023, 0x00000513, 0x00c12083, 0x00812483,
    0x01010113, 0x00008067,
]
CRC_DATA = b'123456789\x00'

class CPU:
    def __init__(self, words, data=b''):
        # Memory is a bytearray of MEM_SIZE bytes: the words at TEXT (little-endian), the data
        # at DATA. Registers x[0..31] start at 0 except x[2] (sp) = STACK; pc starts at TEXT;
        # count (instructions executed) at 0.
        self.mem = bytearray(MEM_SIZE)
        self.x = [0]*32
        self.pc = TEXT
        self.count = 0

    def load(self, addr, n):
        # n bytes from addr, little-endian, as an unsigned number.
        return 0

    def store(self, addr, n, value):
        # The low n bytes of value to addr, little-endian.
        pass

    def step(self):
        # Fetch the word at pc, decode it and execute it. The programs use add, addi, and,
        # andi, auipc, beq, bge, bgeu, bne, ebreak, jal, jalr, lbu, lui, lw, sb, slli, sw and
        # xor (add the rest of RV32I if you like). Keep x[0] at 0 and registers as signed
        # 32-bit values (sext(v, 32)); compare unsigned with v & 0xFFFFFFFF. Count every
        # instruction, ebreak included; return False after an ebreak, True otherwise.
        return False

    def run(self, limit=1000000):
        while self.count < limit and self.step():
            pass
        return self.count
```

```python solution
MEM_SIZE = 0x30000
TEXT, DATA, STACK = 0x0, 0x1000, 0x10000

def sext(v, bits):
    # Sign-extend the low `bits` bits of v.
    v &= (1 << bits) - 1
    return v - (1 << bits) if v >> (bits - 1) else v

R_OPS = {(0, 0): 'add', (0, 0x20): 'sub', (1, 0): 'sll', (2, 0): 'slt', (3, 0): 'sltu',
         (4, 0): 'xor', (5, 0): 'srl', (5, 0x20): 'sra', (6, 0): 'or', (7, 0): 'and'}
I_OPS = {0: 'addi', 2: 'slti', 3: 'sltiu', 4: 'xori', 6: 'ori', 7: 'andi'}
LOADS = {0: 'lb', 1: 'lh', 2: 'lw', 4: 'lbu', 5: 'lhu'}
STORES = {0: 'sb', 1: 'sh', 2: 'sw'}
BRANCHES = {0: 'beq', 1: 'bne', 4: 'blt', 5: 'bge', 6: 'bltu', 7: 'bgeu'}

def decode(w):
    # One 32-bit instruction word to (name, rd, rs1, rs2, imm), with imm already sign-extended.
    op, rd, f3 = w & 0x7F, (w >> 7) & 31, (w >> 12) & 7
    rs1, rs2, f7 = (w >> 15) & 31, (w >> 20) & 31, w >> 25
    if op == 0x33:
        return R_OPS[(f3, f7)], rd, rs1, rs2, 0
    if op == 0x13:
        if f3 == 1:
            return 'slli', rd, rs1, 0, rs2
        if f3 == 5:
            return ('srai' if f7 else 'srli'), rd, rs1, 0, rs2
        return I_OPS[f3], rd, rs1, 0, sext(w >> 20, 12)
    if op == 0x03:
        return LOADS[f3], rd, rs1, 0, sext(w >> 20, 12)
    if op == 0x23:
        return STORES[f3], 0, rs1, rs2, sext((f7 << 5) | rd, 12)
    if op == 0x63:
        imm = ((w >> 31) << 12) | (((w >> 7) & 1) << 11) | (((w >> 25) & 63) << 5) | (((w >> 8) & 15) << 1)
        return BRANCHES[f3], 0, rs1, rs2, sext(imm, 13)
    if op == 0x37:
        return 'lui', rd, 0, 0, sext(w & 0xFFFFF000, 32)
    if op == 0x17:
        return 'auipc', rd, 0, 0, sext(w & 0xFFFFF000, 32)
    if op == 0x6F:
        imm = ((w >> 31) << 20) | (((w >> 12) & 255) << 12) | (((w >> 20) & 1) << 11) | (((w >> 21) & 1023) << 1)
        return 'jal', rd, 0, 0, sext(imm, 21)
    if op == 0x67:
        return 'jalr', rd, rs1, 0, sext(w >> 20, 12)
    if w == 0x00100073:
        return 'ebreak', 0, 0, 0, 0
    raise ValueError(f'unknown instruction {w:#010x}')

# The sieve of Eratosthenes and CRC-16 programs from this lesson, compiled by the course's C
# compiler (-O1). The sieve's array starts zeroed at DATA; the CRC's data is its message.
SIEVE = [
    0x00000097, 0x00c080e7, 0x00100073, 0x00000513, 0x00200593, 0x06400293,
    0x0455da63, 0x00001297, 0xfe428293, 0x00b282b3, 0x0002c283, 0x02029c63,
    0x000202b7, 0x00b2a823, 0x00150513, 0x00b58633, 0x06400293, 0x02565063,
    0x00100293, 0x00001317, 0xfb430313, 0x00c30333, 0x00530023, 0x00b60633,
    0xfe1ff06f, 0x00158593, 0xfadff06f, 0x000202b7, 0x00a2a823, 0x00000513,
    0x00008067,
]
SIEVE_DATA = bytes(100)
CRC = [
    0x00000097, 0x080080e7, 0x00100073, 0x000105b7, 0xfff58593, 0x00054283,
    0x06028063, 0x00050293, 0x00150513, 0x0002c283, 0x00829293, 0x0055c5b3,
    0x00000613, 0x00800293, 0x02565e63, 0x000082b7, 0x0055f2b3, 0x00028c63,
    0x00159293, 0x00001337, 0x02130313, 0x0062c5b3, 0x0080006f, 0x00159593,
    0x000102b7, 0xfff28293, 0x0055f5b3, 0x00160613, 0xfc5ff06f, 0xfa1ff06f,
    0x00058513, 0x00008067, 0xff010113, 0x00112623, 0x00912423, 0x00001517,
    0xf7450513, 0x00000097, 0xf78080e7, 0x00050493, 0x000202b7, 0x0092a823,
    0x0ff4f293, 0x00020337, 0x00532023, 0x00000513, 0x00c12083, 0x00812483,
    0x01010113, 0x00008067,
]
CRC_DATA = b'123456789\x00'

class CPU:
    def __init__(self, words, data=b''):
        self.mem = bytearray(MEM_SIZE)
        for k, w in enumerate(words):
            self.mem[TEXT + 4*k:TEXT + 4*k + 4] = (w & 0xFFFFFFFF).to_bytes(4, 'little')
        self.mem[DATA:DATA + len(data)] = bytes(data)
        self.x = [0]*32
        self.x[2] = STACK
        self.pc = TEXT
        self.count = 0

    def load(self, addr, n):
        return int.from_bytes(self.mem[addr:addr + n], 'little')

    def store(self, addr, n, value):
        self.mem[addr:addr + n] = (value & ((1 << 8*n) - 1)).to_bytes(n, 'little')

    def step(self):
        name, rd, rs1, rs2, imm = decode(self.load(self.pc, 4))
        a, b = self.x[rs1], self.x[rs2]
        u = lambda v: v & 0xFFFFFFFF
        nxt, val = self.pc + 4, None
        if name == 'ebreak':
            self.count += 1
            return False
        elif name == 'add': val = a + b
        elif name == 'sub': val = a - b
        elif name == 'and': val = a & b
        elif name == 'or': val = a | b
        elif name == 'xor': val = a ^ b
        elif name == 'sll': val = a << (b & 31)
        elif name == 'srl': val = u(a) >> (b & 31)
        elif name == 'sra': val = a >> (b & 31)
        elif name == 'slt': val = int(a < b)
        elif name == 'sltu': val = int(u(a) < u(b))
        elif name == 'addi': val = a + imm
        elif name == 'andi': val = a & imm
        elif name == 'ori': val = a | imm
        elif name == 'xori': val = a ^ imm
        elif name == 'slti': val = int(a < imm)
        elif name == 'sltiu': val = int(u(a) < u(imm))
        elif name == 'slli': val = a << imm
        elif name == 'srli': val = u(a) >> imm
        elif name == 'srai': val = a >> imm
        elif name == 'lui': val = imm
        elif name == 'auipc': val = self.pc + imm
        elif name in ('lb', 'lh', 'lw', 'lbu', 'lhu'):
            n = {'lb': 1, 'lh': 2, 'lw': 4, 'lbu': 1, 'lhu': 2}[name]
            val = self.load(u(a + imm), n)
            if name in ('lb', 'lh', 'lw'):
                val = sext(val, 8*n)
        elif name in ('sb', 'sh', 'sw'):
            self.store(u(a + imm), {'sb': 1, 'sh': 2, 'sw': 4}[name], b)
        elif name in ('beq', 'bne', 'blt', 'bge', 'bltu', 'bgeu'):
            taken = {'beq': a == b, 'bne': a != b, 'blt': a < b, 'bge': a >= b,
                     'bltu': u(a) < u(b), 'bgeu': u(a) >= u(b)}[name]
            if taken:
                nxt = self.pc + imm
        elif name == 'jal':
            val, nxt = self.pc + 4, self.pc + imm
        elif name == 'jalr':
            val, nxt = self.pc + 4, (a + imm) & ~1
        if val is not None and rd:
            self.x[rd] = sext(val, 32)
        self.pc = u(nxt)
        self.count += 1
        return True

    def run(self, limit=1000000):
        while self.count < limit and self.step():
            pass
        return self.count
```

```python tests
_c = CPU([0x00500093, 0xffd00113, 0x002081b3, 0x00100073])
_c.run()
assert _c.x[1] == 5 and _c.x[2] == -3 and _c.x[3] == 2 and _c.count == 4, "addi x1, x0, 5; addi x2, x0, -3; add x3, x1, x2; ebreak should leave 5, -3 and 2, in 4 instructions."
_c.store(0x1000, 4, 0x12345678)
assert _c.mem[0x1000] == 0x78 and _c.load(0x1001, 2) == 0x3456, "Memory is little-endian."
_s = CPU(SIEVE, SIEVE_DATA)
_n = _s.run()
assert _n == 2339, f"The sieve runs 2339 instructions to its ebreak; got {_n}."
_primes = [p for p in range(2, 100) if all(p % q for q in range(2, p))]
assert [k for k in range(2, 100) if _s.mem[DATA + k] == 0] == _primes, "The array holds 0 exactly at the primes below 100."
assert _s.load(0x20010, 4) == 25 and _s.x[10] == 0, "The last value stored to 0x20010 is the count, 25, and main returns 0 in a0."
_r = CPU(CRC, CRC_DATA)
assert _r.run() == 1042 and _r.load(0x20010, 4) == 0x29B1, f"The CRC program computes 0x29B1 in 1042 instructions; got {_r.load(0x20010, 4):#06x} after {_r.count}."
```

::: right
Right. Your core runs compiled C exactly as the course's simulator does: the same primes, the same CRC and the same instruction counts.
:::
:::

::: check
given: W = 0x12345678 | 0xDEADBEEF | 0x00C080E7; k = 0 | 1 | 3
answer: floor(W/256^k) - 256*floor(W/256^(k + 1))
base: 16
tolerance: 0

## The word {W:hex8} is stored at address A in little-endian memory. What byte is at A + {k}?

::: right
Right. Byte k is bits 8k to 8k + 7: {answer:hex2}.
:::

::: misses
- [floor(W/256^(3 - k)) - 256*floor(W/256^(4 - k))] That's big-endian order; RISC-V puts the low byte first.
- [any] Not yet. The low byte comes first.
:::
:::

::: check
given: v = 0xFFF | 0x800 | 0xFF6 | 0xC00 | 0x9C4
answer: v - 4096*step(v - 2047.5)
tolerance: 0.4

## A 12-bit immediate field holds {v:hex3}. What value does the instruction use?

::: right
Right. Sign-extended, it's {answer}.
:::

::: misses
- [v] Immediates are signed: a top bit of 1 makes the field negative.
- [any] Not yet. Subtract 4096 if bit 11 is set.
:::
:::

::: check
given: N = 1042 | 2339 | 3636; f = 10 | 50 | 100
answer: N/f
unit: µs
tolerance: 0.5%

## A single-cycle core at {f} MHz runs a program of {N} instructions. How long does it take?

::: right
Right. {N} cycles at {f} MHz: {answer} µs.
:::

::: misses
- [any] Not yet. One instruction per cycle.
:::
:::

::: check
kind: parts
given: a = 50 | 60; l = 20 | 25; b = 15 | 10; f = 25 | 50 | 100

## A multicycle RISC-V core takes 4 cycles for ALU instructions, 5 for loads and 3 for branches and stores. A program is {a}% ALU, {l}% loads and the rest branches and stores, and runs at {f} MHz.

::: part
answer: (4*a + 5*l + 3*(100 - a - l))/100
tolerance: 0.5%

### What is the average CPI?

::: misses
- [any] Not yet. $\sum f_i \cdot \text{CPI}_i$.
:::
:::

::: part
answer: f/((4*a + 5*l + 3*(100 - a - l))/100)
unit: MHz
tolerance: 0.5%

### How many million instructions per second is that?

::: misses
- [f*((4*a + 5*l + 3*(100 - a - l))/100)] Divide the clock by the CPI.
- [any] Not yet. $f/\text{CPI}$.
:::
:::

::: right
Right. Pipelining aims at a CPI near 1, the subject of lesson 13.
:::
:::

::: check
given: n = 1000 | 50000; c = 1.2 | 3.6; f = 10 | 50
answer: n*c/(f*1e6)*1e3
unit: ms
tolerance: 0.5%

## A program runs {n} instructions at CPI {c} on a {f} MHz core. How long does it take?

::: right
Right. $n \times \text{CPI}/f$ = {answer}.
:::

::: misses
- [n/(f*1e6)*1e3] Multiply by the CPI.
- [any] Not yet. Instructions × CPI ÷ clock.
:::
:::

::: explain
## Why does a processor that runs compiled C need a stack pointer set up before main starts?

::: model
Compiled C keeps function return addresses, saved registers and local variables that don't fit in registers on the stack, addressing them relative to sp. The very first thing main or any function may do is subtract from sp and store there. If sp started at 0 or some arbitrary value, those stores would land on top of the program or the data, or outside memory altogether, and the program would corrupt itself. Starting sp at the top of a reserved region of RAM, 0x1_0000 here, with the stack growing down toward the data, gives every function call a safe place to work, which is part of the contract between the compiler and the machine.
:::
:::

::: practice

::: check
given: W = 0xCAFEF00D | 0x0BADC0DE; k = 0 | 1 | 2 | 3
answer: floor(W/256^k) - 256*floor(W/256^(k + 1))
base: 16
tolerance: 0

## Byte {k} of {W:hex8}, little-endian?

::: misses
- [any] Byte k holds bits 8k to 8k + 7.
:::
:::

::: check
given: v = 0x001 | 0x400 | 0x801 | 0xC00 | 0xFFE
answer: v - 4096*step(v - 2047.5)
tolerance: 0.4

## Sign-extend the 12-bit field {v:hex3}?

::: misses
- [any] Subtract 4096 when bit 11 is set.
:::
:::
:::
