about summary refs log tree commit diff stats
Commit message (Expand)AuthorAgeFilesLines
...
* .Kartik K. Agaram2021-08-101-2/+2
* .Kartik K. Agaram2021-08-101-2/+2
* .Kartik K. Agaram2021-08-101-2/+2
* .Kartik K. Agaram2021-08-101-8/+1
* .Kartik K. Agaram2021-08-101-27/+27
* .Kartik K. Agaram2021-08-101-9/+0
* .Kartik K. Agaram2021-08-083-5/+28
* .Kartik K. Agaram2021-08-071-0/+1
* .Kartik K. Agaram2021-08-074-10/+111
* .Kartik K. Agaram2021-08-061-3/+9
* .Kartik K. Agaram2021-08-061-8/+15
* beginnings of a Slack archive readerKartik K. Agaram2021-08-062-0/+105
* .Kartik K. Agaram2021-08-021-0/+4
* better error handling on stray commasKartik K. Agaram2021-08-012-0/+47
* .Kartik K. Agaram2021-08-011-13/+16
* .Kartik K. Agaram2021-08-011-1/+3
* support non-line-oriented processing in next-wordKartik K. Agaram2021-07-2916-36/+73
* maintain aspect ratio when rendering imagesKartik K. Agaram2021-07-293-8/+43
* .Kartik K. Agaram2021-07-291-0/+2
* downsampling photos makes the dithering seem worseKartik K. Agaram2021-07-291-2/+2
* .Kartik K. Agaram2021-07-291-1/+1
* shell: reliable image renderingKartik K. Agaram2021-07-291-0/+1
* shell: rendering image definitionsKartik K. Agaram2021-07-291-1/+68
* shell: support loading 128x128px imagesKartik K. Agaram2021-07-286-10/+10
* shell: literal imagesKartik K. Agaram2021-07-286-8/+42
* saving definitions containing string literalsKartik K. Agaram2021-07-281-2/+0
* .Kartik K. Agaram2021-07-282-22/+0
* .Kartik K. Agaram2021-07-281-0/+142
* shell: second notation for string literalsKartik K. Agaram2021-07-281-5/+126
* shell: render image from pbm data streamKartik K. Agaram2021-07-275-5/+239
* .Kartik K. Agaram2021-07-261-0/+74
* .Kartik K. Agaram2021-07-261-0/+2
* .Kartik K. Agaram2021-07-262-4/+16
* .Kartik K. Agaram2021-07-261-1/+1
* game of life in lispKartik K. Agaram2021-07-263-47/+109
* .Kartik K. Agaram2021-07-263-10/+40
* gracefully trace large multi-dimensional arraysKartik K. Agaram2021-07-261-4/+10
* gracefully trace large arraysKartik K. Agaram2021-07-261-0/+8
* palette operations now a bit fasterKartik K. Agaram2021-07-261-7/+9
* .Kartik K. Agaram2021-07-261-0/+6
* .Kartik K. Agaram2021-07-261-3/+3
* shell primitive: initialize array of some sizeKartik K. Agaram2021-07-261-0/+78
* shell primitive: iset to mutate array at indexKartik K. Agaram2021-07-251-1/+104
* shell primitive: array indexKartik K. Agaram2021-07-251-1/+82
* shell: array typeKartik K. Agaram2021-07-254-1/+161
* .Kartik K. Agaram2021-07-251-9/+9
* .Kartik K. Agaram2021-07-251-5/+5
* .Kartik K. Agaram2021-07-251-4/+4
* shell: starting to implement arraysKartik K. Agaram2021-07-252-5/+65
* .Kartik K. Agaram2021-07-251-11/+10
5d50521b6276b1022c9652aef0ed8'>861418f0 ^
4a943d4e ^




83c67014 ^
4a943d4e ^











2eb174d6 ^





4a943d4e ^



83c67014 ^
4a943d4e ^


83c67014 ^
4a943d4e ^








2eb174d6 ^




c442a5ad ^
3f097f3f ^
2eb174d6 ^







c442a5ad ^
2eb174d6 ^

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
























                                                                                
                                                                             











                                                                                 
 

                                               
 




                                   
                     


                                                                                           
                        








                                                    




                                      
                                                                                                
                                                       


                                                                            
                                                                                    




        

                                               
 




                                                                                                                      
                     


                                                                                                
                        









                                                                




                                       
                                                                                           
                                                       

                                                                                 
                                                                                   

                                          
                                                                                                                        

        
 




                                                 
                     











                                                                                                                





                                                       



                                     
                     


                                                                                        
                        








                                                    




                                   
                                                                          
                                                                                  







                                                                                                           
                                                                                    

        
//: 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 = reinterpret_cast<uint8_t*>(effective_byte_address(modrm));
  const uint8_t* src = reg_8bit(rsrc);
  *dest = *src;
  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 = reinterpret_cast<uint8_t*>(effective_byte_address(modrm));
  uint8_t* dest = reg_8bit(rdest);
  trace(Callstack_depth+1, "run") << "storing 0x" << HEXBYTE << NUM(*src) << end();
  *dest = *src;
  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 = reinterpret_cast<uint8_t*>(effective_byte_address(modrm));
  *dest = src;
  trace(Callstack_depth+1, "run") << "storing 0x" << HEXBYTE << NUM(*dest) << end();
  break;
}