From 9eab2215c7bb8dbe017987078497fa6c7d8943c3 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Mon, 13 May 2024 14:52:19 -0400 Subject: Handle Labels on Seperate Lines --- qdma | Bin 2356292 -> 2355595 bytes qdma.go | 40 +++++++++++++++++++++++----------------- tests/test5.asm | 21 +++++++++++++++++++++ tests/test5.bin | Bin 0 -> 56 bytes 4 files changed, 44 insertions(+), 17 deletions(-) create mode 100755 tests/test5.asm create mode 100644 tests/test5.bin diff --git a/qdma b/qdma index c89f7ae..454c066 100755 Binary files a/qdma and b/qdma differ diff --git a/qdma.go b/qdma.go index 6ff9e81..8aed06a 100755 --- a/qdma.go +++ b/qdma.go @@ -108,15 +108,21 @@ func LabelFind(path string) { continue } - if strings.Contains(s[0], ":") { + hasLabel, err := regexp.MatchString("^.*:", s[0]) + if err != nil { + panic(err) + } + if hasLabel { labels[strings.ReplaceAll(s[0], ":", "")] = uint(i) + if len(s) == 1 { + i -= 4 + } } } } func Encode(inst []string, pc int32) (uint32, error) { var ret uint32 = 0 - offset := 0 for _, s := range inst { switch s { case "syscall": @@ -126,34 +132,29 @@ func Encode(inst []string, pc int32) (uint32, error) { } } - _, isLabel := labels[strings.ReplaceAll(inst[0], ":", "")] - if isLabel { - offset += 1 - } - - function := Instructions[inst[offset]] + function := Instructions[inst[0]] if function.isRtype && function.opcode == 8 { - ret = (RegNums[inst[1+offset]] << 21) | function.opcode + ret = (RegNums[inst[1]] << 21) | function.opcode } else if function.isRtype { - ret = (RegNums[inst[2+offset]] << 21) | (RegNums[inst[3+offset]] << - 16) | (RegNums[inst[1+offset]] << 11) | function.opcode + ret = (RegNums[inst[2]] << 21) | (RegNums[inst[3]] << + 16) | (RegNums[inst[1]] << 11) | function.opcode } else if function.opcode == 2 || function.opcode == 3 { - label, _ := labels[inst[1+offset]] + label, _ := labels[inst[1]] ret = (function.opcode << 26) | uint32(label&0x03FFFFFF) } else { var imm int16 - addr, isLabel := labels[inst[3+offset]] + addr, isLabel := labels[inst[3]] if isLabel { imm = int16(int32(addr) - pc - 12) } else { - i, err := strconv.Atoi(inst[3+offset]) + i, err := strconv.Atoi(inst[3]) if err != nil { return 0, err } imm = int16(i) } - ret = (function.opcode << 26) | (RegNums[inst[2+offset]] << 21) | - (RegNums[inst[1+offset]] << 16) | (0x0000FFFF & uint32(imm)) + ret = (function.opcode << 26) | (RegNums[inst[2]] << 21) | + (RegNums[inst[1]] << 16) | (0x0000FFFF & uint32(imm)) } return ret, nil } @@ -185,7 +186,12 @@ func Assemble(path string) { panic(err) } - if len(inst) == 1 && inst[0] == "" { + _, isLabel := labels[strings.ReplaceAll(inst[0], ":", "")] + if isLabel { + inst = inst[1:] + } + + if len(inst) == 0 || (len(inst) == 1 && inst[0] == "") { i -= 4 continue } diff --git a/tests/test5.asm b/tests/test5.asm new file mode 100755 index 0000000..65c20bb --- /dev/null +++ b/tests/test5.asm @@ -0,0 +1,21 @@ +# This is a test program to test the capabilities of the assembler + +addi $t0, $zero, 10 # load 10 into $t0 +addi $t1, $zero, 12 # load 12 into $t1 +add $t2, $t1, $t0 +jal PrintInt +nop +j Exit # Exit the program +nop + +PrintInt: + add $a0, $t2, $zero + addi $v0, $zero, 1 + syscall + jr $ra + nop + +Exit: + + addi $v0, $zero, 10 + syscall diff --git a/tests/test5.bin b/tests/test5.bin new file mode 100644 index 0000000..61cb805 Binary files /dev/null and b/tests/test5.bin differ -- cgit v1.2.3