about summary refs log tree commit diff stats
path: root/120allocate.subx
Commit message (Expand)AuthorAgeFilesLines
* 7138 - type-check array 'length' instructionKartik Agaram2020-10-291-1/+1
* 6742 - support for formatting in fake screensKartik Agaram2020-09-071-4/+4
* 6659Kartik Agaram2020-07-181-7/+7
* 6658Kartik Agaram2020-07-181-1/+1
* 6643Kartik Agaram2020-07-131-1/+1
* 6627Kartik Agaram2020-07-101-2/+2
* 6612 - reorganize layersKartik Agaram2020-07-051-0/+953
de with linux.' href='/novaburst/xombrero/commit/linux/linux.c?id=33bb6bec1605b35f8903eaa0bf90a80ab665abd1'>33bb6be ^
480cd49 ^























1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741

                      
                       

















































































































































































































































































































































































































































                                                                                 




















































































































































































































































                                                                                      


































                                                                                 























                                                                                
ae40431127e9b'>^
c442a5ad ^
2eb174d6 ^

5c26afb1 ^





4c3a867b ^







5c26afb1 ^






30f844ee ^
5c26afb1 ^






30f844ee ^
5c26afb1 ^






30f844ee ^
5c26afb1 ^






30f844ee ^
5c26afb1 ^






30f844ee ^
5c26afb1 ^






30f844ee ^
5c26afb1 ^






30f844ee ^
5c26afb1 ^






30f844ee ^
5c26afb1 ^






30f844ee ^
5c26afb1 ^






30f844ee ^
5c26afb1 ^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
























                                                                                
                                                                             











                                                                                 
 

                                               
 
       
                                    


                          
                     


                                                                                           
                        








                                                    




                                      
                                                                                                
                                                       
                                                
                                      
                                                                                                                          
                                                                                    




        

                                               
 
       
                                    


                                                                                                                      
                     


                                                                                                
                        









                                                                




                                       
                                                                                           
                                                       
                                                     
                                  
                                                                                   
                                                                                                                          
                                          
                                                                                                                        

        
 




                                                 
                     











                                                                                                                





                                                       
       
                                      

                      
                     


                                                                                        
                        








                                                    




                                   
                                                                          
                                                                                  





                                                                                                           
                                                
                                                                                                                    
                                                                                    

        





                                                                                                                 







                                                                                                                                              






                                                                  
                                                                       






                                                                  
                                                                       






                                                                  
                                                                       






                                                                  
                                                                       






                                                                  
                                                                       






                                                                  
                                                                       






                                                                  
                                                                       






                                                                  
                                                                       






                                                                  
                                                                       






                                                                  
                                                                       

        
//: 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_rm32() {
  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_rm32_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_rm32() {
  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" << HEXBYTE << 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(Name_0f, "9f", "set r8/m8-at-rm32 to 1 if greater, if ZF is unset and SF == OF, 0 otherwise (setcc/setg/setnle)");
put_new(Name_0f, "97", "set r8/m8-at-rm32 to 1 if greater (addr, float), if ZF is unset and CF is unset, 0 otherwise (setcc/seta/setnbe)");
put_new(Name_0f, "9d", "set r8/m8-at-rm32 to 1 if greater or equal, if SF == OF, 0 otherwise (setcc/setge/setnl)");
put_new(Name_0f, "93", "set r8/m8-at-rm32 to 1 if greater or equal (addr, float), if CF is unset, 0 otherwise (setcc/setae/setnb)");
put_new(Name_0f, "9c", "set r8/m8-at-rm32 to 1 if lesser, if SF != OF, 0 otherwise (setcc/setl/setnge)");
put_new(Name_0f, "92", "set r8/m8-at-rm32 to 1 if lesser (addr, float), if CF is set, 0 otherwise (setcc/setb/setnae)");
put_new(Name_0f, "9e", "set r8/m8-at-rm32 to 1 if lesser or equal, if ZF is set or SF != OF, 0 otherwise (setcc/setle/setng)");
put_new(Name_0f, "96", "set r8/m8-at-rm32 to 1 if lesser or equal (addr, float), if ZF is set or CF is set, 0 otherwise (setcc/setbe/setna)");

:(before "End Two-Byte Opcodes Starting With 0f")
case 0x94: {  // set r8/m8-at-rm32 if ZF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = ZF;
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}
case 0x95: {  // set r8/m8-at-rm32 if !ZF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = !ZF;
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}
case 0x9f: {  // set r8/m8-at-rm32 if !SF and !ZF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = !ZF && SF == OF;
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}
case 0x97: {  // set r8/m8-at-rm32 if !CF and !ZF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = (!CF && !ZF);
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}
case 0x9d: {  // set r8/m8-at-rm32 if !SF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = (SF == OF);
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}
case 0x93: {  // set r8/m8-at-rm32 if !CF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = !CF;
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}
case 0x9c: {  // set r8/m8-at-rm32 if SF and !ZF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = (SF != OF);
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}
case 0x92: {  // set r8/m8-at-rm32 if CF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = CF;
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}
case 0x9e: {  // set r8/m8-at-rm32 if SF or ZF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = (ZF || SF != OF);
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}
case 0x96: {  // set r8/m8-at-rm32 if ZF or CF
  const uint8_t modrm = next();
  trace(Callstack_depth+1, "run") << "set r8/m8-at-rm32" << end();
  uint8_t* dest = effective_byte_address(modrm);
  *dest = (ZF || CF);
  trace(Callstack_depth+1, "run") << "storing " << NUM(*dest) << end();
  break;
}