Quick start Rz
This short article will help you to learn Rz language quickly. Here
are the list of distinct features of Rz (version 3.6).
1 Rz is quite like C with only integer type.
2 Global variables must be declared. Local variables are
automatic.
3 An array must be global.
4 Pass reference by &v.
A session in Rz
Try to compile "fac.txt" shown here:
// factorial
fac(n)
if( n == 0 ) return 1
else return n * fac(n-1)
main()
print(fac(6))
Here is the command line and the output at the screen:
C:\rz36\test>rz36 fac.txt
.symbol
...
.code 0
...
jal rads main
trap r0 #0
; fun fac pv 1
:fac
st r1 @1 fp
...
eq r2 r1 #0
jf r2 L102
mov retval #1
jmp L101
jmp L103
:L102
sub r3 r1 #1
push sp r3
jal rads fac
mul r2 r1 r28
mov retval r2
jmp L101
:L103
:L101
ld rads @0 fp
...
ret rads
; fun main pv 0
:main
...
mov r1 #6
push sp r1
jal rads fac
trap r28 #1
:L104
...
ret rads
.data 200
.end
C:\rz36-3\test>
. . .
Now to run the program, you compile the source and save it in
"out.txt" which is an assembly language file. Then, use an assembler
"as21.exe" to compile it into an object file (machine code),
"out.obj". Use a simulator "sim21.exe" to load and run this object
file. To see a list of available command in the simulator use
"h". Use "q" to exit from the simulator.
C:\rz36-3\test>rz36 fac.txt >out.txt
C:\rz36-3\test>as21 out.txt
C:\rz36-3\test>sim21 out.obj
load program, last address 200
>t
PC 0 mov r30 #3500 r0:0 r1:0 r2:0
r3:0 r4:0 r5:0 r6:0 r7:0 r8:0 r9:0
r27:0 r28:0 r29:0 r30:3500 r31:0
>t
PC 1 mov r29 #3000 r0:0 r1:0 r2:0
r3:0 r4:0 r5:0 r6:0 r7:0 r8:0 r9:0
r27:0 r28:0 r29:3000 r30:3500 r31:0
>t
PC 2 jal r27 27
r0:0 r1:0 r2:0 r3:0 r4:0 r5:0 r6:0 r7:0 r8:0 r9:0
r27:3 r28:0 r29:3000 r30:3500 r31:0
>t
PC 27 st r1 @1 r30 r0:0 r1:0 r2:0
r3:0 r4:0 r5:0 r6:0 r7:0 r8:0 r9:0
r27:3 r28:0 r29:3000 r30:3500 r31:0
>g
720 stop, clock 151, execute 151 instructions
. . .
C:\rz36-3\test>
That's it. Enjoy!
Examples
Ex1. To add one to ten.
main()
i = 1
s = 0
while( i <= 10 )
s = s + i
i = i + 1
print(s)
Ex2. Find a maximum value in an array.
ax[10] // declare an array size 10
// max of array a size n
max(a, n)
m = a[0]
i = 1
while( i < n )
if( m < a[i] )
m = a[i]
i = i + 1
return m
// initialise array
init(a, n)
i = 0
while( i < n )
a[i] = i + 10
i = i + 1
main()
init(&ax,10)
// pass ref of ax
print(max(&ax,10))
Ex3. Recursive function
// factorial
fac(n)
if( n == 0 ) return 1
else return n * fac(n-1)
main()
print(fac(6))
Ex4. Making and printing a linked list of integer
cell[1000]
freecell // cell storage
// access functions
head(a)
return cell[a]
tail(a)
return cell[a+1]
sethead(a,i)
cell[a] = i
settail(a,i)
cell[a+1] = i
newnode(i) // get
a new node, with i
p = freecell
freecell = freecell + 2
if( freecell > 10000 )
print("no more cell")
sethead(p,i)
settail(p,0)
return p
// link node a to list m
insert(m,a)
settail(a,tail(m))
settail(m,a)
// print item in list recursively
prlist2(m)
if( m != 0 )
print(head(m),"
")
prlist2(tail(m))
// main print list, skip header node
prlist(m)
prlist2(tail(m))
main()
h =
newnode(0) // make header
insert(h,newnode(1))
insert(h,newnode(2))
insert(h,newnode(3))
prlist(h)
last update 20 Aug 2017