Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef unsigned char u8;
- static void PrintRegister(u8 regVal, u8 wide)
- {
- if (!wide)
- {
- switch (regVal)
- {
- case 0b000: printf("AL"); break;
- case 0b001: printf("CL"); break;
- case 0b010: printf("DL"); break;
- case 0b011: printf("BL"); break;
- case 0b100: printf("AH"); break;
- case 0b101: printf("CH"); break;
- case 0b110: printf("DH"); break;
- case 0b111: printf("BH"); break;
- default: printf("Unexpected regval at line: %d",__LINE__); break;
- }
- } else
- {
- switch (regVal)
- {
- case 0b000: printf("AX"); break;
- case 0b001: printf("CX"); break;
- case 0b010: printf("DX"); break;
- case 0b011: printf("BX"); break;
- case 0b100: printf("SP"); break;
- case 0b101: printf("BP"); break;
- case 0b110: printf("SI"); break;
- case 0b111: printf("DI"); break;
- default: printf("Unexpected regval"); break;
- }
- }
- }
- int main(int argc, char **argv)
- {
- char *fileName = argv[1];
- FILE *file = fopen(fileName, "r");
- if (!file)
- {
- printf("Failed to open file: %s\n", fileName);
- }
- fseek(file, 0, SEEK_END);
- long fileSize = ftell(file);
- fseek(file, 0, SEEK_SET);
- for (int i = 0; i < fileSize; i += 2)
- {
- u8 byte1 = (u8) getc(file);
- u8 byte2 = (u8) getc(file);
- if ((byte1 >> 2) == 0b00100010) // MOV instruction 0b00100010=0x22
- {
- printf("MOV ");
- } else
- {
- printf("unsupported");
- }
- u8 d = (byte1 & 0b00000010) >> 1;
- u8 w = (byte1 & 0b00000001);
- u8 mod = (byte2 >> 6); // Ignored for now
- u8 reg = (byte2 & 0b00111000) >> 3;
- u8 rem = (byte2 & 0b00000111);
- if (d)
- {
- // reg first
- PrintRegister(reg, w);
- printf(",");
- PrintRegister(rem, w);
- } else
- {
- // rem first
- PrintRegister(rem, w);
- printf(",");
- PrintRegister(reg, w);
- }
- printf("\n");
- }
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment