1 //: operating on memory at the address provided by some register
  2 //: we'll now start providing data in a separate segment
  3 
  4 :(scenario add_r32_to_mem_at_r32)
  5 % Reg[EBX].i = 0x10;
  6 % Reg[EAX].i = 0x2000;
  7 == 0x1  # code segment
  8 # op  ModR/M  SIB   displacement  immediate
  9   01  18                                     # add EBX to *EAX
 10 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
 11 == 0x2000  # data segment
 12 01 00 00 00  # 1
 13 +run: add EBX to r/m32
 14 +run: effective address is 0x2000 (EAX)
 15 +run: storing 0x00000011
 16 
 17 :(before "End Mod Special-cases(addr)")
 18 case 0:  // indirect addressing
 19   switch (rm) {
 20   default:  // address in register
 21     trace(90, "run") << "effective address is 0x" << std::hex << Reg[rm].u << " (" << rname(rm) << ")" << end();
 22     addr = Reg[rm].u;
 23     break;
 24   // End Mod 0 Special-cases(addr)
 25   }
 26   break;
 27 
 28 //:
 29 
 30 :(before "End Initialize Op Names(name)")
 31 put(name, "03", "add rm32 to r32");
 32 
 33 :(scenario add_mem_at_r32_to_r32)
 34 % Reg[EAX].i = 0x2000;
 35 % Reg[EBX].i = 0x10;
 36 == 0x1  # code segment
 37 # op  ModR/M  SIB   displacement  immediate
 38   03  18                                      # add *EAX to EBX
 39 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
 40 == 0x2000  # data segment
 41 01 00 00 00  # 1
 42 +run: add r/m32 to EBX
 43 +run: effective address is 0x2000 (EAX)
 44 +run: storing 0x00000011
 45 
 46 :(before "End Single-Byte Opcodes")
 47 case 0x03: {  // add r/m32 to r32
 48   uint8_t modrm = next();
 49   uint8_t arg1 = (modrm>>3)&0x7;
 50   trace(90, "run") << "add r/m32 to " << rname(arg1) << end();
 51   const int32_t* arg2 = effective_address(modrm);
 52   BINARY_ARITHMETIC_OP(+, Reg[arg1].i, *arg2);
 53   break;
 54 }
 55 
 56 //:: subtract
 57 
 58 :(scenario subtract_r32_from_mem_at_r32)
 59 % Reg[EAX].i = 0x2000;
 60 % Reg[EBX].i = 1;
 61 == 0x1  # code segment
 62 # op  ModR/M  SIB   displacement  immediate
 63   29  18                                      # subtract EBX from *EAX
 64 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
 65 == 0x2000  # data segment
 66 0a 00 00 00  # 10
 67 +run: subtract EBX from r/m32
 68 +run: effective address is 0x2000 (EAX)
 69 +run: storing 0x00000009
 70 
 71 //:
 72 
 73 :(before "End Initialize Op Names(name)")
 74 put(name, "2b", "subtract rm32 from r32");
 75 
 76 :(scenario subtract_mem_at_r32_from_r32)
 77 % Reg[EAX].i = 0x2000;
 78 % Reg[EBX].i = 10;
 79 == 0x1  # code segment
 80 # op  ModR/M  SIB   displacement  immediate
 81   2b  18                                      # subtract *EAX from EBX
 82 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
 83 == 0x2000  # data segment
 84 01 00 00 00  # 1
 85 +run: subtract r/m32 from EBX
 86 +run: effective address is 0x2000 (EAX)
 87 +run: storing 0x00000009
 88 
 89 :(before "End Single-Byte Opcodes")
 90 case 0x2b: {  // subtract r/m32 from r32
 91   uint8_t modrm = next();
 92   uint8_t arg1 = (modrm>>3)&0x7;
 93   trace(90, "run") << "subtract r/m32 from " << rname(arg1) << end();
 94   const int32_t* arg2 = effective_address(modrm);
 95   BINARY_ARITHMETIC_OP(-, Reg[arg1].i, *arg2);
 96   break;
 97 }
 98 
 99 //:: and
