-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU.java
More file actions
604 lines (569 loc) · 21.5 KB
/
CPU.java
File metadata and controls
604 lines (569 loc) · 21.5 KB
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
@SuppressWarnings("ALL")
public class CPU {
// Computer memory
// 0x00000000 -> 0x7ffffffd
// The max memory size is 0x7ffffffd
public byte[] memory;
// Registers, [ [],[],[],[],[] ]
// eax, ebx, ecx, edx, edi, esi, eip
public long[] registers;
// Flags: [CF,ZF,SF]
public boolean[] flags;
public boolean running;
public CPU() {
memory = new byte[0x7ffffffd];
registers = new long[13];
flags = new boolean[3];
registers[Registers.esp] = 0x7fffff00;
registers[Registers.ebp] = 0x7fffff00;
}
public String padLeftZeros(String inputString, int length) {
if (inputString.length() >= length) {
return inputString;
}
StringBuilder sb = new StringBuilder();
while (sb.length() < length - inputString.length()) {
sb.append('0');
}
sb.append(inputString);
return sb.toString();
}
public void printMemory(int startingLocation){
printMemory(startingLocation, 100);
}
public void printMemory(int startingLocation, int bytes){
for (int i = startingLocation; i < startingLocation+bytes; i++) {
System.out.println(String.format("0x%02X", memory[i]));
}
}
public int btoi(byte inByte){
return ((int)inByte) & 0xff;
}
public long binaryAdd(long a, long b) {
String String1 = padLeftZeros(Long.toBinaryString(a), 32);
String String2 = padLeftZeros(Long.toBinaryString(b), 32);
char[] String1Array = String1.toCharArray();
char[] String2Array = String2.toCharArray();
String outString = "";
int carry = 0;
for (int i = 31; i > 0; i--) {
int temp = (int) String1Array[i]-48 + (int) String2Array[i]-48 + carry;
carry = 0;
if (temp > 1) {
temp = temp - 2;
carry = 1;
}
outString = Integer.toString(temp) + outString;
}
long outputLong = Long.parseLong(outString, 2);
flags[Flags.CF] = carry==1;
return outputLong;
}
//=============== ALL THE FETCH INSTRUCTIONS FROM EIP =========================
public long fetch32(){
int ip = (int) registers[Registers.eip];
long returnValue = fetch32at(ip);
registers[Registers.eip] = ip+4;
return returnValue;
}
public int fetch16(){
int ip = (int) registers[Registers.eip];
int returnValue = fetch16at(ip);
registers[Registers.eip] = ip+2;
return returnValue;
}
public byte fetch(){
int ip = (int) registers[Registers.eip];
byte returnValue = fetchat(ip);
registers[Registers.eip] = ip+1;
return returnValue;
}
//=============== ALL THE FETCH INSTRUCTIONS FROM ADDRESS =========================
public long fetch32at(int address){
byte byte1 = memory[address++];
byte byte2 = memory[address++];
byte byte3 = memory[address++];
byte byte4 = memory[address++];
long result = btoi(byte4) + btoi(byte3) * 0x100 + btoi(byte2) * 0x10000 + btoi(byte1) * 0x1000000;
return result;
}
public int fetch16at(int address){
byte byte1 = memory[address++];
byte byte2 = memory[address++];
int result = btoi(byte2) + btoi(byte1) * 0x100;
return result;
}
public byte fetchat(int address){
byte byte1 = memory[address];
return byte1;
}
//=============== ALL THE WRITE INSTRUCTIONS =========================
/**
* Writes the 32 bit value to the address in memory
* @param value the value to write
* @param address the address to write to
*/
public void write32at(long value, int address) {
//value = 0x12345678
byte byte4 = (byte) (value & 0x000000ff); //0x00000078
byte byte3 = (byte) ((value & 0x0000ff00) >> 8); //0x00005600
byte byte2 = (byte) ((value & 0x00ff0000) >> 16);
byte byte1 = (byte) ((value & 0xff000000) >> 24);
memory[address++] = byte1;
memory[address++] = byte2;
memory[address++] = byte3;
memory[address] = byte4;
}
public void write16at(int value, int address){
byte byte2 = (byte) (value & 0x00ff); //0x00000078
byte byte1 = (byte) ((value & 0xff00) >> 8); //0x00005600
memory[address++] = byte1;
memory[address] = byte2;
}
public void write8at(byte value, int address){
memory[address] = value;
}
public void push(long literal){
registers[Registers.esp] -= 4;
write32at(literal, (int)registers[Registers.esp]);
}
public long pop(){
long returnValue = fetch32at((int)registers[Registers.esp]);
registers[Registers.esp] += 4;
return returnValue;
}
/**
* Executes 1 instruction starting at the instruction pointer
*/
public void step(){
byte instruction = fetch();
switch(instruction){
//=====================Implementation of MOV instructions===============
case Instructions.MOV_LIT_MEM: {
long lit = fetch32();
int mem = fetch16();
write32at(lit, mem);
break;
}
case Instructions.MOV_LIT_REG: {
long lit = fetch32();
int reg = fetch();
registers[reg] = lit;
break;
}
case Instructions.MOV_REG_REG: {
int reg1 = fetch();
int reg2 = fetch();
registers[reg2] = registers[reg1];
break;
}
case Instructions.MOV_REG_MEM: {
int reg = fetch();
int mem = fetch16();
write32at(registers[reg],mem);
break;
}
case Instructions.MOV_MEM_REG: {
int mem = fetch16();
int reg = fetch();
registers[reg] = fetch32at(mem);
break;
}
//=====================Implementation of Arithmetics instructions===============
case Instructions.ADD_LIT_REG: {
long lit = fetch32();
int reg = fetch();
if(registers[reg]+lit>0x100000000l)
registers[reg] = binaryAdd(registers[reg], lit);
break;
}
case Instructions.ADD_REG_REG: {
int reg1 = fetch();
int reg2 = fetch();
registers[reg2] = binaryAdd(registers[reg2], registers[reg1]);
break;
}
case Instructions.SUB_LIT_REG: {
long lit = fetch32();
int reg = fetch();
//Set appropriate CF
registers[reg] = registers[reg] - lit;
flags[Flags.CF] = registers[reg]<0;
flags[Flags.ZF] = registers[reg]==0;
break;
}
case Instructions.SUB_REG_REG: {
int reg1 = fetch();
int reg2 = fetch();
registers[reg2] = registers[reg2] - registers[reg1];
flags[Flags.CF] = registers[reg2]<0;
flags[Flags.ZF] = registers[reg2]==0;
break;
}
case Instructions.MUL_LIT: {
long lit = fetch32();
registers[Registers.eax] = registers[Registers.eax] * lit;
break;
}
case Instructions.MUL_REG: {
int reg1 = fetch();
registers[Registers.eax] = registers[Registers.eax] * registers[reg1];
break;
}
case Instructions.DIV_LIT: {
long lit = fetch32();
registers[Registers.edx] = registers[Registers.eax] % lit;
registers[Registers.eax] = registers[Registers.eax] / lit;
break;
}
case Instructions.DIV_REG: {
int reg1 = fetch();
registers[Registers.edx] = registers[Registers.eax] % registers[reg1];
registers[Registers.eax] = registers[Registers.eax] / registers[reg1];
break;
}
//=====================Implementation of function call instructions===============
case Instructions.CALL_PRINTF: {
//Calling convention: rdi, rsi, rdx, rcx
int formatstring_memorylocation = (int) registers[Registers.edi];
byte charByte = fetchat(formatstring_memorylocation++);
String formatString = "";
while(charByte != 0){
formatString += (char) charByte;
charByte = fetchat(formatstring_memorylocation++);
}
long vararg1 = registers[Registers.esi];
long vararg2 = registers[Registers.edx];
long vararg3 = registers[Registers.ecx];
System.out.printf(formatString,vararg1,vararg2,vararg3);
break;
}
case Instructions.CALL_PUTCHAR: {
//Calling convention: rdi
char charByte = (char) (registers[Registers.edi] & 0xff);
System.out.print(charByte);
break;
}
case Instructions.CALL_EXIT: {
//Calling convention: rdi
running = false;
break;
}
//=====================Implementation of branching instructions===============
case Instructions.CMP_LIT_REG: {
long lit = fetch32();
byte reg = fetch();
long reg_value = registers[reg];
flags[Flags.CF] = lit-reg_value<0;
flags[Flags.ZF] = lit-reg_value==0;
break;
}
case Instructions.CMP_REG_REG: {
byte reg1 = fetch();
byte reg2 = fetch();
long reg_value1 = registers[reg1];
long reg_value2 = registers[reg2];
flags[Flags.CF] = reg_value1-reg_value2<0;
flags[Flags.ZF] = reg_value1-reg_value2==0;
break;
}
case Instructions.JMP_LIT: {
long address = fetch32();
registers[Registers.eip] = address;
break;
}
case Instructions.JMP_REG: {
byte reg = fetch();
registers[Registers.eip] = registers[reg];
break;
}
case Instructions.JE_LIT: {
long address = fetch32();
if(flags[Flags.ZF]==true) {
registers[Registers.eip] = address;
}
break;
}
case Instructions.JE_REG: {
byte reg = fetch();
if(flags[Flags.ZF]==true) {
registers[Registers.eip] = registers[reg];
}
break;
}
case Instructions.JNE_LIT: {
long address = fetch32();
if(flags[Flags.ZF]!=true) {
registers[Registers.eip] = address;
}
break;
}
case Instructions.JNE_REG: {
byte reg = fetch();
if(flags[Flags.ZF]!=true) {
registers[Registers.eip] = registers[reg];
}
break;
}
case Instructions.JGE_LIT: {
long address = fetch32();
if(flags[Flags.ZF]==true || flags[Flags.CF]==false) {
registers[Registers.eip] = address;
}
break;
}
case Instructions.JGE_REG: {
byte reg = fetch();
if(flags[Flags.ZF]==true || flags[Flags.CF]==false) {
registers[Registers.eip] = registers[reg];
}
break;
}
case Instructions.JG_LIT: {
long address = fetch32();
if(flags[Flags.ZF]==false && flags[Flags.CF]==false) {
registers[Registers.eip] = address;
}
break;
}
case Instructions.JG_REG: {
byte reg = fetch();
if(flags[Flags.ZF]==false && flags[Flags.CF]==false) {
registers[Registers.eip] = registers[reg];
}
break;
}
case Instructions.JLE_LIT: {
long address = fetch32();
if(flags[Flags.ZF]==true || flags[Flags.CF]==true) {
registers[Registers.eip] = address;
}
break;
}
case Instructions.JLE_REG: {
byte reg = fetch();
if(flags[Flags.ZF]==true || flags[Flags.CF]==true) {
registers[Registers.eip] = registers[reg];
}
break;
}
case Instructions.JL_LIT: {
long address = fetch32();
if(flags[Flags.ZF]==false && flags[Flags.CF]==true) {
registers[Registers.eip] = address;
}
break;
}
case Instructions.JL_REG: {
byte reg = fetch();
if(flags[Flags.ZF]==false && flags[Flags.CF]==true) {
registers[Registers.eip] = registers[reg];
}
break;
}
//=====================================Implementation of Stack Manipulation=================
case Instructions.PUSH_LIT: {
long lit = fetch32();
push(lit);
break;
}
case Instructions.PUSH_REG: {
byte reg = fetch();
push(registers[reg]);
break;
}
case Instructions.POP_REG: {
byte reg = fetch();
registers[reg] = pop();
break;
}
case Instructions.CALL_LIT: {
long lit = fetch32();
//PRESERVE REGISTERS
for(int i=0;i<registers.length;i++){
if(i!=Registers.eax && i!=Registers.esp && i!=Registers.ebp && i!=Registers.eip){
push(registers[i]);
}
}
push(registers[Registers.eip]);
registers[Registers.eip] = lit;
break;
}
case Instructions.RET: {
long returnAddress = pop();
registers[Registers.eip] = returnAddress;
//RETURN REGISTERS TO ORIGINAL STATE
for(int i=registers.length-1;i>=0;i--){
if(i!=Registers.eax && i!=Registers.esp && i!=Registers.ebp && i!=Registers.eip){
registers[i] = pop();
}
}
break;
}
case Instructions.ENTER: {
//PUSH RBP
//MOV RSP,RBP
push(registers[Registers.ebp]);
registers[Registers.ebp] = registers[Registers.esp];
break;
}
case Instructions.LEAVE: {
registers[Registers.esp] = registers[Registers.ebp];
registers[Registers.ebp] = pop();
break;
}
//========================Implementation of Binary Logic======================
case Instructions.SHL_LIT_REG: {
byte lit = fetch();
byte reg = fetch();
long result = (registers[reg] << lit) & 0xffffffff;
registers[reg] = result;
break;
}
case Instructions.SHL_REG_REG: {
byte reg1 = fetch();
byte reg2 = fetch();
long result = (registers[reg2] << registers[reg1]) & 0xffffffff;
registers[reg2] = result;
break;
}
case Instructions.SHR_LIT_REG: {
byte lit = fetch();
byte reg = fetch();
long result = (registers[reg] >> lit) & 0xffffffff;
registers[reg] = result;
break;
}
case Instructions.SHR_REG_REG: {
byte reg1 = fetch();
byte reg2 = fetch();
long result = (registers[reg2] >> registers[reg1]) & 0xffffffff;
registers[reg2] = result;
break;
}
case Instructions.AND_LIT_REG: {
byte lit = fetch();
byte reg = fetch();
long result = (registers[reg] & lit) & 0xffffffff;
registers[reg] = result;
break;
}
case Instructions.AND_REG_REG: {
byte reg1 = fetch();
byte reg2 = fetch();
long result = (registers[reg2] & registers[reg1]) & 0xffffffff;
registers[reg2] = result;
break;
}
case Instructions.OR_LIT_REG: {
byte lit = fetch();
byte reg = fetch();
long result = (registers[reg] | lit) & 0xffffffff;
registers[reg] = result;
break;
}
case Instructions.OR_REG_REG: {
byte reg1 = fetch();
byte reg2 = fetch();
long result = (registers[reg2] | registers[reg1]) & 0xffffffff;
registers[reg2] = result;
break;
}
case Instructions.XOR_LIT_REG: {
byte lit = fetch();
byte reg = fetch();
long result = (registers[reg] ^ lit) & 0xffffffff;
registers[reg] = result;
break;
}
case Instructions.XOR_REG_REG: {
byte reg1 = fetch();
byte reg2 = fetch();
long result = (registers[reg2] ^ registers[reg1]) & 0xffffffff;
registers[reg2] = result;
break;
}
case Instructions.NOT: {
byte reg1 = fetch();
long result = (~registers[reg1]) & 0xffffffff;
registers[reg1] = result;
break;
}
}
}
public void run(){
running = true;
while(running){
step();
}
}
public static void main(String[] args) {
CPU myCPU = new CPU();
//EDI=Base
//ESI=Exponent
int i = 0;
myCPU.memory[i++] = Instructions.MOV_LIT_REG;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x05;
myCPU.memory[i++] = Registers.edi;
myCPU.memory[i++] = Instructions.MOV_LIT_REG;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x05;
myCPU.memory[i++] = Registers.esi;
myCPU.memory[i++] = Instructions.MOV_LIT_REG;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x01;
myCPU.memory[i++] = Registers.eax;
i=0x20;
//LOOP: 32 / 0x20
myCPU.memory[i++] = Instructions.CMP_LIT_REG;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = Registers.esi;
myCPU.memory[i++] = Instructions.JE_LIT;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x04;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = Registers.esi;
myCPU.memory[i++] = Instructions.MUL_REG;
myCPU.memory[i++] = Registers.edi;
myCPU.memory[i++] = Instructions.SUB_LIT_REG;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x01;
myCPU.memory[i++] = Registers.esi;
myCPU.memory[i++] = Instructions.JMP_LIT;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x20;
//After:
i = 0x400;
myCPU.memory[i++] = Instructions.MOV_REG_REG;
myCPU.memory[i++] = Registers.eax;
myCPU.memory[i++] = Registers.esi;
myCPU.memory[i++] = Instructions.MOV_LIT_REG;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = 0x10;
myCPU.memory[i++] = 0x00;
myCPU.memory[i++] = Registers.edi;
myCPU.memory[i++] = Instructions.CALL_PRINTF;
myCPU.memory[i++] = Instructions.CALL_EXIT;
i = 0x1000;
myCPU.memory[i++] = (char) '%';
myCPU.memory[i++] = (char) 'd';
myCPU.memory[i++] = 0x00;
myCPU.run();
}
}