Guest User

execute.c

a guest
Nov 27th, 2022
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | Source Code | 0 0
  1. #include "defs.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5.  
  6. static uint16_t g_mem[0x10000];
  7. static uint16_t g_reg[4];
  8.  
  9. uint16_t getSrcVal(uint16_t src, uint16_t srcType)
  10. {
  11.   switch(srcType)
  12.   {
  13.     case Const : return src;
  14.     case Reg   : return g_reg[src];
  15.     case Mem   : return g_mem[src];
  16.     default    : return 0;
  17.   }
  18. }
  19.  
  20. void setDstVal(uint16_t dst, uint16_t dstType, uint16_t val)
  21. {
  22.   switch(dstType)
  23.   {
  24.     case Reg: g_reg[dst] = val; break;
  25.     case Mem: g_mem[dst] = val; break;
  26.   }
  27. }
  28.  
  29. int main(int argc, char **argv)
  30. {
  31.   command_t *prog;
  32.   size_t size;
  33.  
  34.   {
  35.     FILE *file = fopen(argv[1], "rb");
  36.     fseek(file, 0, SEEK_END);
  37.     size = ftell(file) / sizeof(command_t);
  38.     rewind(file);
  39.     prog = calloc(size, sizeof(command_t));
  40.     fread(prog, sizeof(command_t), size, file);
  41.     fclose(file);
  42.   }
  43.  
  44.   for(command_t *cmd = prog; cmd < prog + size; cmd++)
  45.   {
  46.     switch(cmd->opCode)
  47.     {
  48.       case Mov:
  49.       {
  50.         uint16_t srcVal = getSrcVal(cmd->src, cmd->srcType);
  51.         setDstVal(cmd->dst, cmd->dstType, srcVal);
  52.       }
  53.       break;
  54.  
  55.       #define APPLYOP(apply, op)                              \
  56.       {                                                       \
  57.         uint16_t dstVal = getSrcVal(cmd->dst, cmd->dstType);  \
  58.         uint16_t srcVal = getSrcVal(cmd->src, cmd->srcType);  \
  59.         uint16_t result = apply(op, dstVal, srcVal);          \
  60.         setDstVal(cmd->dst, cmd->dstType, result);            \
  61.       }                                                       \
  62.       break;
  63.  
  64.       #define UNARY(op, dst, src) (op src)
  65.       #define BINARY(op, dst, src) (dst op src)
  66.       #define BINARYOFNOT(op, dst, src) (!dst op !src)
  67.  
  68.       case Add  : APPLYOP(BINARY      ,  +);
  69.       case Sub  : APPLYOP(BINARY      ,  -);
  70.       case Mul  : APPLYOP(BINARY      ,  *);
  71.       case Div  : APPLYOP(BINARY      ,  /);
  72.       case Mod  : APPLYOP(BINARY      ,  %);
  73.       case Not  : APPLYOP(UNARY       ,  !);
  74.       case And  : APPLYOP(BINARY      , &&);
  75.       case Or   : APPLYOP(BINARY      , ||);
  76.       case Xor  : APPLYOP(BINARYOFNOT , !=);
  77.       case BNot : APPLYOP(UNARY       ,  ~);
  78.       case BAnd : APPLYOP(BINARY      ,  &);
  79.       case BOr  : APPLYOP(BINARY      ,  |);
  80.       case BXor : APPLYOP(BINARYOFNOT ,  ^);
  81.  
  82.       #undef BINARYOFNOT
  83.       #undef BINARY
  84.       #undef UNARY
  85.       #undef APPLYOP
  86.  
  87.       case Jmp:
  88.       {
  89.         uint16_t addr = getSrcVal(cmd->dst, cmd->dstType);
  90.         cmd = &prog[addr-1];
  91.       }
  92.       break;
  93.       case JmpIf:
  94.       {
  95.         uint16_t addr = getSrcVal(cmd->dst, cmd->dstType);
  96.         uint16_t cond = getSrcVal(cmd->src, cmd->srcType);
  97.         if(cond)
  98.           cmd = &prog[addr-1];
  99.       }
  100.       break;
  101.       case Read:
  102.       {
  103.         setDstVal(cmd->dst, cmd->dstType, getchar());
  104.       }
  105.       break;
  106.       case Write:
  107.       {
  108.         putchar(getSrcVal(cmd->src, cmd->srcType));
  109.       }
  110.       break;
  111.     }
  112.   }
  113.  
  114.   return 0;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment