-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxtize.c
More file actions
205 lines (183 loc) · 4.88 KB
/
xtize.c
File metadata and controls
205 lines (183 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <string.h>
#include <dir.h>
#include <dos.h>
typedef unsigned char byte;
struct ExecParameters {
unsigned env_segment;
char far* cmdline;
struct fcb far* arg1;
struct fcb far* arg2;
byte far* entry_stack;
void (far* __cdecl entry_point)(int dummy);
unsigned psp_segment;
};
void setpsp(unsigned newpsp)
{
union REGS r;
r.h.ah = 0x50;
r.x.bx = newpsp;
intdos(&r, &r);
}
int LoadForDebugging(const char *program, struct ExecParameters *state)
{
union REGS r;
struct SREGS sr;
r.x.ax = 0x4B01;
sr.es = FP_SEG((void far*)state);
r.x.bx = FP_OFF((void far*)state);
sr.ds = FP_SEG((void far*)program);
r.x.dx = FP_OFF((void far*)program);
intdosx(&r, &r, &sr);
if (r.x.cflag)
return -1;
state->psp_segment = getpsp();
setpsp(_psp);
return 0;
}
void far EnterSingleStep(struct ExecParameters far *state);
void interrupt CountingSS();
long far GetCount(void);
void interrupt EmulatingSS();
void do_assert_eq(const char* x, unsigned v1, unsigned v2)
{
if (v1 != v2)
printf("FAIL: %s - %04X != %04X\n", x, v1, v2);
}
#define ASSERT_EQ(word1, word2) do_assert_eq(#word1 ## " == " ## #word2, word1, word2)
int main(int argc, char** argv)
{
static int i = 0x1234;
struct ExecParameters p;
char *target;
p.env_segment = 0;
p.cmdline = MK_FP(_psp, 0x81);
p.arg1 = 0;
p.arg2 = 0;
if (argc == 1)
{
fputs("Missing program name\n", stderr);
return 2;
}
if (stricmp(argv[1], "/T") == 0)
{
unsigned old_sp = _SP;
unsigned new_sp;
unsigned new_bp;
unsigned leave_sp;
unsigned old_bp = _BP;
unsigned leave_bp;
unsigned shl_80_3;
unsigned char rol_F7_2;
unsigned mul_3_4, mul_6_8, mul_2_5, mul_7_11;
unsigned tempds, tempes;
puts("Test mode.");
setvect(0xA1, EmulatingSS);
asm {
int 0xA1
//enter 10, 0
db 0xC8, 0x0A, 0x00, 0x00
mov cx, sp
mov dx, bp
int 0xA1
// push 0
db 0x6A, 0x00
int 0xA1
// push 0x80
db 0x68, 0x80, 0x00
int 0xA1
// shl [WORD PTR bp-14], 3
db 0xC1, 0x66, 0xF2, 0x03
mov bx, WORD PTR [bp-14]
//leave
int 0xA1
db 0xC9
mov [new_sp], cx
mov [new_bp], dx
mov [shl_80_3], bx
mov al, 0xF7
int 0xA1
//rol al, 2
db 0xC0, 0xC0, 2
mov [rol_F7_2], al
int 0xA1
//ror [i], 3
db 0xC1, 0x0E
dw offset i
db 3
mov ax, 3
int 0xA1
//imul bx, ax, 4
db 0x6B, 0xD8, 0x04
mov [mul_3_4], bx
mov bx, 6
int 0xA1
// imul ax, bx, 8
db 0x6B, 0xC3, 0x08
mov [mul_6_8], ax
mov cx, 2
int 0xA1
// imul bx, cx, 5
db 0x6B, 0xD9, 0x05
mov [mul_2_5], bx
mov ax, 7
int 0xA1
//imul ax, ax, 11
db 0x6B, 0xC0, 0x0B
mov [mul_7_11], ax
mov ax, es
int 0xA1
mov es, ax
mov ax, ss
int 0xA1
mov ss, ax
push ds
push cs
int 0xA1
pop ds
mov [tempds], ds
pop ds
push cs
int 0xA1
pop es
mov [tempes], es
}
leave_bp = _BP;
leave_sp = _SP;
ASSERT_EQ(old_sp, leave_sp);
ASSERT_EQ(old_sp - 2, new_bp);
ASSERT_EQ(old_sp - 12, new_sp);
ASSERT_EQ(old_bp, leave_bp);
ASSERT_EQ(0x80 << 3, shl_80_3);
ASSERT_EQ(0xDF, rol_F7_2);
ASSERT_EQ((0x1234 >> 3) | 0x8000, i);
ASSERT_EQ(3*4, mul_3_4);
ASSERT_EQ(6*8, mul_6_8);
ASSERT_EQ(2*5, mul_2_5);
ASSERT_EQ(7*11, mul_7_11);
ASSERT_EQ(_CS, tempds);
ASSERT_EQ(_CS, tempes);
return 0;
}
target = searchpath(argv[1]);
if (target == NULL)
{
fprintf(stderr, "%s not found\n", argv[1]);
return 1;
}
while(*p.cmdline == ' ')
p.cmdline++;
while(*p.cmdline > ' ')
p.cmdline++;
p.cmdline--;
*p.cmdline = *(unsigned char*)(MK_FP(_psp, 0x80)) - (FP_OFF(p.cmdline) - 0x80);
if (LoadForDebugging(target, &p) < 0)
{
fputs("Error\n", stderr);
return 1;
}
setvect(1, EmulatingSS);
EnterSingleStep(&p);
//printf("\n%ld steps observed", GetCount());
return 0;
}