https://github.com/akkartik/mu/blob/main/shell/global.mu
  1 type global-table {
  2   data: (handle array global)
  3   final-index: int
  4   render-list: (handle array int)  # sequence of globals to render on the left side
  5                                    # no pagination or scrolling
  6 }
  7 
  8 type global {
  9   name: (handle array byte)
 10   input: (handle gap-buffer)
 11   value: (handle cell)
 12   trace: (handle trace)
 13 }
 14 
 15 fn initialize-globals _self: (addr global-table) {
 16   var self/esi: (addr global-table) <- copy _self
 17   compare self, 0
 18   {
 19     break-if-!=
 20     abort "initialize globals"
 21     return
 22   }
 23   var data-ah/eax: (addr handle array global) <- get self, data
 24   populate data-ah, 0x80
 25   initialize-primitives self
 26   var render-ah/eax: (addr handle array int) <- get self, render-list
 27   populate render-ah, 0x20/render-size
 28 }
 29 
 30 fn load-globals in: (addr handle cell), self: (addr global-table) {
 31   draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, "loading globals:", 3/fg, 0/bg
 32   var remaining-ah/esi: (addr handle cell) <- copy in
 33   {
 34     var _remaining/eax: (addr cell) <- lookup *remaining-ah
 35     var remaining/ebx: (addr cell) <- copy _remaining
 36     var done?/eax: boolean <- nil? remaining
 37     compare done?, 0/false
 38     break-if-!=
 39     var curr-ah/eax: (addr handle cell) <- get remaining, left
 40     var _curr/eax: (addr cell) <- lookup *curr-ah
 41     var curr/ecx: (addr cell) <- copy _curr
 42     remaining-ah <- get remaining, right
 43     var name-ah/eax: (addr handle cell) <- get curr, left
 44     var name/eax: (addr cell) <- lookup *name-ah
 45     var name-data-ah/eax: (addr handle stream byte) <- get name, text-data
 46     var _name-data/eax: (addr stream byte) <- lookup *name-data-ah
 47     var name-data/edx: (addr stream byte) <- copy _name-data
 48     rewind-stream name-data
 49     draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, " ", 3/fg 0/bg
 50     draw-stream-wrapping-right-then-down-from-cursor-over-full-screen 0/screen, name-data, 3/fg, 0/bg
 51     var value-ah/eax: (addr handle cell) <- get curr, right
 52     var value/eax: (addr cell) <- lookup *value-ah
 53     var value-data-ah/eax: (addr handle stream byte) <- get value, text-data
 54     var _value-data/eax: (addr stream byte) <- lookup *value-data-ah
 55     var value-data/ecx: (addr stream byte) <- copy _value-data
 56     var value-gap-buffer-storage: (handle gap-buffer)
 57     var value-gap-buffer-ah/edi: (addr handle gap-buffer) <- address value-gap-buffer-storage
 58     allocate value-gap-buffer-ah
 59     var value-gap-buffer/eax: (addr gap-buffer) <- lookup *value-gap-buffer-ah
 60     initialize-gap-buffer value-gap-buffer, 0x40000/256KB
 61     load-gap-buffer-from-stream value-gap-buffer, value-data
 62     load-lexical-scope value-gap-buffer-ah, self
 63     loop
 64   }
 65   move-cursor-to-left-margin-of-next-line 0/screen
 66 }
 67 
 68 fn write-globals out: (addr stream byte), _self: (addr global-table) {
 69   var self/esi: (addr global-table) <- copy _self
 70   compare self, 0
 71   {
 72     break-if-!=
 73     abort "write globals"
 74     return
 75   }
 76   write out, "  (globals . (\n"
 77   var data-ah/eax: (addr handle array global) <- get self, data
 78   var data/eax: (addr array global) <- lookup *data-ah
 79   var final-index/edx: (addr int) <- get self, final-index
 80   var curr-index/ecx: int <- copy 1/skip-0
 81   {
 82     compare curr-index, *final-index
 83     break-if->
 84     var curr-offset/ebx: (offset global) <- compute-offset data, curr-index
 85     var curr/ebx: (addr global) <- index data, curr-offset
 86     var curr-value-ah/edx: (addr handle cell) <- get curr, value
 87     var curr-value/eax: (addr cell) <- lookup *curr-value-ah
 88     var curr-type/eax: (addr int) <- get curr-value, type
 89     {
 90       compare *curr-type, 4/primitive-function
 91       break-if-=
 92       compare *curr-type, 5/screen
 93       break-if-=
 94       compare *curr-type, 6/keyboard
 95       break-if-=
 96       write out, "    ("
 97       var curr-name-ah/eax: (addr handle array byte) <- get curr, name
 98       var curr-name/eax: (addr array byte) <- lookup *curr-name-ah
 99       write out, curr-name
100       write out, " . ["
101       var curr-input-ah/eax: (addr handle gap-buffer) <- get curr, input
102       var curr-input/eax: (addr gap-buffer) <- lookup *curr-input-ah
103       {
104         compare curr-input, 0
105         break-if-!=
106         abort "null gap buffer"
107       }
108       append-gap-buffer curr-input, out
109       write out, "])\n"
110     }
111     curr-index <- increment
112     loop
113   }
114   write out, "  ))\n"
115 }
116 
117 # globals layout: 1 char padding, 41 code, 1 padding, 41 code, 1 padding = 85 chars
118 fn render-globals screen: (addr screen), _self: (addr global-table), show-cursor?: boolean {
119   clear-rect screen, 0/xmin, 0/ymin, 0x55/xmax, 0x2f/ymax=screen-height-without-menu, 0xdc/bg=green-bg
120   var self/esi: (addr global-table) <- copy _self
121   compare self, 0
122   {
123     break-if-!=
124     abort "render globals"
125     return
126   }
127   var data-ah/eax: (addr handle array global) <- get self, data
128   var _data/eax: (addr array global) <- lookup *data-ah
129   var data/ebx: (addr array global) <- copy _data
130   var curr-index/edx: int <- copy 0
131   var y1: int
132   copy-to y1, 1/padding-top
133   var y2: int
134   copy-to y2, 1/padding-top
135   $render-globals:loop: {
136     compare curr-index, 0x20/render-size
137     break-if->=
138     {
139       compare y1, 0x2f/ymax
140       break-if-<
141       compare y2, 0x2f/ymax
142       break-if-<
143       break $render-globals:loop
144     }
145     $render-globals:iter: {
146       var cursor-in-current-line?: boolean
147       {
148         compare show-cursor?, 0/false
149         break-if-=
150         compare curr-index, 0
151         break-if-!=
152         copy-to cursor-in-current-line?, 1/true
153       }
154       var render-list-ah/eax: (addr handle array int) <- get self, render-list
155       var render-list/eax: (addr array int) <- lookup *render-list-ah
156       var global-id-a/eax: (addr int) <- index render-list, curr-index
157       var global-id/eax: int <- copy *global-id-a
158       compare global-id, 0
159       break-if-=
160       var global-offset/edx: (offset global) <- compute-offset data, global-id
161       var curr/edx: (addr global) <- index data, global-offset
162       {
163         var render-image?/eax: boolean <- render-image? curr, cursor-in-current-line?
164         compare render-image?, 0/false
165         break-if-=
166         var y/ecx: int <- render-image-definition screen, curr, 0/x y1
167         y <- increment  # padding
168         copy-to y1, y
169         break $render-globals:iter
170       }
171       var curr-input-ah/eax: (addr handle gap-buffer) <- get curr, input
172       var _curr-input/eax: (addr gap-buffer) <- lookup *curr-input-ah
173       var curr-input/ebx: (addr gap-buffer) <- copy _curr-input
174       compare curr-input, 0
175       break-if-=
176       var curr-trace-ah/eax: (addr handle trace) <- get curr, trace
177       var _curr-trace/eax: (addr trace) <- lookup *curr-trace-ah
178       var curr-trace/edx: (addr trace) <- copy _curr-trace
179       $render-globals:render-global: {
180         var x/eax: int <- copy 0
181         var y/ecx: int <- copy y1
182         compare y, y2
183         {
184           break-if->=
185           x, y <- render-gap-buffer-wrapping-right-then-down screen, curr-input, 1/padding-left, y1, 0x2a/xmax, 0x2f/ymax, cursor-in-current-line?, 7/fg=definition, 0xc5/bg=blue-bg
186           y <- increment
187           y <- render-trace screen, curr-trace, 1/padding-left, y, 0x2a/xmax, 0x2f/ymax, 0/no-cursor
188           y <- increment
189           copy-to y1, y
190           break $render-globals:render-global
191         }
192         x, y <- render-gap-buffer-wrapping-right-then-down screen, curr-input, 0x2b/xmin, y2, 0x54/xmax, 0x2f/ymax, cursor-in-current-line?, 7/fg=definition, 0xc5/bg=blue-bg
193         y <- increment
194         y <- render-trace screen, curr-trace, 0x2b/xmin, y, 0x54/xmax, 0x2f/ymax, 0/no-cursor
195         y <- increment
196         copy-to y2, y
197       }
198     }
199     curr-index <- increment
200     loop
201   }
202   # render primitives on top
203   render-primitives screen, 1/xmin=padding-left, 0x55/xmax, 0x2f/ymax
204 }
205 
206 fn render-globals-menu screen: (addr screen), _self: (addr global-table) {
207   var _width/eax: int <- copy 0
208   var height/ecx: int <- copy 0
209   _width, height <- screen-size screen
210   var width/edx: int <- copy _width
211   var y/ecx: int <- copy height
212   y <- decrement
213   var height/ebx: int <- copy y
214   height <- increment
215   clear-rect screen, 0/x, y, width, height, 0xc5/bg=blue-bg
216   set-cursor-position screen, 0/x, y
217   draw-text-rightward-from-cursor screen, " ^r ", width, 0/fg, 0x5c/bg=menu-highlight
218   draw-text-rightward-from-cursor screen, " run main  ", width, 7/fg, 0xc5/bg=blue-bg
219   draw-text-rightward-from-cursor screen, " ^s ", width, 0/fg, 0x5c/bg=menu-highlight
220   draw-text-rightward-from-cursor screen, " run sandbox  ", width, 7/fg, 0xc5/bg=blue-bg
221   draw-text-rightward-from-cursor screen, " ^g ", width, 0/fg, 0x5c/bg=menu-highlight
222   draw-text-rightward-from-cursor screen, " go to  ", width, 7/fg, 0xc5/bg=blue-bg
223   draw-text-rightward-from-cursor screen, " ^a ", width, 0/fg, 0x5c/bg=menu-highlight
224   draw-text-rightward-from-cursor screen, " <<  ", width, 7/fg, 0xc5/bg=blue-bg
225   draw-text-rightward-from-cursor screen, " ^b ", width, 0/fg, 0x5c/bg=menu-highlight
226   draw-text-rightward-from-cursor screen, " <word  ", width, 7/fg, 0xc5/bg=blue-bg
227   draw-text-rightward-from-cursor screen, " ^f ", width, 0/fg, 0x5c/bg=menu-highlight
228   draw-text-rightward-from-cursor screen, " word>  ", width, 7/fg, 0xc5/bg=blue-bg
229   draw-text-rightward-from-cursor screen, " ^e ", width, 0/fg, 0x5c/bg=menu-highlight
230   draw-text-rightward-from-cursor screen, " >>  ", width, 7/fg, 0xc5/bg=blue-bg
231 }
232 
233 fn edit-globals _self: (addr global-table), key: code-point-utf8 {
234   var self/esi: (addr global-table) <- copy _self
235   # ctrl-s
236   {
237     compare key, 0x13/ctrl-s
238     break-if-!=
239     #
240     refresh-cursor-definition self
241     return
242   }
243   var cursor-index/ecx: int <- cursor-global self
244   compare cursor-index, 0
245   {
246     break-if-!=
247     return
248   }
249   var data-ah/eax: (addr handle array global) <- get self, data
250   var data/eax: (addr array global) <- lookup *data-ah
251   var cursor-offset/ecx: (offset global) <- compute-offset data, cursor-index
252   var curr-global/eax: (addr global) <- index data, cursor-offset
253   var curr-editor-ah/eax: (addr handle gap-buffer) <- get curr-global, input
254   var curr-editor/eax: (addr gap-buffer) <- lookup *curr-editor-ah
255   edit-gap-buffer curr-editor, key
256 }
257 
258 fn create-empty-global _self: (addr global-table), name-stream: (addr stream byte), capacity: int {
259   var self/esi: (addr global-table) <- copy _self
260   var final-index-addr/ecx: (addr int) <- get self, final-index
261   increment *final-index-addr
262   var render-list-ah/eax: (addr handle array int) <- get self, render-list
263   var render-list/eax: (addr array int) <- lookup *render-list-ah
264   slide-down render-list, 0/start 0x1f/penultimate, 1/target
265   var curr-index/ecx: int <- copy *final-index-addr
266   var dest/eax: (addr int) <- index render-list, 0
267   copy-to *dest, curr-index
268   var data-ah/eax: (addr handle array global) <- get self, data
269   var data/eax: (addr array global) <- lookup *data-ah
270   var curr-offset/ecx: (offset global) <- compute-offset data, curr-index
271   var curr/esi: (addr global) <- index data, curr-offset
272   var curr-name-ah/eax: (addr handle array byte) <- get curr, name
273   stream-to-array name-stream, curr-name-ah
274   var curr-input-ah/eax: (addr handle gap-buffer) <- get curr, input
275   allocate curr-input-ah
276   var curr-input/eax: (addr gap-buffer) <- lookup *curr-input-ah
277   initialize-gap-buffer curr-input, capacity
278   var trace-ah/eax: (addr handle trace) <- get curr, trace
279   allocate trace-ah
280   var trace/eax: (addr trace) <- lookup *trace-ah
281   initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
282 }
283 
284 fn refresh-cursor-definition _self: (addr global-table) {
285   var self/esi: (addr global-table) <- copy _self
286   var render-list-ah/eax: (addr handle array int) <- get self, render-list
287   var render-list/eax: (addr array int) <- lookup *render-list-ah
288   var cursor-index/edx: (addr int) <- index render-list, 0
289   refresh-definition self, *cursor-index
290 }
291 
292 fn refresh-definition _self: (addr global-table), _index: int {
293   var self/esi: (addr global-table) <- copy _self
294   var data-ah/eax: (addr handle array global) <- get self, data
295   var data/eax: (addr array global) <- lookup *data-ah
296   var index/ebx: int <- copy _index
297   var offset/ebx: (offset global) <- compute-offset data, index
298   var curr-global/ebx: (addr global) <- index data, offset
299   var curr-input-ah/edx: (addr handle gap-buffer) <- get curr-global, input
300   var curr-trace-ah/eax: (addr handle trace) <- get curr-global, trace
301   var curr-trace/eax: (addr trace) <- lookup *curr-trace-ah
302   clear-trace curr-trace
303   var curr-value-ah/edi: (addr handle cell) <- get curr-global, value
304   var definitions-created-storage: (stream int 0x10)
305   var definitions-created/ecx: (addr stream int) <- address definitions-created-storage
306   read-and-evaluate-and-save-gap-buffer-to-globals curr-input-ah, curr-value-ah, self, definitions-created, curr-trace, 0/no-screen, 0/no-keyboard
307 }
308 
309 fn assign-or-create-global _self: (addr global-table), name: (addr array byte), value: (handle cell), index-updated: (addr int), trace: (addr trace) {
310   var self/esi: (addr global-table) <- copy _self
311   compare self, 0
312   {
313     break-if-!=
314     abort "assign global"
315   }
316   var curr-index/ecx: int <- find-symbol-name-in-globals self, name
317   {
318     compare curr-index, -1/not-found
319     break-if-!=
320     var final-index-addr/eax: (addr int) <- get self, final-index
321     increment *final-index-addr
322     curr-index <- copy *final-index-addr
323     var arr-ah/eax: (addr handle array int) <- get self, render-list
324     var arr/eax: (addr array int) <- lookup *arr-ah
325     slide-down arr, 0/start 0x1e/penultimate, 1/target
326     var dest/eax: (addr int) <- index arr, 0
327     copy-to *dest, curr-index
328   }
329   var data-ah/eax: (addr handle array global) <- get self, data
330   var data/eax: (addr array global) <- lookup *data-ah
331   var curr-offset/esi: (offset global) <- compute-offset data, curr-index
332   var curr/esi: (addr global) <- index data, curr-offset
333   var curr-name-ah/eax: (addr handle array byte) <- get curr, name
334   copy-array-object name, curr-name-ah
335   var curr-value-ah/eax: (addr handle cell) <- get curr, value
336   copy-handle value, curr-value-ah
337   var index-updated/edi: (addr int) <- copy index-updated
338   copy-to *index-updated, curr-index
339   var trace-ah/eax: (addr handle trace) <- get curr, trace
340   allocate trace-ah
341   var trace/eax: (addr trace) <- lookup *trace-ah
342   initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
343 }
344 
345 fn bump-global _globals: (addr global-table), global-id: int {
346   var globals/esi: (addr global-table) <- copy _globals
347   var render-list-ah/eax: (addr handle array int) <- get globals, render-list
348   var render-list/eax: (addr array int) <- lookup *render-list-ah
349   var idx/ecx: int <- find-slide-down-slot-in-array render-list, global-id
350   {
351     compare idx, 0
352     break-if-!=
353     return
354   }
355   slide-down render-list, 0/start idx, 1/target
356   var dest/eax: (addr int) <- index render-list, 0
357   var val/ecx: int <- copy global-id
358   copy-to *dest, val
359 }
360 
361 fn cursor-global _globals: (addr global-table) -> _/ecx: int {
362   var globals/esi: (addr global-table) <- copy _globals
363   var render-list-ah/eax: (addr handle array int) <- get globals, render-list
364   var render-list/eax: (addr array int) <- lookup *render-list-ah
365   var dest/eax: (addr int) <- index render-list, 0
366   return *dest
367 }
368 
369 fn lookup-symbol-in-globals _sym: (addr cell), out: (addr handle cell), _globals: (addr global-table), trace: (addr trace), inner-screen-var: (addr handle cell), inner-keyboard-var: (addr handle cell) {
370   var sym/eax: (addr cell) <- copy _sym
371   var sym-name-ah/eax: (addr handle stream byte) <- get sym, text-data
//: SubX mostly deals with instructions operating on 32-bit operands, but we
//: still need to deal with raw bytes for strings and so on.

//: Unfortunately the register encodings when dealing with bytes are a mess.
//: We need a special case for them.
:(code)
string rname_8bit(uint8_t r) {
  switch (r) {
  case 0: return "AL";  // lowest byte of EAX
  case 1: return "CL";  // lowest byte of ECX
  case 2: return "DL";  // lowest byte of EDX
  case 3: return "BL";  // lowest byte of EBX
  case 4: return "AH";  // second lowest byte of EAX
  case 5: return "CH";  // second lowest byte of ECX
  case 6: return "DH";  // second lowest byte of EDX
  case 7: return "BH";  // second lowest byte of EBX
  default: raise << "invalid 8-bit register " << r << '\n' << end();  return "";
  }
}

uint8_t* effective_byte_address(uint8_t modrm) {
  uint8_t mod = (modrm>>6);
  uint8_t rm = modrm & 0x7;
  if (mod == 3) {
    // select an 8-bit register
    trace(Callstack_depth+1, "run") << "r/m8 is " << rname_8bit(rm) << end();
    return reg_8bit(rm);
  }
  // the rest is as usual
  return mem_addr_u8(effective_address_number(modrm));
}

uint8_t* reg_8bit(uint8_t rm) {
  uint8_t* result = reinterpret_cast<uint8_t*>(&Reg[rm & 0x3].i);  // _L register
  if (rm & 0x4)
    ++result;  // _H register;  assumes host is little-endian
  return result;
}

:(before "End Initialize Op Names")
put_new(Name, "88", "copy r8 to r8/m8-at-r32");

:(code)
void test_copy_r8_to_mem_at_r32() {
  Reg[EBX].i = 0x224488ab;
  Reg[EAX].i = 0x2000;
  run(
      "== code 0x1\n"
      // op     ModR/M  SIB   displacement  immediate
      "  88     18                                      \n"  // copy BL to the byte at *EAX
      // ModR/M in binary: 00 (indirect mode) 011 (src BL) 000 (dest EAX)
      "== data 0x2000\n"
      "f0 cc bb aa\n"
  );
  CHECK_TRACE_CONTENTS(
      "run: copy BL to r8/m8-at-r32\n"
      "run: effective address is 0x00002000 (EAX)\n"
      "run: storing 0xab\n"
  );
  CHECK_EQ(0xaabbccab, read_mem_u32(0x2000));
}

:(before "End Single-Byte Opcodes")
case 0x88: {  // copy r8 to r/m8
  const uint8_t modrm = next();
  const uint8_t rsrc = (modrm>>3)&0x7;
  trace(Callstack_depth+1, "run") << "copy " << rname_8bit(rsrc) << " to r8/m8-at-r32" << end();
  // use unsigned to zero-extend 8-bit value to 32 bits
  uint8_t* dest = effective_byte_address(modrm);
  const uint8_t* src = reg_8bit(rsrc);
  *dest = *src;  // Read/write multiple elements of vector<uint8_t> at once. Assumes sizeof(int) == 4 on the host as well.
  trace(Callstack_depth+1, "run") << "storing 0x" << HEXBYTE << NUM(*dest) << end();
  break;
}

//:

:(before "End Initialize Op Names")
put_new(Name, "8a", "copy r8/m8-at-r32 to r8");

:(code)
void test_copy_mem_at_r32_to_r8() {
  Reg[EBX].i = 0xaabbcc0f;  // one nibble each of lowest byte set to all 0s and all 1s, to maximize value of this test
  Reg[EAX].i = 0x2000;
  run(
      "== code 0x1\n"
      // op     ModR/M  SIB   displacement  immediate
      "  8a     18                                      \n"  // copy just the byte at *EAX to BL
      // ModR/M in binary: 00 (indirect mode) 011 (dest EBX) 000 (src EAX)
      "== data 0x2000\n"
      "ab ff ff ff\n"  // 0xab with more data in following bytes
  );
  CHECK_TRACE_CONTENTS(
      "run: copy r8/m8-at-r32 to BL\n"
      "run: effective address is 0x00002000 (EAX)\n"
      "run: storing 0xab\n"
      // remaining bytes of EBX are *not* cleared
      "run: EBX now contains 0xaabbccab\n"
  );
}

:(before "End Single-Byte Opcodes")
case 0x8a: {  // copy r/m8 to r8
  const uint8_t modrm = next();
  const uint8_t rdest = (modrm>>3)&0x7;
  trace(Callstack_depth+1, "run") << "copy r8/m8-at-r32 to " << rname_8bit(rdest) << end();
  // use unsigned to zero-extend 8-bit value to 32 bits
  const uint8_t* src = effective_byte_address(modrm);
  uint8_t* dest = reg_8bit(rdest);
  trace(Callstack_depth+1, "run") << "storing 0x" << HEXBYTE << NUM(*src) << end();
  *dest = *src;  // Read/write multiple elements of vector<uint8_t> at once. Assumes sizeof(int) == 4 on the host as well.
  const uint8_t rdest_32bit = rdest & 0x3;
  trace(Callstack_depth+1, "run") << rname(rdest_32bit) << " now contains 0x" << HEXWORD << Reg[rdest_32bit].u << end();
  break;
}

:(code)
void test_cannot_copy_byte_to_ESP_EBP_ESI_EDI() {
  Reg[ESI].u = 0xaabbccdd;
  Reg[EBX].u = 0x11223344;
  run(
      "== code 0x1\n"
      // op     ModR/M  SIB   displacement  immediate
      "  8a     f3                                      \n"  // copy just the byte at *EBX to 8-bit register '6'
      // ModR/M in binary: 11 (direct mode) 110 (dest 8-bit 'register 6') 011 (src EBX)
  );
  CHECK_TRACE_CONTENTS(
      // ensure 8-bit register '6' is DH, not ESI
      "run: copy r8/m8-at-r32 to DH\n"
      "run: storing 0x44\n"
  );
  // ensure ESI is unchanged
  CHECK_EQ(Reg[ESI].u, 0xaabbccdd);
}

//:

:(before "End Initialize Op Names")
put_new(Name, "c6", "copy imm8 to r8/m8-at-r32 (mov)");

:(code)
void test_copy_imm8_to_mem_at_r32() {
  Reg[EAX].i = 0x2000;
  run(
      "== code 0x1\n"
      // op     ModR/M  SIB   displacement  immediate
      "  c6     00                          dd          \n"  // copy to the byte at *EAX
      // ModR/M in binary: 00 (indirect mode) 000 (unused) 000 (dest EAX)
      "== data 0x2000\n"
      "f0 cc bb aa\n"
  );
  CHECK_TRACE_CONTENTS(
      "run: copy imm8 to r8/m8-at-r32\n"
      "run: effective address is 0x00002000 (EAX)\n"
      "run: storing 0xdd\n"
  );
  CHECK_EQ(0xaabbccdd, read_mem_u32(0x2000));
}

:(before "End Single-Byte Opcodes")
case 0xc6: {  // copy imm8 to r/m8
  const uint8_t modrm = next();
  const uint8_t src = next();
  trace(Callstack_depth+1, "run") << "copy imm8 to r8/m8-at-r32" << end();
  trace(Callstack_depth+1, "run") << "imm8 is 0x" << HEXWORD << NUM(src) << end();
  const uint8_t subop = (modrm>>3)&0x7;  // middle 3 'reg opcode' bits
  if (subop != 0) {
    cerr << "unrecognized subop for opcode c6: " << NUM(subop) << " (only 0/copy currently implemented)\n";
    exit(1);
  }
  // use unsigned to zero-extend 8-bit value to 32 bits
  uint8_t* dest = effective_byte_address(modrm);
  *dest = src;  // Write multiple elements of vector<uint8_t> at once. Assumes sizeof(int) == 4 on the host as well.
  trace(Callstack_depth+1, "run") << "storing 0x" << HEXBYTE << NUM(*dest) << end();
  break;
}

//:: set flags (setcc)

:(before "End Initialize Op Names")
put_new(Name_0f, "94", "set r8/m8-at-rm32 to 1 if equal, if ZF is set, 0 otherwise (setcc/setz/sete)");
put_new(Name_0f, "95", "set r8/m8-at-rm32 to 1 if not equal, if ZF is not set, 0 otherwise (setcc/setnz/setne)");
put_new(