Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define ARG_NORM 0
- #define ARG_JR 1
- #define ARG_OFF 2
- void print_hexadecimal(uint8_t, FILE*);
- char *address_string(int);
- char *byteToHexString(uint8_t);
- int address = 0xD1A881;
- const char hexadecimals[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
- const char *cc_table[] = {"nz", "z", "nc", "c", "po", "pe", "p", "m"};
- const char *suffixes[] = {".sis", ".lis", ".sil", ".lil"};
- char addressString[7];
- char hex[3] = {0, 0, 0};
- typedef struct {
- char *opcodeName;
- char *opcodeOperand;
- uint8_t opcodeLength;
- uint8_t argType;
- } opcodes_t;
- extern const opcodes_t opcodes[256];
- typedef struct {
- char opcode[10];
- char operand[50];
- } disassembly;
- disassembly disasm;
- int main(int argc, char **argv) {
- uint8_t inputBytes[6];
- uint8_t inputWhichByte = 0;
- uint8_t whichSuffix = 0;
- uint8_t byte;
- uint8_t index;
- char* spaces = " ";
- int offset = 0x4C;
- int programSize;
- FILE *inPrgm;
- FILE *outPrgm;
- if (argc != 3) {
- fprintf(stderr, "Error: invalid arguments\n");
- exit(1);
- }
- inPrgm = fopen(argv[1], "r");
- outPrgm = fopen(argv[2], "w");
- if (!inPrgm || !outPrgm) {
- fprintf(stderr, "Error: can't open input or output file\n");
- }
- fseek(inPrgm, -2, SEEK_END);
- programSize = ftell(inPrgm);
- fseek(inPrgm, 0x4C, SEEK_SET);
- fprintf(outPrgm, "Disassembly of %s:\n\n", argv[1]);
- while (offset < programSize) {
- fprintf(stderr, "%d - ", ftell(inPrgm));
- byte = fgetc(inPrgm);
- inputBytes[inputWhichByte++] = byte;
- // Check suffixes
- if (byte == 0x40) {
- whichSuffix = 1;
- continue;
- } else if (byte == 0x49) {
- whichSuffix = 2;
- continue;
- } else if (byte == 0x52) {
- whichSuffix = 3;
- continue;
- } else if (byte == 0x5B) {
- whichSuffix = 4;
- continue;
- } else {
- whichSuffix = 0;
- // Check 2-byte opcode
- if (byte == 0xCB) {
- inputWhichByte++;
- } else if (byte == 0xDD) {
- inputWhichByte++;
- } else if (byte == 0xED) {
- inputWhichByte++;
- } else if (byte == 0xFD) {
- inputWhichByte++;
- } else {
- // Single byte opcode
- strcpy(disasm.opcode, opcodes[byte].opcodeName);
- if (whichSuffix) {
- strcat(disasm.opcode, suffixes[whichSuffix - 1]);
- }
- if (opcodes[byte].opcodeLength == 1) {
- if (opcodes[byte].argType == ARG_NORM) {
- sprintf(disasm.operand, opcodes[byte].opcodeOperand, byteToHexString(inputBytes[inputWhichByte++] = fgetc(inPrgm)));
- } else {
- char byte2 = fgetc(inPrgm);
- inputBytes[inputWhichByte++] = (uint8_t)byte2;
- sprintf(disasm.operand, opcodes[byte].opcodeOperand, address_string(address + byte2 + 2));
- }
- } else if (opcodes[byte].opcodeLength == 3) {
- uint8_t byte2 = fgetc(inPrgm);
- uint8_t byte3 = fgetc(inPrgm);
- uint8_t byte4 = fgetc(inPrgm);
- inputBytes[inputWhichByte++] = byte2;
- inputBytes[inputWhichByte++] = byte3;
- inputBytes[inputWhichByte++] = byte4;
- sprintf(disasm.operand, opcodes[byte].opcodeOperand, address_string(byte2 + (byte3 << 8) + (byte4 << 16)));
- } else {
- strcpy(disasm.operand, opcodes[byte].opcodeOperand);
- }
- }
- }
- // Fix spacing of the opcode
- strcat(disasm.opcode, spaces + strlen(disasm.opcode));
- // Print address to disassembly
- fprintf(outPrgm, address_string(address));
- fprintf(outPrgm, ": ");
- // Print input hexadecimals to disassembly
- for (index = 0; index < inputWhichByte; index++) {
- print_hexadecimal(inputBytes[index], outPrgm);
- }
- for (index = 6; index > inputWhichByte; index--) {
- fprintf(outPrgm, " ");
- }
- // Print opcode, suffix and operand to disassembly, and new line
- fprintf(outPrgm, "%s %s\n", disasm.opcode, disasm.operand);
- address += inputWhichByte;
- offset += inputWhichByte;
- inputWhichByte = 0;
- }
- return 0;
- }
- void print_hexadecimal(uint8_t byte, FILE *outPrgm) {
- fprintf(outPrgm, "%c%c", hexadecimals[byte >> 4], hexadecimals[byte & 0x0F]);
- }
- char *address_string(int address) {
- uint8_t index;
- for (index = 0; index < 6; index++) {
- addressString[index] = hexadecimals[(address >> (20 - 4 * index)) & 0x0F];
- }
- return addressString;
- }
- char *byteToHexString(uint8_t byte) {
- hex[0] = hexadecimals[byte >> 4];
- hex[1] = hexadecimals[byte & 0x0F];
- return hex;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement