add: MUL, DIV instructions

This commit is contained in:
xamidev
2024-10-23 20:21:02 +02:00
parent ff5e9c07d7
commit fd0d05003e
5 changed files with 84 additions and 5 deletions

View File

@@ -106,6 +106,24 @@ void assemble(char* filename)
buffer[i++] = reg1_n; buffer[i++] = reg1_n;
buffer[i++] = reg2_n; buffer[i++] = reg2_n;
} }
else if (strncmp(line, "MUL", 3) == 0)
{
sscanf(line, "%s %[^,], %s", instruction, reg1, reg2);
int reg1_n = reg1[1] - '0';
int reg2_n = reg2[1] - '0';
buffer[i++] = MUL;
buffer[i++] = reg1_n;
buffer[i++] = reg2_n;
}
else if (strncmp(line, "DIV", 3) == 0)
{
sscanf(line, "%s %[^,], %s", instruction, reg1, reg2);
int reg1_n = reg1[1] - '0';
int reg2_n = reg2[1] - '0';
buffer[i++] = DIV;
buffer[i++] = reg1_n;
buffer[i++] = reg2_n;
}
else if (strncmp(line, "OR", 3) == 0) else if (strncmp(line, "OR", 3) == 0)
{ {
sscanf(line, "%s %[^,], %s", instruction, reg1, reg2); sscanf(line, "%s %[^,], %s", instruction, reg1, reg2);

12
cpu.c
View File

@@ -74,6 +74,16 @@ void cpu_exec(uint8_t opcode)
reg2 = cpu.memory[cpu.pc++]; reg2 = cpu.memory[cpu.pc++];
cpu.reg[reg1] -= cpu.reg[reg2]; cpu.reg[reg1] -= cpu.reg[reg2];
break; break;
case MUL:
reg1 = cpu.memory[cpu.pc++];
reg2 = cpu.memory[cpu.pc++];
cpu.reg[reg1] *= cpu.reg[reg2];
break;
case DIV:
reg1 = cpu.memory[cpu.pc++];
reg2 = cpu.memory[cpu.pc++];
cpu.reg[reg1] /= cpu.reg[reg2];
break;
case OR: case OR:
reg1 = cpu.memory[cpu.pc++]; reg1 = cpu.memory[cpu.pc++];
reg2 = cpu.memory[cpu.pc++]; reg2 = cpu.memory[cpu.pc++];
@@ -218,6 +228,8 @@ void mem_dump()
case 0xb0: case 0xb0:
case 0xb1: case 0xb1:
case 0xb2:
case 0xb3:
printf("\e[43m%02x\e[0m ", cpu.memory[i]); printf("\e[43m%02x\e[0m ", cpu.memory[i]);
break; break;

2
cpu.h
View File

@@ -23,6 +23,8 @@ typedef enum
// 0xB? -> Arithmetic operations // 0xB? -> Arithmetic operations
ADD = 0xB0, ADD = 0xB0,
SUB = 0xB1, SUB = 0xB1,
MUL = 0xB2,
DIV = 0xB3,
// 0xC? -> Bitwise operations // 0xC? -> Bitwise operations
OR = 0xC0, OR = 0xC0,

48
examples/hello.asm Normal file
View File

@@ -0,0 +1,48 @@
; Hello, world!
PUT R1, 72
PUT R2, 101
PUT R3, 108
OUT R1
OUT R2
OUT R3
OUT R3
XOR R1, R1
XOR R2, R2
XOR R3, R3
PUT R1, 111
PUT R2, 44
PUT R3, 32
OUT R1
OUT R2
OUT R3
XOR R1, R1
XOR R2, R2
XOR R3, R3
PUT R1, 119
PUT R2, 111
PUT R3, 114
OUT R1
OUT R2
OUT R3
XOR R1, R1
XOR R2, R2
XOR R3, R3
PUT R1, 108
PUT R2, 100
PUT R3, 33
OUT R1
OUT R2
OUT R3
HLT

View File

@@ -1,7 +1,6 @@
;this is a comment ;this is a comment
PUT R2, 5 PUT R2, 5
MOV R1, R2 PUT R3, 2
DIV R2, R3
ADD R1, R2
HLT HLT