diff options
| author | Jacob McDonnell <jacob@jacobmcdonnell.com> | 2024-05-13 14:52:19 -0400 |
|---|---|---|
| committer | Jacob McDonnell <jacob@jacobmcdonnell.com> | 2024-05-13 14:52:19 -0400 |
| commit | 9eab2215c7bb8dbe017987078497fa6c7d8943c3 (patch) | |
| tree | f7dd90797d5566f148562ed9ce4fc7e1be56f7d5 | |
| parent | c41d9302ae9d62a2a18afc26758085d9633ab314 (diff) | |
Handle Labels on Seperate Lines
| -rwxr-xr-x | qdma | bin | 2356292 -> 2355595 bytes | |||
| -rwxr-xr-x | qdma.go | 40 | ||||
| -rwxr-xr-x | tests/test5.asm | 21 | ||||
| -rw-r--r-- | tests/test5.bin | bin | 0 -> 56 bytes |
4 files changed, 44 insertions, 17 deletions
| Binary files differ @@ -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 Binary files differnew file mode 100644 index 0000000..61cb805 --- /dev/null +++ b/tests/test5.bin |