100 
101 :(scenario and_r32_with_mem_at_r32)
102 % Reg[EAX].i = 0x2000;
103 % Reg[EBX].i = 0xff;
104 == 0x1  # code segment
105 # op  ModR/M  SIB   displacement  immediate
106   21  18                                      # and EBX with *EAX
107 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
108 == 0x2000  # data segment
109 0d 0c 0b 0a  # 0x0a0b0c0d
110 +run: and EBX with r/m32
111 +run: effective address is 0x2000 (EAX)
112 +run: storing 0x0000000d
113 
114 //:
115 
116 :(before "End Initialize Op Names(name)")
117 put(name, "23", "r32 = bitwise AND of r32 with rm32");
118 
119 :(scenario and_mem_at_r32_with_r32)
120 % Reg[EAX].i = 0x2000;
121 % Reg[EBX].i = 0x0a0b0c0d;
122 == 0x1  # code segment
123 # op  ModR/M  SIB   displacement  immediate
124   23  18                                      # and *EAX with EBX
125 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
126 == 0x2000  # data segment
127 ff 00 00 00  # 0xff
128 +run: and r/m32 with EBX
129 +run: effective address is 0x2000 (EAX)
130 +run: storing 0x0000000d
131 
132 :(before "End Single-Byte Opcodes")
133 case 0x23: {  // and r/m32 with r32
134   uint8_t modrm = next();
135   uint8_t arg1 = (modrm>>3)&0x7;
136   trace(90, "run") << "and r/m32 with " << rname(arg1) << end();
137   const int32_t* arg2 = effective_address(modrm);
138   BINARY_BITWISE_OP(&, Reg[arg1].u, *arg2);
139   break;
140 }
141 
142 //:: or
143 
144 :(scenario or_r32_with_mem_at_r32)
145 % Reg[EAX].i = 0x2000;
146 % Reg[EBX].i = 0xa0b0c0d0;
147 == 0x1  # code segment
148 # op  ModR/M  SIB   displacement  immediate
149   09  18                                      # or EBX with *EAX
150 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
151 == 0x2000  # data segment
152 0d 0c 0b 0a  # 0x0a0b0c0d
153 +run: or EBX with r/m32
154 +run: effective address is 0x2000 (EAX)
155 +run: storing 0xaabbccdd
156 
157 //:
158 
159 :(before "End Initialize Op Names(name)")
160 put(name, "0b", "r32 = bitwise OR of r32 with rm32");
161 
162 :(scenario or_mem_at_r32_with_r32)
163 % Reg[EAX].i = 0x2000;
164 % Reg[EBX].i = 0xa0b0c0d0;
165 == 0x1  # code segment
166 # op  ModR/M  SIB   displacement  immediate
167   0b  18                                      # or *EAX with EBX
168 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
169 == 0x2000  # data segment
170 0d 0c 0b 0a  # 0x0a0b0c0d
171 +run: or r/m32 with EBX
172 +run: effective address is 0x2000 (EAX)
173 +run: storing 0xaabbccdd
174 
175 :(before "End Single-Byte Opcodes")
176 case 0x0b: {  // or r/m32 with r32
177   uint8_t modrm = next();
178   uint8_t arg1 = (modrm>>3)&0x7;
179   trace(90, "run") << "or r/m32 with " << rname(arg1) << end();
180   const int32_t* arg2 = effective_address(modrm);
181   BINARY_BITWISE_OP(|, Reg[arg1].u, *arg2);
182   break;
183 }
184 
185 //:: xor
186 
187 :(scenario xor_r32_with_mem_at_r32)
188 % Reg[EAX].i = 0x2000;
189 % Reg[EBX].i = 0xa0b0c0d0;
190 == 0x1  # code segment
191 # op  ModR/M  SIB   displacement  immediate
192   31  18                                      # xor EBX with *EAX
193 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
194 == 0x2000  # data segment
195 0d 0c bb aa  # 0xaabb0c0d
196 +run: xor EBX with r/m32
197 +run: effective address is 0x2000 (EAX)
198 +run: storing 0x0a0bccdd
199 
200 //:
201 
202 :(before "End Initialize Op Names(name)")
203 put(name, "33", "r32 = bitwise XOR of r32 with rm32");
204 
205 :(scenario xor_mem_at_r32_with_r32)
206 % Reg[EAX].i = 0x2000;
207 % Reg[EBX].i = 0xa0b0c0d0;
208 == 0x1  # code segment
209 # op  ModR/M  SIB   displacement  immediate
210   33  18                                      # xor *EAX with EBX
211 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
212 == 0x2000  # data segment
213 0d 0c 0b 0a  # 0x0a0b0c0d
214 +run: xor r/m32 with EBX
215 +run: effective address is 0x2000 (EAX)
216 +run: storing 0xaabbccdd
217 
218 :(before "End Single-Byte Opcodes")
219 case 0x33: {  // xor r/m32 with r32
220   uint8_t modrm = next();
221   uint8_t arg1 = (modrm>>3)&0x7;
222   trace(90, "run") << "xor r/m32 with " << rname(arg1) << end();
223   const int32_t* arg2 = effective_address(modrm);
224   BINARY_BITWISE_OP(|, Reg[arg1].u, *arg2);
225   break;
226 }
227 
228 //:: not
229 
230 :(scenario not_of_mem_at_r32)
231 % Reg[EBX].i = 0x2000;
232 == 0x1  # code segment
233 # op  ModR/M  SIB   displacement  immediate
234   f7  13                                      # negate *EBX
235 # ModR/M in binary: 00 (indirect mode) 010 (subop not) 011 (dest EBX)
236 == 0x2000  # data segment
237 ff 00 0f 0f  # 0x0f0f00ff
238 +run: operate on r/m32
239 +run: effective address is 0x2000 (EBX)
240 +run: subop: not
241 +run: storing 0xf0f0ff00
242 
243 //:: compare (cmp)
244 
245 :(scenario compare_mem_at_r32_with_r32_greater)
246 % Reg[EAX].i = 0x2000;
247 % Reg[EBX].i = 0x0a0b0c07;
248 == 0x1  # code segment
249 # op  ModR/M  SIB   displacement  immediate
250   39  18                                      # compare EBX with *EAX
251 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
252 == 0x2000  # data segment
253 0d 0c 0b 0a  # 0x0a0b0c0d
254 +run: compare EBX with r/m32
255 +run: effective address is 0x2000 (EAX)
256 +run: SF=0; ZF=0; OF=0
257 
258 :(scenario compare_mem_at_r32_with_r32_lesser)
259 % Reg[EAX].i = 0x2000;
260 % Reg[EBX].i = 0x0a0b0c0d;
261 == 0x1  # code segment
262 # op  ModR/M  SIB   displacement  immediate
263   39  18                                      # compare EBX with *EAX
264 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
265 == 0x2000  # data segment
266 07 0c 0b 0a  # 0x0a0b0c0d
267 +run: compare EBX with r/m32
268 +run: effective address is 0x2000 (EAX)
269 +run: SF=1; ZF=0; OF=0
270 
271 :(scenario compare_mem_at_r32_with_r32_equal)
272 % Reg[EAX].i = 0x2000;
273 % Reg[EBX].i = 0x0a0b0c0d;
274 == 0x1  # code segment
275 # op  ModR/M  SIB   displacement  immediate
276   39  18                                      # compare EBX with *EAX
277 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
278 == 0x2000  # data segment
279 0d 0c 0b 0a  # 0x0a0b0c0d
280 +run: compare EBX with r/m32
281 +run: effective address is 0x2000 (EAX)
282 +run: SF=0; ZF=1; OF=0
283 
284 //:
285 
286 :(before "End Initialize Op Names(name)")
287 put(name, "3b", "compare: set SF if r32 < rm32");
288 
289 :(scenario compare_r32_with_mem_at_r32_greater)
290 % Reg[EAX].i = 0x2000;
291 % Reg[EBX].i = 0x0a0b0c0d;
292 == 0x1  # code segment
293 # op  ModR/M  SIB   displacement  immediate
294   3b  18                                      # compare *EAX with EBX
295 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
296 == 0x2000  # data segment
297 07 0c 0b 0a  # 0x0a0b0c0d
298 +run: compare r/m32 with EBX
299 +run: effective address is 0x2000 (EAX)
300 +run: SF=0; ZF=0; OF=0
301 
302 :(before "End Single-Byte Opcodes")
303 case 0x3b: {  // set SF if r32 < r/m32
304   uint8_t modrm = next();
305   uint8_t reg1 = (modrm>>3)&0x7;
306   trace(90, "run") << "compare r/m32 with " << rname(reg1) << end();
307   int32_t arg1 = Reg[reg1].i;
308   int32_t* arg2 = effective_address(modrm);
309   int32_t tmp1 = arg1 - *arg2;
310   SF = (tmp1 < 0);
311   ZF = (tmp1 == 0);
312   int64_t tmp2 = arg1 - *arg2;
313   OF = (tmp1 != tmp2);
314   trace(90, "run") << "SF=" << SF << "; ZF=" << ZF << "; OF=" << OF << end();
315   break;
316 }
317 
318 :(scenario compare_r32_with_mem_at_r32_lesser)
319 % Reg[EAX].i = 0x2000;
320 % Reg[EBX].i = 0x0a0b0c07;
321 == 0x1  # code segment
322 # op  ModR/M  SIB   displacement  immediate
323   3b  18                                      # compare *EAX with EBX
324 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
325 == 0x2000  # data segment
326 0d 0c 0b 0a  # 0x0a0b0c0d
327 +run: compare r/m32 with EBX
328 +run: effective address is 0x2000 (EAX)
329 +run: SF=1; ZF=0; OF=0
330 
331 :(scenario compare_r32_with_mem_at_r32_equal)
332 % Reg[EAX].i = 0x2000;
333 % Reg[EBX].i = 0x0a0b0c0d;
334 == 0x1  # code segment
335 # op  ModR/M  SIB   displacement  immediate
336   3b  18                                      # compare *EAX with EBX
337 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
338 == 0x2000  # data segment
339 0d 0c 0b 0a  # 0x0a0b0c0d
340 +run: compare r/m32 with EBX
341 +run: effective address is 0x2000 (EAX)
342 +run: SF=0; ZF=1; OF=0
343 
344 //:: copy (mov)
345 
346 :(scenario copy_r32_to_mem_at_r32)
347 % Reg[EBX].i = 0xaf;
348 % Reg[EAX].i = 0x60;
349 == 0x1
350 # op  ModR/M  SIB   displacement  immediate
351   89  18                                      # copy EBX to *EAX
352 # ModR/M in binary: 00 (indirect mode) 011 (src EAX) 000 (dest EAX)
353 +run: copy EBX to r/m32
354 +run: effective address is 0x60 (EAX)
355 +run: storing 0x000000af
356 
357 //:
358 
359 :(before "End Initialize Op Names(name)")
360 put(name, "8b", "copy rm32 to r32");
361 
362 :(scenario copy_mem_at_r32_to_r32)
363 % Reg[EAX].i = 0x2000;
364 == 0x1  # code segment
365 # op  ModR/M  SIB   displacement  immediate
366   8b  18                                      # copy *EAX to EBX
367 # ModR/M in binary: 00 (indirect mode) 011 (src EBX) 000 (dest EAX)
368 == 0x2000  # data segment
369 af 00 00 00  # 0xaf
370 +run: copy r/m32 to EBX
371 +run: effective address is 0x2000 (EAX)
372 +run: storing 0x000000af
373 
374 :(before "End Single-Byte Opcodes")
375 case 0x8b: {  // copy r32 to r/m32
376   uint8_t modrm = next();
377   uint8_t reg1 = (modrm>>3)&0x7;
378   trace(90, "run") << "copy r/m32 to " << rname(reg1) << end();
379   int32_t* arg2 = effective_address(modrm);
380   Reg[reg1].i = *arg2;
381   trace(90, "run") << "storing 0x" << HEXWORD << *arg2 << end();
382   break;
383 }
384 
385 //:
386 
387 :(before "End Initialize Op Names(name)")
388 put(name, "88", "copy r8 (lowermost byte of r32) to r8/m8-at-r32");
389 
390 :(scenario copy_r8_to_mem_at_r32)
391 % Reg[EBX].i = 0x224488ab;
392 % Reg[EAX].i = 0x2000;
393 == 0x1
394 # op  ModR/M  SIB   displacement  immediate
395   88  18                                      # copy just the lowermost byte of EBX to the byte at *EAX
396 # ModR/M in binary: 00 (indirect mode) 011 (src EBX) 000 (dest EAX)
397 == 0x2000
398 f0 cc bb aa  # 0xf0 with more data in following bytes
399 +run: copy lowermost byte of EBX to r8/m8-at-r32
400 +run: effective address is 0x2000 (EAX)
401 +run: storing 0xab
402 % CHECK_EQ(0xaabbccab, read_mem_u32(0x2000));
403 
404 :(before "End Single-Byte Opcodes")
405 case 0x88: {  // copy r/m8 to r8
406   uint8_t modrm = next();
407   uint8_t reg2 = (modrm>>3)&0x7;
408   trace(90, "run") << "copy lowermost byte of " << rname(reg2) << " to r8/m8-at-r32" << end();
409   // use unsigned to zero-extend 8-bit value to 32 bits
410   uint8_t* arg1 = reinterpret_cast<uint8_t*>(effective_address(modrm));
411   *arg1 = Reg[reg2].u;
412   trace(90, "run") << "storing 0x" << HEXBYTE << NUM(*arg1) << end();
413   break;
414 }
415 
416 //:
417 
418 :(before "End Initialize Op Names(name)")
419 put(name, "8a", "copy r8/m8-at-r32 to r8 (lowermost byte of r32)");
420 
421 :(scenario copy_mem_at_r32_to_r8)
422 % Reg[EBX].i = 0xaabbcc0f;  // one nibble each of lowest byte set to all 0s and all 1s, to maximize value of this test
423 % Reg[EAX].i = 0x2000;
424 == 0x1
425 # op  ModR/M  SIB   displacement  immediate
426   8a  18                                      # copy just the byte at *EAX to lowermost byte of EBX (clearing remaining bytes)
427 # ModR/M in binary: 00 (indirect mode) 011 (dest EBX) 000 (src EAX)
428 == 0x2000  # data segment
429 ab ff ff ff  # 0xab with more data in following bytes
430 +run: copy r8/m8-at-r32 to lowermost byte of EBX
431 +run: effective address is 0x2000 (EAX)
432 +run: storing 0xab
433 # remaining bytes of EBX are *not* cleared
434 +run: EBX now contains 0xaabbccab
435 
436 :(before "End Single-Byte Opcodes")
437 case 0x8a: {  // copy r/m8 to r8
438   uint8_t modrm = next();
439   uint8_t reg1 = (modrm>>3)&0x7;
440   trace(90, "run") << "copy r8/m8-at-r32 to lowermost byte of " << rname(reg1) << end();
441   // use unsigned to zero-extend 8-bit value to 32 bits
442   uint8_t* arg2 = reinterpret_cast<uint8_t*>(effective_address(modrm));
443   trace(90, "run") << "storing 0x" << HEXBYTE << NUM(*arg2) << end();
444   *reinterpret_cast<uint8_t*>(&Reg[reg1].u) = *arg2;  // assumes host is little-endian
445   trace(90, "run") << rname(reg1) << " now contains 0x" << HEXWORD << Reg[reg1].u << end();
446   break;
447 }
448 
449 //:: jump
450 
451 :(scenario jump_mem_at_r32)
452 % Reg[EAX].i = 0x2000;
453 == 0x1  # code segment
454 # op  ModR/M  SIB   displacement  immediate
455   ff  20                                      # jump to *EAX
456 # ModR/M in binary: 00 (indirect mode) 100 (jump to r/m32) 000 (src EAX)
457   05                              00 00 00 01
458   05                              00 00 00 02
459 == 0x2000  # data segment
460 08 00 00 00  # 8
461 +run: inst: 0x00000001
462 +run: jump to r/m32
463 +run: effective address is 0x2000 (EAX)
464 +run: jumping to 0x00000008
465 +run: inst: 0x00000008
466 -run: inst: 0x00000003
467 
468 :(before "End Op ff Subops")
469 case 4: {  // jump to r/m32
470   trace(90, "run") << "jump to r/m32" << end();
471   int32_t* arg2 = effective_address(modrm);
472   EIP = *arg2;
473   trace(90, "run") << "jumping to 0x" << HEXWORD << EIP << end();
474   break;
475 }
476 
477 //:: push
478 
479 :(scenario push_mem_at_r32)
480 % Reg[EAX].i = 0x2000;
481 % Reg[ESP].u = 0x14;
482 == 0x1  # code segment
483 # op  ModR/M  SIB   displacement  immediate
484   ff  30                                      # push *EAX to stack
485 # ModR/M in binary: 00 (indirect mode) 110 (push r/m32) 000 (src EAX)
486 == 0x2000  # data segment
487 af 00 00 00  # 0xaf
488 +run: push r/m32
489 +run: effective address is 0x2000 (EAX)
490 +run: decrementing ESP to 0x00000010
491 +run: pushing value 0x000000af
492 
493 :(before "End Op ff Subops")
494 case 6: {  // push r/m32 to stack
495   trace(90, "run") << "push r/m32" << end();
496   const int32_t* val = effective_address(modrm);
497   push(*val);
498   break;
499 }
500 
501 //:: pop
502 
503 :(before "End Initialize Op Names(name)")
504 put(name, "8f", "pop top of stack to rm32");
505 
506 :(scenario pop_mem_at_r32)
507 % Reg[EAX].i = 0x60;
508 % Reg[ESP].u = 0x2000;
509 == 0x1  # code segment
510 # op  ModR/M  SIB   displacement  immediate
511   8f  00                                      # pop stack into *EAX
512 # ModR/M in binary: 00 (indirect mode) 000 (pop r/m32) 000 (dest EAX)
513 == 0x2000  # data segment
514 30 00 00 00  # 0x30
515 +run: pop into r/m32
516 +run: effective address is 0x60 (EAX)
517 +run: popping value 0x00000030
518 +run: incrementing ESP to 0x00002004
519 
520 :(before "End Single-Byte Opcodes")
521 case 0x8f: {  // pop stack into r/m32
522   uint8_t modrm = next();
523   uint8_t subop = (modrm>>3)&0x7;
524   switch (subop) {
525     case 0: {
526       trace(90, "run") << "pop into r/m32" << end();
527       int32_t* dest = effective_address(modrm);
528       *dest = pop();
529       break;
530     }
531   }
532   break;
533 }
534 
535 //:: special-case for loading address from disp32 rather than register
536 
537 :(scenario add_r32_to_mem_at_displacement)
538 % Reg[EBX].i = 0x10;  // source
539 == 0x1  # code segment
540 # op  ModR/M  SIB   displacement  immediate
541   01  1d            00 20 00 00              # add EBX to *0x2000
542 # ModR/M in binary: 00 (indirect mode) 011 (src EBX) 101 (dest in disp32)
543 == 0x2000  # data segment
544 01 00 00 00  # 1
545 +run: add EBX to r/m32
546 +run: effective address is 0x2000 (disp32)
547 +run: storing 0x00000011
548 
549 :(before "End Mod 0 Special-cases(addr)")
550 case 5:  // exception: mod 0b00 rm 0b101 => incoming disp32
551   addr = next32();
552   trace(90, "run") << "effective address is 0x" << std::hex << addr << " (disp32)" << end();
553   break;
554 
555 //:
556 
557 :(scenario add_r32_to_mem_at_r32_plus_disp8)
558 % Reg[EBX].i = 0x10;  // source
559 % Reg[EAX].i = 0x1ffe;  // dest
560 == 0x1  # code segment
561 # op  ModR/M  SIB   displacement  immediate
562   01  58            02                       # add EBX to *(EAX+2)
563 # ModR/M in binary: 01 (indirect+disp8 mode) 011 (src EBX) 000 (dest EAX)
564 == 0x2000  # data segment
565 01 00 00 00  # 1
566 +run: add EBX to r/m32
567 +run: effective address is initially 0x1ffe (EAX)
568 +run: effective address is 0x2000 (after adding disp8)
569 +run: storing 0x00000011
570 
571 :(before "End Mod Special-cases(addr)")
572 case 1:  // indirect + disp8 addressing
573   switch (rm) {
574   default:
575     addr = Reg[rm].u;
576     trace(90, "run") << "effective address is initially 0x" << std::hex << addr << " (" << rname(rm) << ")" << end();
577     break;
578   // End Mod 1 Special-cases(addr)
579   }
580   if (addr > 0) {
581     addr += static_cast<int8_t>(next());
582     trace(90, "run") << "effective address is 0x" << std::hex << addr << " (after adding disp8)" << end();
583   }
584   break;
585 
586 :(scenario add_r32_to_mem_at_r32_plus_negative_disp8)
587 % Reg[EBX].i = 0x10;  // source
588 % Reg[EAX].i = 0x2001;  // dest
589 == 0x1  # code segment
590 # op  ModR/M  SIB   displacement  immediate
591   01  58            ff                       # add EBX to *(EAX-1)
592 # ModR/M in binary: 01 (indirect+disp8 mode) 011 (src EBX) 000 (dest EAX)
593 == 0x2000  # data segment
594 01 00 00 00  # 1
595 +run: add EBX to r/m32
596 +run: effective address is initially 0x2001 (EAX)
597 +run: effective address is 0x2000 (after adding disp8)
598 +run: storing 0x00000011
599 
600 //:
601 
602 :(scenario add_r32_to_mem_at_r32_plus_disp32)
603 % Reg[EBX].i = 0x10;  // source
604 % Reg[EAX].i = 0x1ffe;  // dest
605 == 0x1  # code segment
606 # op  ModR/M  SIB   displacement  immediate
607   01  98            02 00 00 00              # add EBX to *(EAX+2)
608 # ModR/M in binary: 10 (indirect+disp32 mode) 011 (src EBX) 000 (dest EAX)
609 == 0x2000  # data segment
610 01 00 00 00  # 1
611 +run: add EBX to r/m32
612 +run: effective address is initially 0x1ffe (EAX)
613 +run: effective address is 0x2000 (after adding disp32)
614 +run: storing 0x00000011
615 
616 :(before "End Mod Special-cases(addr)")
617 case 2:  // indirect + disp32 addressing
618   switch (rm) {
619   default:
620     addr = Reg[rm].u;
621     trace(90, "run") << "effective address is initially 0x" << std::hex << addr << " (" << rname(rm) << ")" << end();
622     break;
623   // End Mod 2 Special-cases(addr)
624   }
625   if (addr > 0) {
626     addr += next32();
627     trace(90, "run") << "effective address is 0x" << std::hex << addr << " (after adding disp32)" << end();
628   }
629   break;
630 
631 :(scenario add_r32_to_mem_at_r32_plus_negative_disp32)
632 % Reg[EBX].i = 0x10;  // source
633 % Reg[EAX].i = 0x2001;  // dest
634 == 0x1  # code segment
635 # op  ModR/M  SIB   displacement  immediate
636   01  98            ff ff ff ff              # add EBX to *(EAX-1)
637 # ModR/M in binary: 10 (indirect+disp32 mode) 011 (src EBX) 000 (dest EAX)
638 == 0x2000  # data segment
639 01 00 00 00  # 1
640 +run: add EBX to r/m32
641 +run: effective address is initially 0x2001 (EAX)
642 +run: effective address is 0x2000 (after adding disp32)
643 +run: storing 0x00000011
644 
645 //:: lea
646 
647 :(before "End Initialize Op Names(name)")
648 put(name, "8d", "load effective address of memory in rm32 into r32");
649 
650 :(scenario lea)
651 % Reg[EAX].u = 0x2000;
652 == 0x1
653 # op  ModR/M  SIB   displacement  immediate
654   8d  18
655 # ModR/M in binary: 00 (indirect mode) 011 (dest EBX) 000 (src EAX)
656 +run: lea into EBX
657 +run: effective address is 0x2000 (EAX)
658 
659 :(before "End Single-Byte Opcodes")
660 case 0x8d: {  // lea m32 to r32
661   uint8_t modrm = next();
662   uint8_t arg1 = (modrm>>3)&0x7;
663   trace(90, "run") << "lea into " << rname(arg1) << end();
664   Reg[arg1].u = effective_address_number(modrm);
665   break;
666 }