Skip to content

Commit e3b2209

Browse files
committed
Implement labels
1 parent 9b83016 commit e3b2209

9 files changed

+114
-12
lines changed
File renamed without changes.

programs/conditionals.ao

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
MR1 200
88
MR1 NULL
9+
10+
loop:
911
MR2 1
1012
ADD
1113
MR2 200
12-
JL 5
14+
JL loop
1315
WRITE
1416
HALT

programs/deceleration_integrator.ao

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
; = = = = = = = = = = = = = = =
2+
; DECELERATION_INTEGRATOR.AO
3+
; = = = = = = = = = = = = = = =
4+
; Author: arda-guler
5+
; = = = = = = = = = = = = = = =
6+
; A symplectic Euler integrator to compute the final position
7+
; of a decelerating object with given initlai position, speed
8+
; and a constant rate of deceleration.
9+
; Assumes constant time step of 1. (So I don't need to keep
10+
; calling the multiplication function every loop.)
11+
; = = = = = = = = = = = = = = =
12+
13+
JMP start
14+
15+
; - - - - - - - - - -
16+
; VARIABLES
17+
; - - - - - - - - - -
18+
; address 2 - position
19+
3000
20+
21+
load_vel_into_r2:
22+
MR2
23+
24+
; this is actually velocity + 100
25+
; since there are no negative numbers
26+
; we will use 100 as our zero point
27+
; address 4 - velocity
28+
150
29+
JMP return_update_pos
30+
31+
; - - - - - - - - - -
32+
; PROGRAM
33+
; - - - - - - - - - -
34+
35+
start:
36+
; - - - update velocity
37+
; read velocity from memory
38+
MR2 4
39+
MEMR
40+
41+
; now, load decel rate into R2
42+
; which is 10
43+
MR2 10
44+
45+
; update the value of velocity
46+
SUB
47+
48+
; save it back onto memory
49+
MR2 4
50+
MEMW
51+
52+
; check if we are still moving in positive direction
53+
MR2 100
54+
JL end_program
55+
56+
; - - - update position
57+
; load position into R1
58+
MR2 2
59+
MEMR
60+
61+
; load velocity onto R2
62+
JMP load_vel_into_r2
63+
64+
return_update_pos:
65+
; update the value of position
66+
ADD
67+
MR2 100
68+
SUB
69+
70+
; save it back onto memory
71+
MR2 2
72+
MEMW
73+
JMP start
74+
75+
end_program:
76+
; write the end position onto display register R5
77+
MR2 2
78+
MEMR
79+
WRITE
80+
81+
HALT

programs/division.ao

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
; Also displays quotient on display register (register 5)
66

77
; Initialize the counter variable at address 2000
8-
MR2 2000
98
MR1 1
9+
MR2 2000
1010
MEMW
1111

1212
; load the dividend and divisor
1313
MR1 100
1414
MR2 15
1515

16-
; - - - LOOP - - -
16+
loop:
1717
; subtract divisor
1818
SUB
1919

@@ -33,11 +33,10 @@ RECALL
3333

3434
; if dividend is still larger than divisor, loop
3535
MR2 15
36-
JG 8
36+
JG loop
3737

38-
; - - - END LOOP - - -
3938
; if the int is divisible exactly, we need to increment the counter one more before halting (26)
40-
JL 42
39+
JL save_result
4140
WRITE
4241
MR2 2000
4342
MEMR
@@ -49,6 +48,7 @@ RECALL
4948
MR2 15
5049
SUB
5150

51+
save_result:
5252
; save remainder at address 2001
5353
MR2 2001
5454
MEMW

programs/infinite_loop.ao

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
MR1 NULL
99
MR2 1
10+
11+
loop:
1012
ADD
1113
WRITE
12-
JMP 5
14+
JMP loop

programs/multiplication.ao

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ MR1 NULL
1212
MR2 2000
1313
MEMW
1414

15-
; - - - LOOP - - -
15+
loop:
1616
; read value from memory
1717
MR2 2000
1818
MEMR
@@ -28,7 +28,7 @@ MR2 1
2828
SUB
2929
WRITE
3030
MR2 1
31-
JG 9
31+
JG loop
3232

3333
; write result to register 5
3434
MR2 2000

src/compiler.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,22 @@ def compile_program(input_file, output_file="output.cis"):
1111
inlines_sanitized.extend(split_elements)
1212
else:
1313
inlines_sanitized.append(element)
14+
15+
labels = {}
16+
statement_pos = 1
17+
for line in inlines_sanitized:
18+
if line.endswith(":"):
19+
labels[line[0:-1]] = str(statement_pos)
20+
else:
21+
statement_pos += 1
22+
23+
print(labels)
1424

1525
outlines = []
1626
for line in inlines_sanitized:
17-
if line.lower() == "add":
27+
if line.lower() in labels:
28+
outlines.append(labels[line.lower()])
29+
elif line.lower() == "add":
1830
outlines.append(str(int(cpu_add)))
1931
elif line.lower() == "sub":
2032
outlines.append(str(int(cpu_sub)))
@@ -42,6 +54,8 @@ def compile_program(input_file, output_file="output.cis"):
4254
outlines.append(str(int(cpu_halt)))
4355
elif line.lower() == "null":
4456
outlines.append("0")
57+
elif line.endswith(":"):
58+
pass
4559
else:
4660
outlines.append(line)
4761

src/main.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def main():
173173
canvas.create_text(20, 30 + i * 30, text="Register" + str(i+1) + ": " + str(cpu.registers[i].get_value()) + " (" + str(int(cpu.registers[i].get_value())) + ")", anchor="w", font=('Arial', 12))
174174

175175
window.update()
176-
time.sleep(0.1)
176+
time.sleep(0.2)
177177

178178
if int(cpu.registers[3].get_value()) < mem.size and cpu.run:
179179
canvas.delete("all")
@@ -182,4 +182,7 @@ def main():
182182
print(cpu)
183183
print(mem)
184184

185+
if slow:
186+
input("Press Enter to exit.")
187+
185188
main()

src/memory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def write_to_memory(self, address, value):
4040
self.data[address] = value
4141
return
4242

43-
if address > len(self.data) - 1:
43+
else:
4444
padding = address - len(self.data) + 1
4545
for i in range(padding):
4646
self.data.append(NULL)

0 commit comments

Comments
 (0)