## 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..

**A note on jumps**

Note that we only have jz as a conditional jump, ie, the jump will occur when the result of the prior ALU operation is zero. In programming situations we will require more, ie, we want instructions like

jgt r1, r2, xyz // r1, r2 denotes two registers //and xyz denotes an address/label

Which will jump to address xyz when r1>r2 and will execute the next instruction otherwise. But it is possible to construct a small code segment which is functionally the same with jgt instruction:

sub r3 r1 r2 //twos complement subtraction ldi r4, 0x8000 and r3 r3 r4 //check the sign bit jz xyz //jump if sign bit is zero, ie, if r1>r2

jump greater or equal instruction, jge r1, r2, xyz, can be “simulated in a similar way

sub r3 r1 r2 //twos complement subtraction jz xyz //jump if r1 =r2 ldi r4, 0x8000 and r3 r3 r4 //check the sign bit jz xyz //jump if sign bit is zero, ie, if r1>r2

**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 ldi 3 0x0 or 3 0 3 jz out //checks 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 ldi 3 0x0 or 3 0 3 jz out //checks 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 ldi 3 0x0 or 3 1 3 jz out //checks 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