Add .LOOP instruction, dump memory to disk with cli arg

This commit is contained in:
Joseph Ferano 2025-03-23 08:58:29 +07:00
parent fafb440891
commit 85a8172923
2 changed files with 17 additions and 0 deletions

View File

@ -111,6 +111,9 @@ execute_instruction :: proc(cpu: ^Cpu, inst: Instruction) {
case .JP, .JPE: jump = cpu.flags[.PF]
case .JB, .JNAE: jump = cpu.flags[.CF]
case .LOOP:
cpu.registers[.cx].full -= 1
jump = cpu.registers[.cx].full != 0
case .LOOPNZ, .LOOPNE:
cpu.registers[.cx].full -= 1
// According to this resource, the loopnz inst does not change any flags

View File

@ -95,4 +95,18 @@ main :: proc() {
if what_to_print == "instructions" || what_to_print == "all" {
print_instructions_stdout(instructions_list[:])
}
if len(os.args) > 3 && os.args[3] == "dump" {
f2,open_err := os.open("./listing-54.data", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if open_err == nil {
bytes_written,write_err := os.write(f2, cpu.memory[:])
if write_err == nil {
fmt.println("Dumpped", bytes_written, "to disk: listing-54.data")
} else {
fmt.println("Error2:", write_err)
}
} else {
fmt.println("Error1:", open_err)
}
defer os.close(f)
}
}