Rizin
unix-like reverse engineering framework and cli tools
i8080dis.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2012 Alexander Demin <alexander@demin.ws>
2 // SPDX-License-Identifier: MIT
3 
4 // This file is part of Radio-86RK Tools project.
5 //
6 // Intel 8080 disassembler.
7 //
8 // https://github.com/begoon/rk86-tools
9 //
10 // Copyright (C) 2012 Alexander Demin <alexander@demin.ws>
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files
14 // (the "Software"), to deal in the Software without restriction,
15 // including without limitation the rights to use, copy, modify, merge,
16 // publish, distribute, sublicense, and/or sell copies of the Software,
17 // and to permit persons to whom the Software is furnished to do so,
18 // subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 
31 #include <string.h>
32 #include <stdio.h>
33 #include <assert.h>
34 
35 static char *reg[] = { "b", "c", "d", "e", "h", "l", "m", "a" };
36 static char *rp[] = { "b", "d", "h", "sp" };
37 static char *push_rp[] = { "b", "d", "h", "psw" };
38 static char *cond[] = { "nz", "z", "nc", "c", "po", "pe", "p", "m" };
39 static char *rst[] = { "0", "1", "2", "3", "4", "5", "6", "7" };
40 
41 struct arg_t {
42  int type; /* 1 - next byte, 2 - next word, 3 - in opcode */
43  int shift;
44  int mask;
45  char **fmt;
46 };
47 
48 static struct opcode_t {
49  unsigned char cmd;
50  int size;
51  char *name;
52  struct arg_t arg1, arg2;
53 } opcodes[] = {
54  { 0x76, 1, "hlt" },
55  { 0x06, 2, "mvi", { 3, 3, 7, reg }, { 1 } },
56  { 0xc3, 3, "jmp", { 2 } },
57  { 0x40, 1, "mov", { 3, 3, 7, reg }, { 3, 0, 7, reg } },
58  { 0x01, 3, "lxi", { 3, 4, 3, rp }, { 2 } },
59  { 0x32, 3, "sta", { 2 } },
60  { 0x3a, 3, "lda", { 2 } },
61  { 0x2a, 3, "lhld", { 2 } },
62  { 0x22, 3, "shld", { 2 } },
63  { 0x0a, 1, "ldax", { 3, 4, 1, rp } },
64  { 0x02, 1, "stax", { 3, 4, 1, rp } },
65  { 0xeb, 1, "xchg" },
66  { 0xf9, 1, "sphl" },
67  { 0xe3, 1, "xthl" },
68  { 0xc5, 1, "push", { 3, 4, 3, push_rp } },
69  { 0xc1, 1, "pop", { 3, 4, 3, push_rp } },
70  { 0xdb, 2, "in", { 1 } },
71  { 0xd3, 2, "out", { 1 } },
72  { 0x03, 1, "inx", { 3, 4, 3, rp } },
73  { 0x0b, 1, "dcx", { 3, 4, 3, rp } },
74  { 0x04, 1, "inr", { 3, 3, 7, reg } },
75  { 0x05, 1, "dcr", { 3, 3, 7, reg } },
76  { 0x09, 1, "dad", { 3, 4, 3, rp } },
77  { 0x2f, 1, "cma" },
78  { 0x07, 1, "rlc" },
79  { 0x0f, 1, "rrc" },
80  { 0x17, 1, "ral" },
81  { 0x1f, 1, "rar" },
82  { 0xfb, 1, "ei" },
83  { 0xf3, 1, "di" },
84  { 0x00, 1, "nop" },
85  { 0x37, 1, "stc" },
86  { 0x3f, 1, "cmc" },
87  { 0xe9, 1, "pchl" },
88  { 0x27, 1, "daa" },
89  { 0xcd, 3, "call", { 2 } },
90  { 0xc9, 1, "ret" },
91  { 0xc7, 1, "rst", { 3, 3, 7, rst } },
92  { 0xc0, 1, "r", { 3, 3, 7, cond } },
93  { 0xc2, 3, "j", { 3, 3, 7, cond }, { 2 } },
94  { 0xc4, 3, "c", { 3, 3, 7, cond }, { 2 } },
95  { 0x80, 1, "add", { 3, 0, 7, reg } },
96  { 0x80|0x46, 2, "adi", { 1 } },
97  { 0x88, 1, "adc", { 3, 0, 7, reg } },
98  { 0x88|0x46, 2, "aci", { 1 } },
99  { 0x90, 1, "sub", { 3, 0, 7, reg } },
100  { 0x90|0x46, 2, "sui", { 1 } },
101  { 0x98, 1, "sbb", { 3, 0, 7, reg } },
102  { 0x98|0x46, 2, "sbi", { 1 } },
103  { 0xa0, 1, "ana", { 3, 0, 7, reg } },
104  { 0xa0|0x46, 2, "ani", { 1 } },
105  { 0xa8, 1, "xra", { 3, 0, 7, reg } },
106  { 0xa8|0x46, 2, "xri", { 1 } },
107  { 0xb0, 1, "ora", { 3, 0, 7, reg } },
108  { 0xb0|0x46, 2, "ori", { 1 } },
109  { 0xb8, 1, "cmp", { 3, 0, 7, reg } },
110  { 0xb8|0x46, 2, "cpi", { 1 } },
111  { 0x00, 1, "nop" },
112  { 0x00, 0 }
113 };
114 
115 static void arg(char* s, int const cmd, struct arg_t const* arg, int val) {
116  if (arg->type == 3) {
117  strcat(s, arg->fmt[(cmd >> arg->shift) & arg->mask]);
118  } else {
119  if (arg->type == 1)
120  sprintf(s, "%02X", val & 0xff);
121  else if (arg->type == 2)
122  sprintf(s, "%04X", val);
123  }
124 }
125 
126 static int i8080_disasm(unsigned char const* const code, char* text, int text_sz) {
127  int const cmd = code[0];
128  int const p = code[1] | (code[2] << 8);
129 
130  struct opcode_t const *op;
131  for (op = &opcodes[0]; op->size; ++op) {
132  int const grp = cmd &
133  ~((op->arg1.mask << op->arg1.shift) |
134  (op->arg2.mask << op->arg2.shift));
135  int const branch = (grp == 0xc0 || grp == 0xc2 || grp == 0xc4);
136  if (grp == op->cmd) {
137  strcpy(text, op->name);
138  if (!branch) strcat(text, " ");
139  arg(text + strlen(text), cmd, &op->arg1, p);
140  if (op->arg2.type != 0) strcat(text, (branch ? " " : ", "));
141  arg(text + strlen(text), cmd, &op->arg2, p);
142  return op->size;
143  }
144  }
145  snprintf(text, text_sz, "db @ 0x%02x", cmd);
146  return 1;
147 }
ut8 op
Definition: 6502dis.c:13
ut16 val
Definition: armass64_const.h:6
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags cmd
Definition: sflib.h:79
static char * rst[]
Definition: i8080dis.c:39
static void arg(char *s, int const cmd, struct arg_t const *arg, int val)
Definition: i8080dis.c:115
static char * cond[]
Definition: i8080dis.c:38
static char * reg[]
Definition: i8080dis.c:35
static char * push_rp[]
Definition: i8080dis.c:37
static char * rp[]
Definition: i8080dis.c:36
static struct opcode_t opcodes[]
static int i8080_disasm(unsigned char const *const code, char *text, int text_sz)
Definition: i8080dis.c:126
snprintf
Definition: kernel.h:364
sprintf
Definition: kernel.h:365
void * p
Definition: libc.cpp:67
#define const
Definition: ansidecl.h:240
static RzSocket * s
Definition: rtr.c:28
Definition: i8080dis.c:41
char ** fmt
Definition: i8080dis.c:45
int shift
Definition: i8080dis.c:43
int type
Definition: i8080dis.c:42
int mask
Definition: i8080dis.c:44
Definition: inftree9.h:24
struct arg_t arg1 arg2
Definition: i8080dis.c:52
char * name
Definition: i8080dis.c:51
int size
Definition: i8080dis.c:50
unsigned char cmd
Definition: i8080dis.c:49
Definition: dis.c:32