Improve emulator

This commit is contained in:
Nero 2019-09-28 20:38:35 +00:00
parent fcb4db25ad
commit 2935d7ee6c
2 changed files with 33 additions and 4 deletions

View File

@ -7,11 +7,11 @@ main:
test al, al
jz .ret
mov dl, al
mov ah, 0x02
int 0x21
mov cl, 0x02
call 5
jmp .loop
.ret:
ret
hello:
db "Hello!", 0
db "Hello!", 0x0A, 0x0D, 0

View File

@ -27,6 +27,7 @@ void* rmptr;
#define AL regset.r8[0]
#define CL regset.r8[2]
#define DL regset.r8[4]
#define AX regset.r16[0]
#define BX regset.r16[3]
#define SP regset.r16[4]
#define BP regset.r16[5]
@ -169,6 +170,13 @@ void step() {
modrm(BIT8);
RM8 = R8(reg);
break;
case 0x8B: // MOV reg16,r/m16
modrm(BIT16);
R16(reg) = RM16;
break;
case 0xA1: // MOV AX,memoffs16
AX = *(uint16_t*)&mem[imm16()];
break;
case 0xAC: // LODSB
AL = mem[SI];
SI++;
@ -196,7 +204,7 @@ void step() {
handle_syscall(CL);
} else {
push(ip);
ip += reg;
ip = reg;
}
break;
case 0xEB: // JMP rb
@ -214,6 +222,21 @@ void step() {
}
}
void copy_cmdline(char* str) {
int i, c;
uint8_t *len = &mem[0x80];
char* ptr = (char*)&mem[0x81];
c = strlen(str);
// Clip at max length
if (c>0x7E) {
fprintf(stderr, "Command line too long, max is 126 bytes\n");
exit(1);
}
memcpy(ptr, str, c);
ptr[c]=0x0D;
*len=c;
}
int main(int argc, char** argv) {
memset(&mem, sizeof(mem), 0);
memset(&regset, sizeof(regset), 0);
@ -221,6 +244,12 @@ int main(int argc, char** argv) {
mem[1]=0x20;
push(0);
if (argc>2) {
copy_cmdline(argv[2]);
} else {
copy_cmdline("");
}
FILE* fd = fopen(argv[1], "r");
fread(mem + ip, 1, sizeof(mem) - ip, fd);
while(1) {