Skip to content

Commit c42cf80

Browse files
committed
Implement constants in AO lang
1 parent 81f6fa0 commit c42cf80

11 files changed

+299
-45
lines changed

docs/AO_language.txt

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
AO Programming Language
2-
= = = = = = = = = = = = =
2+
- - - - - - - - - - - - - - - - - - - - - - -
33

44
The AO (ASM Operands) language is a very low-level assembly language designed for TurnaCore machines.
55
It matches 1-to-1 to the equivalent compiled binary machine code. Actually, the only thing it makes is to
@@ -76,7 +76,31 @@ Later, when you want to jump to the label, you can simply do the following:
7676
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
7777
JMP this_is_a_label
7878
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
79+
80+
Sometimes, you would like to use certain constants in your program. These numbers can be defined in a way similar
81+
to the C language's #define. The syntax for defining constants is as follows:
7982

83+
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
84+
#pi=3
85+
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
86+
87+
The # denotes that this expression is a constant definition. 'pi' is the name of your constant and 3 is the value. Please note that
88+
spaces are NOT allowed.
89+
90+
After defining a constant, you can use it anywhere in your code by simply typing its name. For example, if you want to
91+
see whether pi is larger than the value in Register 2 and jump to a label if so, you can write the following:
92+
93+
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
94+
MR1 pi
95+
JG some_label_defined_somewhere_else
96+
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
97+
98+
Since we defined the value of pi as 3, this is esentially the same thing as writing:
99+
100+
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
101+
MR1 3
102+
JG some_label_defined_somewhere_else
103+
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
80104

81105
AO files are compiled into CIS (compiled instruction sequence) files. These are the files which the CPU reads.
82106

programs/conditionals.ao

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1+
; = = = = = = = = =
12
; CONDITIONALS.AO
3+
; = = = = = = = = =
24
; Author: arda-guler
3-
;
4-
; This is a simple counter that counts up to 200
5+
; = = = = = = = = =
6+
; This is a simple counter that counts up to the given value
57
; Uses the JL (jump if less than) instruction
8+
; = = = = = = = = =
9+
; INPUTS:
610

7-
MR1 200
11+
#TARGET=200
12+
; = = = = = = = = =
13+
; OUTPUTS: NONE
14+
; = = = = = = = = =
15+
16+
MR1 TARGET
817
MR1 NULL
918

1019
loop:
1120
MR2 1
1221
ADD
13-
MR2 200
22+
MR2 TARGET
1423
JL loop
1524
WRITE
1625
HALT

programs/deceleration_integrator.ao

+16-5
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,34 @@
99
; Assumes constant time step of 1. (So I don't need to keep
1010
; calling the multiplication function every loop.)
1111
; = = = = = = = = = = = = = = =
12+
; INPUTS:
13+
14+
#INIT_POS=3000
15+
16+
; this is actually velocity + 100
17+
; since there are no negative numbers
18+
; we will use 100 as our zero point.
19+
#INIT_VEL=150
20+
#DECEL_RATE=10
21+
; = = = = = = = = =
22+
; OUTPUTS:
23+
; Final position on display register, Register 5.
24+
; Also saves it on memory address 2.
25+
; = = = = = = = = =
1226

1327
JMP start
1428

1529
; - - - - - - - - - -
1630
; VARIABLES
1731
; - - - - - - - - - -
1832
; address 2 - position
19-
3000
33+
INIT_POS
2034

2135
load_vel_into_r2:
2236
MR2
2337

24-
; this is actually velocity + 100
25-
; since there are no negative numbers
26-
; we will use 100 as our zero point
2738
; address 4 - velocity
28-
150
39+
INIT_VEL
2940
JMP return_update_pos
3041

3142
; - - - - - - - - - -

programs/division.ao

+30-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
1+
; = = = = = = = = =
12
; DIVISION.AO
3+
; = = = = = = = = =
24
; Author: arda-guler
3-
;
4-
; Dividing 100 by 15, saving the quotient and the remainder on memory
5+
; = = = = = = = = =
6+
; Dividing the given dividend by the given divisor, saving the quotient and the remainder on memory
57
; Also displays quotient on display register (register 5)
8+
; = = = = = = = = =
9+
; INPUTS:
10+
11+
#DIVIDEND=100
12+
#DIVISOR=15
13+
; = = = = = = = = =
14+
; OUTPUTS:
15+
; Saves quotient and remainer on memory locations ADR_COUNTER and ADR_REMAINDER
16+
; Also displays quotient on display register, Register 5.
17+
18+
#ADR_COUNTER=2000
19+
#ADR_REMAINDER=2001
20+
; = = = = = = = = =
621

