REPTILE: EXAMPLE PROGRAMS
Reptile has a minimum instruction set which is capable of solving quite complicated problems.. In the coming lectures we will continue to add a few more instructions like PUSH, POP, CALL and RET but it is quite sufficient even without these instructions.. Below we will give examples of the sort of problems which are solvable by reptile
Note that these programs can be written in many different ways..
Summing all integers from 1 to N
- The program has two variables, N and SUM. N is considered unsigned.
- The program will sum all the integers between 1 and N
- The program will write its result to the variable SUM.
In the following program, N is arbitrarily chosen as 60.
.data
N: 60
sum:
.code
ldi 0 N
ld 0 0
jz out //if N=0
loop inc 1
add 2 2 1
dec 0
jz out
jmp loop
out ld 0 sum
st 0 2
end jmp end
Summing an alternating series
Same with the above, but this time evaluates 1-2+3-4+5-6+7-8+.... up to N.
.data
N: 60
sum:
.code
ldi 0 N
ld 0 0
jz out //if N=0
loop inc 1
add 2 2 1
dec 0
jz out
inc 1
sub 2 2 1
dec 0
jz out
jmp loop
out ld 0 sum
st 0 2
end jmp end
Multiplying unsigned integers A and B
.data
A:20
B:30
mult:
.code
ldi 0 A
ld 0 0
ldi 1 B
ld 1 1
jz out //if B==0
loop add 2 2 0
dec 1
jz out
jmp loop
ld 0 mult
st 0 2
end jmp end
Computing nth fibonacci number
Multiplying signed numbers
Computing n^x
Factorial
IEEE 754
Bubble sort
Quick sort
Towers of hanoi