Skip to content

Commit 24c3c99

Browse files
committed
Added correct relational mapping of jumps
1 parent 5e7deb7 commit 24c3c99

File tree

5 files changed

+52
-23
lines changed

5 files changed

+52
-23
lines changed

best_example.rasm

+2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ start:
22
.lda #a
33
.lda #idx
44
.call add
5+
.jmp -1
56
.stp
67

78

89
add:
910
.add #10, #12
1011
.sta d2 ; d1 is used by call and ret and d0 by some variations of add, div, ... . Also keep in mind that using call again will overwrite the return address.
12+
.jze -2
1113
.ret
1214

1315
100 a

out_best_example.asm

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
0 lda #97
22
1 lda #1
33
2 lda #2
4-
3 sta 16
4+
3 sta 18
55
4 lda #6
6-
5 add 16
7-
6 sta 16
8-
7 jmp 9
9-
8 stp
10-
9 lda #12
11-
10 sta 15
12-
11 lda #10
13-
12 add 15
14-
13 sta 17
15-
14 jmp (16)
16-
15 0
17-
16 0
6+
5 add 18
7+
6 sta 18
8+
7 jmp 10
9+
8 jmp 2
10+
9 stp
11+
10 lda #12
12+
11 sta 17
13+
12 lda #10
14+
13 add 17
15+
14 sta 19
16+
15 jze 10
17+
16 jmp (18)
1818
17 0
19+
18 0
20+
19 0
1921
100 97

out_example.asm

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
7 sta 1
99
8 sta 1
1010
9 sta 1
11-
10 jmp 7
11+
10 jmp 6
1212
11 sta 11
1313
12 jze 0
1414
13 lda 1

out_example_big.asm

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
7 sta 1
99
8 sta 1
1010
9 sta 1
11-
10 jmp 7
11+
10 jmp 6
1212
11 sta 11
1313
12 sta 12
1414
13 jze 0
@@ -20,12 +20,12 @@
2020
19 sta 2
2121
20 lda 5
2222
21 lda 20
23-
22 sta 38
24-
23 jmp 22
25-
24 lda #1
26-
25 div 20
27-
26 lda #2
28-
27 sta 10
23+
22 sta 37
24+
23 jmp 21
25+
24 div #1,
26+
25 lda #2
27+
26 sta 10
28+
27 0
2929
28 0
3030
29 0
3131
30 0
@@ -36,5 +36,4 @@
3636
35 0
3737
36 0
3838
37 0
39-
38 0
4039
100 10

regcomp.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,32 @@ def main(input: str, output: str) -> None:
293293
data_cell_lookup_table: dict[str, int] = {} # We currently allocate them all in one chunk
294294
for label, rel_labels in labels_in_order:
295295
new_address = get_gap_of_size(len(rel_labels) + 1, reserved, max_address)
296+
297+
for i, rlabel in enumerate(rel_labels.copy()): # Do not modify a list you're iterating over
298+
if isinstance(rlabel, list):
299+
continue
300+
inst, data, *_ = rlabel.split(" ", maxsplit=1) + [""]
301+
if inst in ("jmp", "jnz", "jze", "jle") and data.lstrip('+-').isnumeric():
302+
reljump = int(data)
303+
position = i
304+
last_position = position + reljump
305+
while position != last_position:
306+
if reljump > 0:
307+
position += 1
308+
if position >= len(rel_labels):
309+
raise IndexError("Traversal went out of bounds")
310+
if isinstance(rel_labels[position], list):
311+
reljump -= 1
312+
reljump += len(rel_labels[position])
313+
elif reljump < 0:
314+
position -= 1
315+
if position < 0:
316+
raise IndexError("Traversal went out of bounds")
317+
if isinstance(rel_labels[position], list):
318+
reljump += 1
319+
reljump -= len(rel_labels[position])
320+
rel_labels[i] = f"{inst} {reljump}"
321+
296322
rel_labels = unnest_iterable(rel_labels) # Means flattening the list
297323

298324
if new_address == -1:
@@ -336,7 +362,7 @@ def main(input: str, output: str) -> None:
336362
dat = replace_placeholders_with_whitespace_condition(dat, all_placeholders, lookup_table)
337363
if inst in ("jmp", "jnz", "jze", "jle"):
338364
if dat.startswith("-") or dat.startswith("+"):
339-
cool_reserved[idx] = f"{i} {inst} {i + int(dat) + 1}\n"
365+
cool_reserved[idx] = f"{i} {inst} {i + int(dat)}\n"
340366
i += 1
341367
continue
342368
cool_reserved[idx] = f"{i} {inst} {dat}\n"

0 commit comments

Comments
 (0)