A Virtual Machine

The Problem

  • Still a lot of magic in the interpreter of [%x interp %]

  • Build something that mimics hardware more closely

  • See [%b Nisan2021 %] for a fuller description

  • Or play [Human Resource Machine][human_resource_machine]

Architecture

  • Our [%g virtual_machine “virtual machine” %] (VM)

[% figure slug=“vm-architecture” img=“architecture.svg” alt=“Virtual machine architecture” caption=“Architecture of the virtual machine.” %]

Architecture

  1. [%g instruction_pointer “Instruction pointer” %] (IP) holds the address of the next instruction

    • Automatically initialized to 0, so every program must start there
  2. Instructions can access [%g register_hardware “registers” %] R0 to R3 directly

    • No memory-to-memory operations
  3. 256 [%g word_memory “words” %] of memory

    • Addresses fit in a single byte

What It Can Do

  • [%g instruction_set “Instruction set” %] defines what the processor can do

  • Each instruction is 3 bytes

  • [%g op_code “op code” %] fits in one byte

  • May have zero, one, or two single-byte operands

    • Could use variable-length instructions, but that would complicate the VM
  • Each operand is a register identifier, a constant, or an address

Writing Instructions

  • Instructions are numbers,

  • Write them in [%g assembly_code “assembly code” %] for readability

Name Code Format Action Example
hlt 1 – Halt program hlt
ldc 2 rv Load constant ldc R0 123
ldr 3 rr Load register ldr R0 R1
add 6 rr Add add R0 R1
bne 9 rv Branch if not equal bne R0 123
prr 10 r- Print register prr R0

Details

[%inc architecture.py %]

VM Class

[%inc vm.py mark=init %]

Fetching and Unpacking

[%inc vm.py mark=fetch %]

  • Use [%g bitwise_operation “bitwise operations” %] to get bytes out of integer

[% figure slug=“vm-unpacking” img=“unpacking.svg” alt=“Unpacking instructions” caption=“Using bitwise operations to unpack instructions.” %]

Running a Program

[%inc vm.py mark=run omit=skip %]

Sample Instructions

  • Store a value

[%inc vm.py mark=store %]

  • Addition

[%inc vm.py mark=add %]

  • [%g conditional_jump “Conditional jump” %]

[%inc vm.py mark=beq %]

Writing Programs

  • Hexadecimal

[%inc print_r1.mx %]

  • Assembly code

[%inc print_r1.as %]

  • Write an [%g assembler “assembler” %] to the latter into the former

Labels

  • Instruction set doesn’t have names for addresses

  • But we want [%g label_address “labels” %] for readability

[%inc count_up.as %]

Counting Up

[%inc count_up.out %]

Counting Up

[% figure slug=“vm-count-up” img=“count_up.svg” alt=“Counting from 0 to 2” caption=“Flowchart of assembly language program to count up from 0 to 2.” %]

Counting Up

[% figure slug=“vm-count-trace” img=“count_trace.svg” alt=“Trace counting program” caption=“Tracing registers and memory values for a simple counting program.” %]

Assembler Class

[%inc assembler.py mark=class %]

Resolving Labels

[%inc assembler.py mark=labels %]

  • Either save label or increment current address

Compiling an Instruction

[%inc assembler.py mark=compile %]

Converting a Value

  • Look up label’s address if value starts with @

  • Convert value to number if it doesn’t

[%inc assembler.py mark=value %]

Summary

[% figure slug=“vm-concept-map” img=“concept_map.svg” alt=“Concept map for virtual machine and assembler” caption=“Concept map.” %]