Advertisement
bottomy

Stage 2 VM (no logging)

Sep 10th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.63 KB | None | 0 0
  1. /*
  2. http://www.hackforums.net/showthread.php?tid=1971988&pid=17935062#pid17935062
  3. */
  4.  
  5.  
  6. /* Without Logging */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10.  
  11. void jmp(const char, const int, const int);
  12. void movr(const char, const int, const int);
  13. void movm(const char, const int, const int);
  14. void add(const char, const int, const int);
  15. void xor(const char, const int, const int);
  16. void cmp(const char, const int, const int);
  17. void jmpe(const char, const int, const int);
  18. void hlt(const char, const int, const int);
  19.  
  20.  
  21. void (*Instruction[])(const char, const int, const int) = {
  22.     jmp, movr, movm, add, xor, cmp, jmpe, hlt
  23. };
  24.  
  25. enum {
  26.     REG_r0, REG_r1, REG_r2, REG_r3,
  27.     REG_cs, REG_ds,
  28.     REG_fl, REG_ip,
  29.     REG_Total
  30. };
  31.  
  32. int Registers[REG_Total] = { 0, 0, 0, 0, 0, 0x10, 0, 0 };
  33.  
  34. char Mem[] = {
  35.     0x31, 0x04, 0x33, 0xaa, 0x40, 0x02, 0x80, 0x03, 0x52, 0x00, 0x72, 0x01, 0x73, 0x01, 0xb2, 0x50,
  36.     0x30, 0x14, 0xc0, 0x01, 0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  47.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51.    
  52.     0x98, 0xab, 0xd9, 0xa1, 0x9f, 0xa7, 0x83, 0x83, 0xf2, 0xb1, 0x34, 0xb6, 0xe4, 0xb7, 0xca, 0xb8,
  53.     0xc9, 0xb8, 0x0e, 0xbd, 0x7d, 0x0f, 0xc0, 0xf1, 0xd9, 0x03, 0xc5, 0x3a, 0xc6, 0xc7, 0xc8, 0xc9,
  54.     0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
  55.     0xda, 0xdb, 0xa9, 0xcd, 0xdf, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  56.     0x26, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
  57.     0x7d, 0x1f, 0x15, 0x60, 0x4d, 0x4d, 0x52, 0x7d, 0x0e, 0x27, 0x6d, 0x10, 0x6d, 0x5a, 0x06, 0x56,
  58.     0x47, 0x14, 0x42, 0x0e, 0xb6, 0xb2, 0xb2, 0xe6, 0xeb, 0xb4, 0x83, 0x8e, 0xd7, 0xe5, 0xd4, 0xd9,
  59.     0xc3, 0xf0, 0x80, 0x95, 0xf1, 0x82, 0x82, 0x9a, 0xbd, 0x95, 0xa4, 0x8d, 0x9a, 0x2b, 0x30, 0x69,
  60.     0x4a, 0x69, 0x65, 0x55, 0x1c, 0x7b, 0x69, 0x1c, 0x6e, 0x04, 0x74, 0x35, 0x21, 0x26, 0x2f, 0x60,
  61.     0x03, 0x4e, 0x37, 0x1e, 0x33, 0x54, 0x39, 0xe6, 0xba, 0xb4, 0xa2, 0xad, 0xa4, 0xc5, 0x95, 0xc8,
  62.     0xc1, 0xe4, 0x8a, 0xec, 0xe7, 0x92, 0x8b, 0xe8, 0x81, 0xf0, 0xad, 0x98, 0xa4, 0xd0, 0xc0, 0x8d,
  63.     0xac, 0x22, 0x52, 0x65, 0x7e, 0x27, 0x2b, 0x5a, 0x12, 0x61, 0x0a, 0x01, 0x7a, 0x6b, 0x1d, 0x67,
  64.     0x75, 0x70, 0x6c, 0x1b, 0x11, 0x25, 0x25, 0x70, 0x7f, 0x7e, 0x67, 0x63, 0x30, 0x3c, 0x6d, 0x6a,
  65.     0x01, 0x51, 0x59, 0x5f, 0x56, 0x13, 0x10, 0x43, 0x19, 0x18, 0xe5, 0xe0, 0xbe, 0xbf, 0xbd, 0xe9,
  66.     0xf0, 0xf1, 0xf9, 0xfa, 0xab, 0x8f, 0xc1, 0xdf, 0xcf, 0x8d, 0xf8, 0xe7, 0xe2, 0xe9, 0x93, 0x8e,
  67.     0xec, 0xf5, 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68.    
  69.     0x37, 0x7a, 0x07, 0x11, 0x1f, 0x1d, 0x68, 0x25, 0x32, 0x77, 0x1e, 0x62, 0x23, 0x5b, 0x47, 0x55,
  70.     0x53, 0x30, 0x11, 0x42, 0xf6, 0xf1, 0xb1, 0xe6, 0xc3, 0xcc, 0xf8, 0xc5, 0xe4, 0xcc, 0xc0, 0xd3,
  71.     0x85, 0xfd, 0x9a, 0xe3, 0xe6, 0x81, 0xb5, 0xbb, 0xd7, 0xcd, 0x87, 0xa3, 0xd3, 0x6b, 0x36, 0x6f,
  72.     0x6f, 0x66, 0x55, 0x30, 0x16, 0x45, 0x5e, 0x09, 0x74, 0x5c, 0x3f, 0x29, 0x2b, 0x66, 0x3d, 0x0d,
  73.     0x02, 0x30, 0x28, 0x35, 0x15, 0x09, 0x15, 0xdd, 0xec, 0xb8, 0xe2, 0xfb, 0xd8, 0xcb, 0xd8, 0xd1,
  74.     0x8b, 0xd5, 0x82, 0xd9, 0x9a, 0xf1, 0x92, 0xab, 0xe8, 0xa6, 0xd6, 0xd0, 0x8c, 0xaa, 0xd2, 0x94,
  75.     0xcf, 0x45, 0x46, 0x67, 0x20, 0x7d, 0x44, 0x14, 0x6b, 0x45, 0x6d, 0x54, 0x03, 0x17, 0x60, 0x62,
  76.     0x55, 0x5a, 0x4a, 0x66, 0x61, 0x11, 0x57, 0x68, 0x75, 0x05, 0x62, 0x36, 0x7d, 0x02, 0x10, 0x4b,
  77.     0x08, 0x22, 0x42, 0x32, 0xba, 0xe2, 0xb9, 0xe2, 0xd6, 0xb9, 0xff, 0xc3, 0xe9, 0x8a, 0x8f, 0xc1,
  78.     0x8f, 0xe1, 0xb8, 0xa4, 0x96, 0xf1, 0x8f, 0x81, 0xb1, 0x8d, 0x89, 0xcc, 0xd4, 0x78, 0x76, 0x61,
  79.     0x72, 0x3e, 0x37, 0x23, 0x56, 0x73, 0x71, 0x79, 0x63, 0x7c, 0x08, 0x11, 0x20, 0x69, 0x7a, 0x14,
  80.     0x68, 0x05, 0x21, 0x1e, 0x32, 0x27, 0x59, 0xb7, 0xcf, 0xab, 0xdd, 0xd5, 0xcc, 0x97, 0x93, 0xf2,
  81.     0xe7, 0xc0, 0xeb, 0xff, 0xe9, 0xa3, 0xbf, 0xa1, 0xab, 0x8b, 0xbb, 0x9e, 0x9e, 0x8c, 0xa0, 0xc1,
  82.     0x9b, 0x5a, 0x2f, 0x2f, 0x4e, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  85. };
  86.  
  87. int main(int argc, const char *argv[])
  88. {
  89.     for ( ; ; )
  90.     {
  91.         const char Val = Mem[Registers[REG_ip]];
  92.         char OpCode = (Val >> 5) & 7, Mod = (Val >> 4) & 1, Op1 = Val & 7, Op2 = Mem[Registers[REG_ip] + 1];
  93.        
  94.         Instruction[(int)OpCode](Mod, Op1, Op2);
  95.     }
  96.    
  97.     return 0;
  98. }
  99.  
  100. void jmp(const char mod, const int op1, const int op2)
  101. {
  102.     if (mod)
  103.     {
  104.         Registers[REG_ip] = (op2 * 16) + Registers[op1];
  105.         Registers[REG_cs] = op2;
  106.     }
  107.    
  108.     else
  109.     {
  110.         Registers[REG_ip] = (Registers[REG_cs] * 16) + Registers[op1];
  111.     }
  112. }
  113.  
  114. void movr(const char mod, const int op1, const int op2)
  115. {
  116.     Registers[op1] = mod? op2 : Registers[op2];
  117.     Registers[REG_ip] += 2;
  118. }
  119.  
  120. void movm(const char mod, const int op1, const int op2)
  121. {
  122.     if (mod)
  123.     {
  124.         Mem[(Registers[REG_ds] * 16) + Registers[op1]] = Registers[op2];
  125.     }
  126.    
  127.     else
  128.     {
  129.         Registers[op1] = Mem[(Registers[REG_ds] * 16) + Registers[op2]];
  130.     }
  131.    
  132.     Registers[REG_ip] += 2;
  133. }
  134.  
  135. void add(const char mod, const int op1, const int op2)
  136. {
  137.     Registers[op1] += mod? op2 : Registers[op2];
  138.     Registers[REG_ip] += 2;
  139. }
  140.  
  141. void xor(const char mod, const int op1, const int op2)
  142. {
  143.     Registers[op1] ^= mod? op2 : Registers[op2];
  144.     Registers[REG_ip] += 2;
  145. }
  146.  
  147. void cmp(const char mod, const int op1, const int op2)
  148. {
  149.     char a = Registers[op1], b = mod? op2 : Registers[op2];
  150.    
  151.     if (a == b) Registers[REG_fl] = 0;
  152.     else if (a < b) Registers[REG_fl] = 0xff;
  153.     else if (a > b) Registers[REG_fl] = 1;
  154.    
  155.     Registers[REG_ip] += 2;
  156. }
  157.  
  158. void jmpe(const char mod, const int op1, const int op2)
  159. {
  160.     if (Registers[REG_fl] == 0) jmp(mod, op1, op2);
  161.     else Registers[REG_ip] += 1 + mod;
  162. }
  163.  
  164. void hlt(const char mod, const int op1, const int op2)
  165. {
  166.     printf("Halted, should output memory to file!\n");
  167.    
  168.     FILE *fp = fopen("iuenv31eEmOoe3434134urchqe54ucnDFmkd3ofun3c9nrv13ic3ir.txt", "wb");
  169.    
  170.     if (fp)
  171.     {
  172.         fwrite(Mem, sizeof(Mem), 1, fp);
  173.         fclose(fp);
  174.     }
  175.    
  176.     exit(0);
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement