-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_opcode.c
517 lines (411 loc) · 12 KB
/
vm_opcode.c
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
/*
* Dated : 05/August/2014 , 16:43PM
* Author Amber N Pachauri
* Purpose: This is TurboC 3.0/ DOS program utility created with concept
* of Virtual-Machine, and it runs based on commands being passed
* with each command instruction is in itself encrypted data
* for which gets decoded and stored and read back again.
*
* The VM is like dynamic stack which keeps un/rolling with the
* kind of data being read from sources of kind : *.bin fles.
*
* These files are provided from within the code respository.
* Files are like task1.bin; task2.bin and list of test cases files
* testxx.bin
*
* For better understandng have used variales, functions and
* code layout almost equivalent as speciied in task1.txt.
*
* On executing this module two files are created directories
* - task1/result.txt
* _ task2/result.txt
*/
#include<stdio.h>
#include<stdlib.h>
/*-----------------------------------------------
* some gobal declarations/definations , which are used
* across functions and VM runtime environment
*------------------------------------------------*/
long int ip, sp, sptr, *pCurrInstr;
//variable to hold address locations.
long int **pData = NULL;
FILE *pTask, *pTask1, *pTask2; // file pointer
//variable to hold, extracted decoded values
//in form of operation, Optional data and Binary operation.
long int iOper = 0x00;
long int iOpt = 0x00;
long int iBinop;
//variable to indicate STOP running VM.
int iVMStop;
#define VM_FILE_PATH "e:\\task1.bin"
//task2.bin
//#define VM_TEST_PATH "e:\\test25.bin"
//#define VM_TEST_PATH "e:\\test25.bin"
//some test files test01.bin...test25.bin
#define VM_FILE_TASK1 "e:\\task1\\result.txt"
#define VM_FILE_TASK2 "e:\\task2\\result.txt"
// some forward declarations
void perform_VM(int iBinoper , int iOperation);
//execute VM operations.
void decode_VM(long int *pDataCode);
//decode instruction in VM files.
/*-----------------------------------------------
* some gobal definations , which are used
* across VM runtime environment
*------------------------------------------------*/
void f(long int iOptionalData)
{ // routine to allow data to be stored
// and stack pointer is decremented
sp = sp -1;
fprintf(pTask2,"storing-%ld at loc:%d\n", iOptionalData, sp);
*pData[sp] = iOptionalData;
}
long int g(void)
{ // routine which allows to read back data
// and send to stdout when requested.
long int iTmp = *pData[sp];
sp = sp + 1;
fprintf(pTask2,"return data-%ld\n", iTmp);
return iTmp;
}
/* This module is used to decode the
* data values and further perform operations,
* assupplied from data-array, while reading insruction
*
* Parameters:
* pDataCode - holds the encrypted data.
*
* Bits are aligned from MSB-LSB ; left->right 31..0;
* bits position :
* position 31 : binop , 1-bit.
* positin 30-23 : operation, 7-bits.
* position 23-0 : optional data, 24-bits
*/
void decode_VM(long int *pDataCode)
{
//exract binop
long int iMask = 0x01;
long int iCheck, iValue;
long int iTmp = 0x00;
//int j, i =0, j = 0;
iBinop = iOpt = iOper = 0x00;
iValue = iCheck = *pDataCode;
fprintf(pTask2,"received instr(%ld) \nDecoding..", *pDataCode);
iMask |= 1 << 31;
iValue = iValue & iMask;
iBinop &= iValue;
fprintf(pTask2,"binop-%d ;\t", iBinop);
//extract operation
iValue |= *pDataCode;
//iValue |= iCheck;
//printf("val1 =%ld; chk1-%ld", iValue, iCheck);
iTmp |= (iValue >> 24);
//printf("\ntmp =%ld; chk3-%ld", iTmp, iCheck);
iTmp &= 0xfff; //set those particular pattern
iOper |= iTmp;
fprintf(pTask2,"oper-%d ;\t", iOper);
//getchar();
//extract the optiona bits
iOpt |= *pDataCode;
//printf("data-%ld..\t", iOpt);
iOpt &= 0xfff; //set the lower 24 bits;
fprintf(pTask2,"optional-%d.\n", iOpt);
}
/* This routine is used to perform operations over the
* contents in dynamic stack, based on the few arguments
* extracted before - operations, optional data, and Binop
*
* Parameters:
* iBinoper - '1' or '0' as specified.
* iOperation - 0..10 as specified.
*/
void perform_VM(int iBinoper , int iOperation)
{
long int addr, st_data;
long int res, a, b, tData;
char oneByte;
long int cond, x32Bits;
switch(iOperation)
{
// operations states for VM to operate with.
/* Note : there is problem or some of the case
* operation bit values is not defined ?? or may be
* code implemented below is bit incorrect; for example
* case bit value for 127, 15 etc.
* please re-check?
* if its signed bit should i carry over and assume
* 127 as 0.
*/
//case 127:
case 0:
{
if (iBinoper){///0=> add, <op> is +
fprintf(pTask2,"0=> add, <op> is +\n");
b = g();
a = g();
res &= (a+b);
f(res);
}else{ //pop sp= sp+1;
fprintf(pTask2,"incr 'sp'\n");
sp = sp +1;
}
}break;
//case 127:
case 1:
{
if ( iBinoper) { /// 1= sub, <op> is -
fprintf(pTask2,"1=> sub, <op> is -\n");
b = g();
a = g();
res &= (a-b);
f(res);
}else { //push const(optinal)
fprintf(pTask2,"push 'constant'\n");
f(iOpt);
}
}break;
case 2:
{
if (iBinoper) {//2= mul, <op> is *
fprintf(pTask2,"2=> mul, <op> is *\n");
b = g();
a = g();
res &= (a*b);
f (res);
}else { //push ip
fprintf(pTask2,"push 'ip'\n");
f(ip);
}
}break;
case 3:
{
if (iBinoper) { ///3-div,<op> is /
fprintf(pTask2,"3=>div,<op> is / \n");
b = g();
a = g();
res &= (a/b);
f (res);
} else { //push sp
fprintf(pTask2,"push 'sp' \n");
f(sp);
}
}break;
case 4: //load
{
if (iBinoper){ ///4-and, <op> is &
fprintf(pTask2,"4=> and, <op> is & \n");
b = g();
a = g();
res &= (a&b);
f (res);
}else {
fprintf(pTask2,"load addr\n");
addr = g();
f(*pData[addr]);
}
}break;
case 5: //store
{
if (iBinoper) { ///5- or ,<op> is |
fprintf(pTask2,"5=> or ,<op> is |\n");
b = g();
a = g();
res &= (a|b);
f (res);
} else {
fprintf(pTask2,"store addr\n");
st_data = g();
addr = g();
*pData[addr] = st_data;
}
}break;
case 6: //jmp
{
if (iBinoper) { ///6- xor ,<op> is ^
fprintf(pTask2,"6=> xor ,<op> is ^\n");
b = g();
a = g();
res &= (a^b);
f(res);
}else {
fprintf(pTask2,"implement setjmp\n");
cond = g();
addr = g();
if (0 != cond) {
ip = addr;
}
}
}break;
case 7:
{
if (iBinoper) { //7- eq ,<op> is =
fprintf(pTask2,"7 => eq ,<op> is = \n");
b = g();
a = g();
res = ((a==b) ? 1 : 0);
f(res);
} else { //not
fprintf(pTask2," not condition\n");
if ( !g() )
f(1);
else
f(0);
}
}break;
case 127:
/* note : often the operational bit '8'
* in which case should be binary map
* msb->lsb : 10000000 can be assumed as same??
* More precisely there is no action specified
* for task1.txt for such case - should assume
* carry-over bit?
* So either to make it more sensibe for me it could
* be carried over bit? in which case i should call
* case '0' or lets see and check out if we print
* charcters to stdout??*/
case 8: //putc
{
if (iBinoper) {
//8- lt ,<op> is <
b = g();
a = g();
res = ((a<b) ? 1 : 0);
f(res);
}else {
//output exactly one-byte to
//stdout, ASCII text
tData = g();
oneByte = (char) (tData&0xff);
//printf("byte2stdout: %c\n", oneByte);
//putc(oneByte,stdout);
fprintf(pTask2,"byte2stdout: %c\n" ,oneByte);
}
}break;
case 9:
{ //read exactly one byte from
//stdin and cast to 32bts
printf("enter onebyte:\n");
getchar();
oneByte = getc(stdin);
x32Bits = (long int)oneByte;
f( x32Bits & 0xff);
}break;
case 10: //halt
{//0x0A
fprintf(pTask2,"\nVirtual-Machine, halting.\n");
iVMStop = 1;
}break;
//no default case;
default:
fprintf(pTask2,"operation(%d) not defined..\n",iOperation);
}
}
/* The VM main entry point*/
int main(int argc , char* argv[])
{
int i, iDataIndex =0;
int iGetDataSz, iGetImageSz;
// flags to check inernal state machine.
long int iDataSz; //variabe for data-size
long int iImageSz; //variable for image-size
char ch, cData[10];
//variabe to reading each lines , character-by-character
iImageSz = iDataSz = iGetDataSz = iGetImageSz =0;
pTask = pTask1 = pTask2 = NULL;
pTask = fopen(VM_FILE_PATH, "r");
//pTask = fopen(VM_TEST_PATH, "r");
//pTask = fopen(VM_FILE1_PATH, "r");
// open the output file
pTask1 = fopen(VM_FILE_TASK1, "w+");
if ( !pTask1 ){
printf("No output task-1 file, exiting");
exit(0);
}
//check if file exists?
if ( pTask != NULL) {
fprintf(pTask1,"VM opened, successfuly.\n");
i =0;
cData[i] = 0;
printf("\n VM & output files opened.");
printf("\n Running VM tasks...");
while ( !feof (pTask) ) // read whole file
{
ch = fgetc(pTask);
if ( ch == '\n'){ //read each line
cData[i] = 0;
fprintf(pTask1, "VM-entry: %s\n", cData);
//getchar();
cData[i] = 0;
if( !iGetDataSz ) {
//convert to datasize
iGetDataSz =1;
iDataSz = strtol(cData, 0, 16);
fprintf(pTask1, "VM data-size: %ld\n", iDataSz);
//allocate fordata-size elemsts
pData = (long int**) malloc (sizeof(long int*) * (iDataSz) );
for ( i =0; i< iDataSz; i++){
//allocate all pointers to array index length - 32bits.
pData[i] = (long int*) malloc ( sizeof (long int));
}
i=0;
continue;
}//data-size
if( !iGetImageSz ) {
//convert to imagesize
iGetImageSz =1;
iImageSz = strtol(cData, 0, 16);
fprintf(pTask1,"VM Image-size: %ld\n", iImageSz);
i=0;
continue;
}//image-size
//load VM
if(( pData != NULL) && (iGetImageSz)){
fprintf(pTask1,"VM data: %ld \n", strtol(cData, 0 ,16));
*pData[iDataIndex++] = strtol(cData, 0, 16);
i =0;
continue;
}
}//reached newline check??
cData[i++] = ch; //keep reading char
}// finished loading VM
fprintf(pTask1,"\nVM stack-enteries-%d", iDataIndex);
printf("\n finished loading VM .");
/* Safe to close VM file - VM data is read
* and loaded into memory. */
fclose(pTask);
fclose(pTask1);
pTask1=pTask = NULL;
pTask2 = fopen(VM_FILE_TASK2, "w+");
if ( !pTask2 ){
printf("No output task2-file, exiting");
exit(0);
}
//continue...to Interpret VM images
ip = 0;
//sptr = iDataIndex;
sp = iDataSz;
iVMStop = 0;
printf("\n Executing VM tasks...");
do {
//execution of VM
pCurrInstr = pData[ip++];
//ip++;
fprintf(pTask2,"\n\nSending-%ld ;\t", *pCurrInstr);
printf("\nReading memory: %ld", *pCurrInstr);
decode_VM(pCurrInstr);
//pass the instruction, decode the bits
perform_VM(iBinop, iOper);
//perform VM related, operations.
if (iVMStop)
break;
}while(1);
printf(" \n VM halted.");
fclose(pTask2);
free(pData);
}
else
printf("file NOT exists..\n");
//getchar();
//stop console from exiting!!
return 0;
}