c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

sim86.cpp (1978B)


      1 #include "sim86.h"
      2 
      3 #include "sim86_memory.h"
      4 #include "sim86_text.h"
      5 #include "sim86_decode.h"
      6 
      7 #include "sim86_memory.cpp"
      8 #include "sim86_text.cpp"
      9 #include "sim86_decode.cpp"
     10 
     11 static void DisAsm8086(memory *Memory, u32 DisAsmByteCount, segmented_access DisAsmStart)
     12 {
     13     segmented_access At = DisAsmStart;
     14     
     15     disasm_context Context = DefaultDisAsmContext();
     16     
     17     u32 Count = DisAsmByteCount;
     18     while(Count)
     19     {
     20         instruction Instruction = DecodeInstruction(&Context, Memory, &At);
     21         if(Instruction.Op)
     22         {
     23             if(Count >= Instruction.Size)
     24             {
     25                 Count -= Instruction.Size;
     26             }
     27             else
     28             {
     29                 fprintf(stderr, "ERROR: Instruction extends outside disassembly region\n");
     30                 break;
     31             }
     32             
     33             UpdateContext(&Context, Instruction);
     34             printf("%s", GetMnemonic(Instruction.Op));
     35             unsigned char before = 0x10;
     36             unsigned char after = 12;
     37             printf("    ; 0x%x->0x%x", before, after);
     38             printf("\n");
     39 
     40 //            if(IsPrintable(Instruction))
     41  //           {
     42    //             PrintInstruction(Instruction, stdout);
     43 
     44      //       }
     45 
     46         }
     47         else
     48         {
     49             fprintf(stderr, "ERROR: Unrecognized binary in instruction stream.\n");
     50             break;
     51         }
     52     }
     53 }
     54 
     55 int main(int ArgCount, char **Args)
     56 {
     57     memory *Memory = (memory *)malloc(sizeof(memory));
     58     
     59     if(ArgCount > 1)
     60     {
     61         for(int ArgIndex = 1; ArgIndex < ArgCount; ++ArgIndex)
     62         {
     63             char *FileName = Args[ArgIndex];
     64             u32 BytesRead = LoadMemoryFromFile(FileName, Memory, 0);
     65             
     66             printf("; %s disassembly:\n", FileName);
     67             printf("bits 16\n");
     68             DisAsm8086(Memory, BytesRead, {});
     69         }
     70     }
     71     else
     72     {
     73         fprintf(stderr, "USAGE: %s [8086 machine code file] ...\n", Args[0]);
     74     }
     75     
     76     return 0;
     77 }