722
; Initialize the counter variable at address 2000
8-
MR1 1
9-
MR2 2000
23+
MR1 0
24+
MR2 ADR_COUNTER
1025
MEMW
1126

1227
; load the dividend and divisor
13-
MR1 100
14-
MR2 15
28+
MR1 DIVIDEND
29+
MR2 DIVISOR
1530

1631
loop:
1732
; subtract divisor
@@ -21,40 +36,39 @@ SUB
2136
WRITE
2237

2338
; increase counter
24-
MR2 2000
39+
MR2 ADR_COUNTER
2540
MEMR
2641
MR2 1
2742
ADD
28-
MR2 2000
43+
MR2 ADR_COUNTER
2944
MEMW
3045

3146
; get dividend into R1 again to continue
3247
RECALL
3348

3449
; if dividend is still larger than divisor, loop
35-
MR2 15
50+
MR2 DIVISOR
3651
JG loop
3752

38-
; if the int is divisible exactly, we need to increment the counter one more before halting (26)
53+
; if the int is divisible exactly, we need to increment the counter one more before halting
3954
JL save_result
4055
WRITE
41-
MR2 2000
56+
MR2 ADR_COUNTER
4257
MEMR
4358
MR2 1
4459
ADD
45-
MR2 2000
60+
MR2 ADR_COUNTER
4661
MEMW
4762
RECALL
48-
MR2 15
63+
MR2 DIVISOR
4964
SUB
5065

5166
save_result:
52-
; save remainder at address 2001
53-
MR2 2001
67+
MR2 ADR_REMAINDER
5468
MEMW
5569

5670
; display result
57-
MR2 2000
71+
MR2 ADR_COUNTER
5872
MEMR
5973
WRITE
6074
HALT

programs/infinite_loop.ao

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
; = = = = = = = = =
12
; INFINITE_LOOP.AO
3+
; = = = = = = = = =
24
; Author: arda-guler
3-
;
5+
; = = = = = = = = =
46
; This is an infinite loop example where
57
; we just keep incrementing the same
68
; value.
9+
; = = = = = = = = =
10+
; INPUTS: NONE
11+
; = = = = = = = = =
12+
; OUTPUTS: NONE
13+
; = = = = = = = = =
714

815
MR1 NULL
916
MR2 1

programs/multiplication.ao

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
1+
; = = = = = = = = =
12
; MULTIPLICATION.AO
3+
; = = = = = = = = =
24
; Author: arda-guler
3-
;
4-
; Multiplying 13 and 27.
5+
; = = = = = = = = =
6+
; Multiplying two given numbers.
7+
; = = = = = = = = =
8+
; INPUTS:
9+
10+
#NUM_1=13
11+
#NUM_2=27
12+
; = = = = = = = = =
13+
; OUTPUT:
14+
15+
; The result will be given in the display register, Register 5.
16+
; = = = = = = = = =
17+
18+
#ADR_VAR=2000
519

620
; Initialize a loop counter using register 5
7-
MR1 14
21+
MR1 NUM_1
22+
MR2 1
23+
ADD
824
WRITE
925

10-
; Initialize a variable at memory location 2000
26+
; Initialize a variable
1127
MR1 NULL
12-
MR2 2000
28+
MR2 ADR_VAR
1329
MEMW
1430

1531
loop:
1632
; read value from memory
17-
MR2 2000
33+
MR2 ADR_VAR
1834
MEMR
19-
; add 27
20-
MR2 27
35+
; add NUM_2
36+
MR2 NUM_2
2137
ADD
2238
; save updated value to memory
23-
MR2 2000
39+
MR2 ADR_VAR
2440
MEMW
2541
; decrease counter
2642
RECALL
@@ -31,7 +47,7 @@ MR2 1
3147
JG loop
3248

3349
; write result to register 5
34-
MR2 2000
50+
MR2 ADR_VAR
3551
MEMR
3652
WRITE
3753

programs/simple_add.ao

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
; = = = = = = = = =
12
; SIMPLE_ADD.AO
3+
; = = = = = = = = =
24
; Author: arda-guler
3-
;
5+
; = = = = = = = = =
46
; A simple adding operation.
7+
; = = = = = = = = =
8+
; INPUTS:
59

6-
MR1 15
7-
MR2 22
10+
#NUM_1=15
11+
#NUM_2=22
12+
; = = = = = = = = =
13+
; OUTPUTS:
14+
; The result will be displayed in display register, Register 5.
15+
; = = = = = = = = =
16+
17+
MR1 NUM_1
18+
MR2 NUM_2
819
ADD
920
WRITE
1021
HALT

0 commit comments

Comments
 (0)