Encode jump offsets, collect instruction lines as strings, print at end

This commit is contained in:
Joseph Ferano 2025-02-24 20:03:18 +07:00
parent 1f9837b256
commit 6365bd73a0

View File

@ -106,6 +106,8 @@ InstructionInfo :: struct {
is_jump: bool,
}
// TODO: Maybe we can get rid of it since I don't have to specify the shift_offset,
// not like it changes a lot
reg_first_last := RegInfo{ in_first_byte = true, shift_offset = 0 }
reg_second_middle := RegInfo{ in_first_byte = false, shift_offset = 3 }
@ -345,6 +347,7 @@ main :: proc() {
// f,err := os.open(len(os.args) > 1 ? os.args[1] : "./asm_files/01-02-39.bin")
f,err := os.open(os.args[1])
if err != os.ERROR_NONE {
fmt.eprintln("ERROR:", err)
os.exit(1)
}
defer os.close(f)
@ -368,9 +371,11 @@ main :: proc() {
// fmt.printfln("%d", asdf2)
read_next := false
src_dst := true
fmt.println("bits 16\n")
idx := 0
added_label := false
line_count := 0
instruction_builder := strings.builder_make()
instruction_list := make([dynamic]string, 128)
for idx < bytes_read {
processed := 1
curr_byte := data[idx]
@ -378,7 +383,9 @@ main :: proc() {
instruction, ok := try_find_instruction(curr_byte)
if !ok {
txt := "unknown instruction"
fmt.printfln("%s %*[1]s %8b", txt, RIGHT_ALIGN_AMOUNT - len(txt), ";;", curr_byte)
line := fmt.aprintf("%s %*[1]s %8b", txt, RIGHT_ALIGN_AMOUNT - len(txt), ";;", curr_byte)
instruction_list[line_count] = line
line_count += 1
idx += 1
continue
}
@ -409,6 +416,7 @@ main :: proc() {
reg = (b >> reg_info.shift_offset) & 0b111
}
data_idx := idx + 1
if instruction.has_address {
@ -474,11 +482,9 @@ main :: proc() {
opname = strings.to_lower(fmt.aprintf("%s", instruction.opname))
}
if instruction.is_jump {
if !added_label {
fmt.println("\nlabel:")
added_label = true
}
full_inst = fmt.aprintf("%s %s", strings.to_lower(opname), "label")
// NOTE: In order to mimic the label offset, you have to take the value you got and add two
value := (i8)(data[idx+1]) + 2
full_inst = fmt.aprintf("%s $%s%d ; %d", strings.to_lower(opname), value >= 0 ? "+" : "", value, value - 2)
processed += 1
} else {
opname = strings.to_lower(opname)
@ -488,11 +494,17 @@ main :: proc() {
full_inst = fmt.aprintf("%s %s%s, %s", opname, size_string, lhs, rhs)
}
}
fmt.printf("%s %*[1]s", full_inst, RIGHT_ALIGN_AMOUNT - len(full_inst), ";;")
fmt.sbprintf(&instruction_builder, "%s %*[1]s", full_inst, RIGHT_ALIGN_AMOUNT - len(full_inst), ";;")
for i in 0..<processed {
fmt.printf(" %08b", data[idx + i])
fmt.sbprintf(&instruction_builder, " %08b", data[idx + i])
}
fmt.println()
instruction_list[line_count] = strings.clone(strings.to_string(instruction_builder))
idx += processed
line_count += 1
strings.builder_reset(&instruction_builder)
}
fmt.println("bits 16\n")
for i in 0..<line_count {
fmt.println(instruction_list[i])
}
}