//: Running SubX programs on the VM. //: (Not to be confused with the 'run' subcommand for running ELF binaries on //: the VM. That comes later.) :(before "End Help Texts") put_new(Help, "syntax", "SubX programs consist of segments, each segment in turn consisting of lines.\n" "Line-endings are significant; each line should contain a single\n" "instruction, macro or directive.\n" "\n" "Comments start with the '#' character. It should be at the start of a word\n" "(start of line, or following a space).\n" "\n" "Each segment starts with a header line: a '==' delimiter followed by the name of\n" "the segment and a (sometimes approximate) starting address in memory.\n" "The name 'code' is special; instructions to execute should always go here.\n" "\n" "The resulting binary starts running code from a label called 'Entry'\n" "in the code segment.\n" "\n" "Segments with the same name get merged together. This rule helps keep functions\n" "and their data close together in .subx files.\n" "You don't have to specify the starting address after the first time.\n" "\n" "Lines consist of a series of words. Words can contain arbitrary metadata\n" "after a '/', but they can never contain whitespace. Metadata has no effect\n" "at runtime, but can be handy when rewriting macros.\n" "\n" "Check out the example programs in the apps/ directory, particularly apps/ex*.\n" ); :(before "End Help Contents") cerr << " syntax\n"; :(code) void test_copy_imm32_to_EAX() { // At the lowest level, SubX programs are a series of hex bytes, each // (variable-length) instruction on one line. run( // Comments start with '#' and are ignored. "# comment\n" // Segment headers start with '==', a name and a starting hex address. // There's usually one code and one data segment. The code segment // always comes first. "== code 0x1\n" // code segment // After the header, each segment consists of lines, and each line // consists of words separated by whitespace. // // All words can have metadata after a '/'. No spaces allowed in // metadata, of course. // Unrecognized metadata never causes errors, so you can use it for // documentation. // // Within the code segment in particular, x86 instructions consist of // some number of the following parts and sub-parts (see the Readme and // cheatsheet.pdf for details): // opcodes: 1-3 bytes // ModR/M byte // SIB byte // displacement: 0/1/2/4 bytes // immediate: 0/1/2/4 bytes // opcode ModR/M SIB displacement immediate // instruction mod, reg, Reg/Mem bits scale, index, base // 1-3 bytes 0/1 byte 0/1 byte 0/1/2/4 bytes 0/1/2/4 bytes " b8 . . . 0a 0b 0c 0d\n" // copy 0x0d0c0b0a to EAX // The periods are just to help the eye track long gaps between columns, // and are otherwise ignored. ); // This program, when run, causes the following events in the trace: CHECK_TRACE_CONTENTS( "load: 0x00000001 -> b8\n" "load: 0x00000002 -> 0a\n" "load: 0x00000003 -> 0b\n" "load: 0x00000004 -> 0c\n" "load: 0x00000005 -> 0d\n" "run: copy imm32 0x0d0c0b0a to EAX\n" ); } // top-level helper for tests: parse the input, load the hex bytes into memory, run void run(const string& text_bytes) { program p; istringstream in(text_bytes); // Loading Test Program parse(in, p); if (trace_contains_errors()) return; // if any stage raises errors, stop immediately // Running Test Program load(p); if (trace_contains_errors()) return; // convenience to keep tests concise: 'Entry' label need not be provided // not allowed in real programs if (p.entry)
# example program: reading a URL over HTTP

def main [
  local-scope
  $print [aaa] 10/newline
  google:&:source:char <- start-reading-from-network null/real-resources, [google.com/]
  $print [bbb] 10/newline
  n:num <- copy 0
  buf:&:buffer:char <- new-buffer 30
  {
    c:char, done?:bool <- read google
    break-if done?
    n <- add