Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* instruction table:
- * -------------------
- * OPCODE MNEMONIC FORMAT
- * 00 LDA 3/4
- * 0C STA 3/4
- * XX ADDR 2
- */
- struct instruction {
- unsigned char opcode;
- char opcode[6] mnemonic;
- int format; /* 1: format 1, 2: format 2, 3: format 3/4 */
- };
- unsigned int LC = 1000;
- while (LC < 2000) {
- unsigned int instruction_address = LC;
- unsigned char indx; /* indexed? */
- unsigned char indirect; /* indirect? */
- unsigned char immediate; /* immediate? */
- unsigned char baserel; /* base relative? */
- unsigned char pcrel; /* pc relative? */
- unsigned char absolute; /* format 4? */
- unsigned char operand[9]; /* the operand */
- /* read the byte stored at LC, then increase LC by one */
- unsigned byte opcode = get_byte_from_memory(LC++);
- /* translate the opcode (using the lookup table described above) */
- struct instruction *instr = opcode_to_instruction(opcode);
- /* now disassemble the instruction based on its format */
- if (instr->format == 1) {
- /* format one, the easiest one! */
- indx = 0;
- indirect = 0;
- immediate = 0;
- baserel = 0;
- pcrel = 0;
- absolute = 0;
- strcpy(operand, "");
- } else if (instr->format == 2) {
- /* format 2, read an extra byte (the registers) */
- unsigned char regs = get_byte_from_memory(LC++);
- unsigned char r1 = regs>>4;
- unsigned char r2 = regs & 0x0F;
- operand[0] = to_register(r1);
- operand[1] = ',';
- operand[2] = to_register(r2);
- operand[3] = 0;
- indx = 0;
- indirect = 0;
- immediate = 0;
- baserel = 0;
- pcrel = 0;
- absolute = 0;
- } else {
- /* format 3/4 */
- unsigned char byte2 = get_byte_from_memory(LC++);
- unsigned char byte3 = get_byte_from_memory(LC++);
- indirect = (opcode & 0x02)>>1;
- immediate = (opcode & 0x01)>>1;
- indx = (byte2 & 0x80)>>7;
- baserel = (byte2 & 0x40)>>6;
- pcrel = (byte2 & 0x20)>>5;
- absoulte = (byte2 & 0x10)>>4;
- if (absolute) {
- /* format 4 */
- unsigned char byte4 = get_byte_from_memory(LC++);
- unsigned int address = ((byte2 & 0x0F)<<16) +
- ((byte3 & 0xFF)<< 8) +
- ((byte4 & 0xFF)<< 0);
- integer_to_string(operand, address);
- } else {
- /* format 3 */
- unsigned int address = ((byte2 & 0x0F)<<8) +
- ((byte3 & 0xFF)<<0);
- /* handle relativity */
- if (baserel) {
- address += BASE;
- } else if (pcrel) (
- /* FIXME: Two's complement issue */
- address += LC;
- }
- integer_to_string(operand, address);
- }
- }
- /* now print the original instruction */
- printf("At %6x : ", instruction_address);
- /* format 4?? add + */
- printf("%c", abs ? '+':' ');
- /* print the mnemonic */
- printf("%s ", instr->mnemonic);
- /* immediate? add # */
- if (immediate)
- printf("#");
- /* indirect? add @ */
- if (indirect)
- printf("@");
- /* print the operand */
- printf(operand);
- /* indexed? add ,X */
- if (indexed)
- printf(",X");
- /* done */
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement