-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtinyDoc.txt
146 lines (124 loc) · 5.35 KB
/
tinyDoc.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
Tiny is a very simple assembly code interpreter.
The executable runs on Sun architectures.
---------------------------------------
an example Tiny program (a longer program is attached at the end):
var i
str prompt "enter a number: "
str announce \nthe square is"
label myloop ; main loop
sys writes prompt
sys readi i
move i r3
muli i r3
; some more comment
sys writes announce
sys writei r3 ;
cmpi 1 r3
jne myloop
sys halt ; optional if at end
end
---------------------------------------
Tiny simulates an architecture that has 4 data registers, a stack pointer (sp),
a frame pointer (fp) and both integer an floating point arithmetic. All data
elements have size 1. The data representation is unknown to the user.
Tiny accepts the following assembly codes:
var id ; reserves and names a memory cell. first letter alphanum
then alphanum with punctuations, case sensitive
; both integer and real (float) have the size of one memory cell
str sid "a string constant" ; the only operation on string constants is sys writes sid
strings can include \n for end-of-line
; var and str declarations must preceed all code and labels (during debugging,
; enforcement of this rule can be disabled. See the "mix" command line option)
label target ; a jump target
move opmrl opmr ; only one operand can be a memory id or stack variable
addi opmrl reg ; integer addition, reg = reg + op1
addr opmrl reg ; real (i.e. floatingpoint) addition
subi opmrl reg ; computes reg = reg - op1
subr opmrl reg
muli opmrl reg ; computes reg = reg * op1
mulr opmrl reg
divi opmrl reg ; computes reg = reg / op1
divr opmrl reg
inci reg ; increment the (integer) register value by 1
deci reg ; decrement the (integer) register value by 1
cmpi opmrl reg ; integer comparison; must preceed a conditional jump;
it compares the first operand with the second op and
sets the "processor status". (The status remains the
same until the next cmp instruction is executed.)
E.g, a subsequent jgt will jump if op1 > op2
push opmrl ; push a data item onto the stack. Operand can be
; omitted, in which case an empty element is pushed.
pop opmr ; pops an element from the stack. If the operand is
; non-empty, the element is moved there
jsr target ; jump to target and push the current pc onto the stack
ret ; pop an address from the stack and jump there
link # ; push frame pointer (fp) onto stack, copy sp into fp,
; push # empty cells onto stack
unlnk ; copy fp into sp, pop fp from stack
cmpr opmrl reg ; real comparison
jmp target ; unconditional jump
jgt target ; jump if (op1 of the preceeding cmp was) greater (than op2)
jlt target ; jump if less than
jge target ; jump if greater of equal
jle target ; jump if less or equal
jeq target ; jump if equal
jne target ; jump if not equal
sys readi opmr ; system call for reading an integer from input
sys readr opmr ; system call for reading a real value
sys writei opmr ; system call for outputting an integer
sys writer opmr ; system call for outputting an integer
sys writes sid ; system call for outputting a string constant
sys halt ; system call to end the execution
end ; end of the assembly code (not an opcode)
notation used for the operands:
id stands for the name of a memory location
sid stands for the name of a string constant
# stands for an integer number
target stands for the name of a jump target
$offset stands for a stack variable at address fp+offset
reg stands for a register, named r0,r1,r2, or r3, case insensitive
opmrl stands for a memory id, stack variable, register or a number (literal),
the format for real is digit*[.digit*][E[+|-]digit*]
opmr stands for a memory id, stack variable, or a register
; semicolon leads in a comment (which is ignored by the interpreter). It can
be at the beginning on a line or after an assembly code
data representation:
No assumption can be made about the representations. Real and integer cannot be
mixed. Using an integer where a real is expected (and vice versa) leads to
undefined results.
-------------------------------
Running tiny
syntax : tiny sourcefile [d1|d2|d3 [mix]]
the second argument generates debug output
d1: print a program listing
d2: d1 + print each line as it gets interpreted
d2: d2 + print machine status and variable content at each step
mix: allow declarations inbetween code
-----------------------------
A longer sample program. The program asks for a number and prints 5 asterisk
triangles with this length.
var length
str star "*"
str prompt "enter number: "
str eol "\n"
move 0 r2
sys writes prompt
sys readi length
move 1 r3
label outerloop
move r3 r0
label starloop
sys writes star
subi 1 r0
cmpi 0 r0
jne starloop
sys writes eol
addi 1 r3
cmpi length r3
jge outerloop
move 1 r3
addi 1 r2
cmpi 4 r2
jge outerloop
sys halt
end