PMIS Version 0.4 Demonstration Programs

Table of contents

Arithmetic calculationUsing memory elements
Conditional branchesConditional branches with "else"
A recursive routineLess than
Greater thanLess than or equal to
Greater than or equal toAndi versus Ori
Every PMIS instructionPseudo random number generator
Little-endian versus Big-endianAn infinite loop
A switch statement using registersA bubble sort

Instructions for use

Cut and paste the programs below into the text input box of PMIS, and follow the instructions regarding initial values or where to look for results. Some examples have been taken from Patterson and Hennessy.

Arithmetic calculation

This program demonstrates 'f = (g + h) - (i + j)' where g=5, h=10, i=4, j=3. (Patterson & Hennessy, p110). It performs the calculation, and places the final result in $s0.
addi $s1, $zero, 5
addi $s2, $zero, 10
addi $s3, $zero, 4
addi $s4, $zero, 3
add $t0, $s1, $s2
add $t1, $s3, $s4
sub $s0, $t0, $t1
[back to top]

Using memory elements

This program demonstrates 'g = h + A[i]' where h=1, i=6. (Patterson & Hennessy p114). It will retrieve the 7th element of the array whose base address (0x38) is in $s3. Try stepping through it, and look for the final result in $s1. This array has only got 88 elements, however - PMIS has only got 400b of memory!
#initialise values#
add $t8, $zero, $zero 
addi $t9, $zero, 324
top:
addi $t8, $t8, 4
sw $t8, 56($t8)
beq $t8, $t9, end
j top
end:
#demo prog start#
addi $s3, $zero, 56
addi $s4, $zero, 6
addi $s2, $zero, 1
add $t1, $s4, $s4
add $t1, $t1, $t1
add $t1, $t1, $s3
lw $t0, 0($t1)
add $s1, $s2, $t0
[back to top]

Conditional branches

This program demonstrates 'if (i == j) go to L1; f = g + h; L1: f = f - i' where i=7, j=6, f=10, g=5, h=8. (Patterson & Hennessy, p123-4 #1). Try stepping through it, and look for the final result in $s0.
addi $s3, $zero, 7
addi $s4, $zero, 6
addi $s1, $zero, 5
addi $s2, $r0, 8
beq $s3, $s4, L1
add $s0, $s1, $s2
L1: sub $s0, $s0, $s3
[back to top]

Conditional branches with "else"

This program demonstrates 'if (i == j) f = g + h; else f = g - h' where i=7, j=6, f=10, g=5, h=8. (Patterson & Hennessy, p123-4 #2). Try stepping through it and look for the final result in $s0.
addi $s3, $zero, 7
addi $s4, $zero, 6
addi $s1, $zero, 5
addi $s2, $r0, 8
bne $s3, $s4, Else
add $s0, $s1, $s2
j Exit
Else: sub $s0, $s1, $s2
Exit:
[back to top]

A recursive routine

This program demonstrates a recursive procedure in GCO2812 Ass1 2002. Please set the initial value of $a0 (0 < $a0 < 0x1e) by directly editing the register. I wouldn't advise stepping through this one! (Used with permission).
jal L3
j end
L3: bne $a0, $zero, L1
addi $v0, $zero, 1
j L2
L1: addi $sp, $sp, -8
sw $ra, 0($sp)
sw $a0, 4($sp)
addi $a0, $a0, -1
jal L3
add $v0, $v0, $v0
addi $v0, $v0, 1
lw $ra, 0($sp)
lw $a0, 4($sp)
addi $sp, $sp, 8
L2: jr $ra
end:
[back to top]

Less than

Check how many times 'ffffffff' is written just below the last instruction in memory after the program is run. The counter here is $t0; the target is $t1.
addi $t0, $zero, 1
addi $t1, $zero, 3
addi $s0, $zero, -1
addi $s1, $zero, 60
loop: 
slt $t2, $t0, $t1
beq $t2, $zero, end
addi $t0, $t0, 1
addi $s2, $zero, 0
add $s2, $t0, $t0
add $s2, $s2, $s2
add $s2, $s2, $s1
sw $s0, 0($s2)
j loop
end:
[back to top]

Greater than

Check how many times 'ffffffff' is written just below the last instruction in memory after the program is run. The counter here is $t0; the target is $t1.
addi $t0, $zero, 3
addi $t1, $zero, 1
addi $s0, $zero, -1
addi $s1, $zero, 60
loop: 
slt $t2, $t1, $t0
beq $t2, $zero, end
addi $t0, $t0, -1
addi $s2, $zero, 0
add $s2, $t0, $t0
add $s2, $s2, $s2
add $s2, $s2, $s1
sw $s0, 0($s2)
j loop
end:
[back to top]

Less than or equal to

Check how many times 'ffffffff' is written just below the last instruction in memory after the program is run. The counter here is $t0; the target is $t1.
addi $t0, $zero, 1
addi $t1, $zero, 3
addi $s0, $zero, -1
addi $s1, $zero, 60
loop: 
slt $t2, $t1, $t0
bne $t2, $zero, end
addi $t0, $t0, 1
addi $s2, $zero, 0
add $s2, $t0, $t0
add $s2, $s2, $s2
add $s2, $s2, $s1
sw $s0, 0($s2)
j loop
end:
[back to top]

Greater than or equal to

Check how many times 'ffffffff' is written just below the last instruction in memory after the program is run. The counter here is $t0; the target is $t1.
addi $t0, $zero, 3
addi $t1, $zero, 1
addi $s0, $zero, -1
addi $s1, $zero, 60
loop: 
slt $t2, $t0, $t1
bne $t2, $zero, end
addi $t0, $t0, -1
addi $s2, $zero, 0
add $s2, $t0, $t0
add $s2, $s2, $s2
add $s2, $s2, $s1
sw $s0, 0($s2)
j loop
end:
[back to top]

andi versus ori

This program demonstrates the difference between 'andi' and 'ori'. Try stepping through it.
addi $t0, $r0, -1
ori $t1, $t0, 4096
andi $t2, $t0, 4096
[back to top]

Every PMIS instruction!

This program uses every single instruction in the PMIS instruction set. There is absolutely no other point to this program. This program is identical to the "Demo" button program.
addi $t0, $r0, 1
addi $t1, $zero, 2
add $t2, $t0, $t1
and $t4, $t2, $t3
andi $t5, $t3, 255
beq $t2, $t4, skip1
addi $t0, $r0, 32767
skip1:
bne $t1, $t2, skip2
addi $t0, $r0, 32767
skip2:
j skip3
addi $t0, $r0, 32767
skip3:
jal function1
addi $t6, $r0, 4095
j skipFunction1
function1:
addi $t0, $t0, 32767
jr $ra
skipFunction1:
sb $t5, 180($t5)
lb $t5, 256($r0)
lui $t7, 65535
nop
nor $t8, $t7, $r0
ori $t7, $t7, 65535
addi $sp, $sp, -12
sw $t3, 0($sp)
sw $t5, 4($sp)
sw $t6, 8($sp)
add $t0, $zero, $r0
add $t1, $zero, $r0
add $t2, $zero, $r0
add $t3, $zero, $r0
add $t4, $zero, $r0
add $t5, $zero, $r0
add $t6, $zero, $r0
add $t7, $zero, $r0
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 0($sp)
addi $sp, $sp, 12
or $s3, $s0, $s1
ori $s4, $s3, 240
sb $s4, 394($r0)
sll $s5, $s4, 31
slt $s7, $s5, $s6
srl $s5, $s5, 31
sub $s7, $s6, $s5
[back to top]

Pseudo-random number generator

This program writes sort-of random numbers into nearly all the available PMIS memory using x = (a * x + b) % c. After the program has finished running, look for the first number in 0x80. Don't step through this one either!
addi $t5, $r0, 128
addi $a0, $r0, 16384
addi $t4, $r0, 67
addi $t0, $r0, 3
rpt:
slt $t2, $r0, $t4
beq $t2, $r0, end
addi $t4, $t4, -1
sll $t1, $t0, 1
add $t1, $t0, $t1
addi $t1, $t1, 5
add $t6, $r0, $r0
jal getMod
add $t0, $r0, $t3
sw $t3, 0($t5)
add $t3, $r0, $r0
addi $t5, $t5, 4
j rpt
getMod:
slt $t2, $t1, $a0
beq $t2, $r0, calcMod
add $t3, $r0, $t1
j goBack
calcMod:
slt $t2, $t3, $t1
beq $t2, $r0, skip
add $t3, $t3, $a0
j calcMod
skip:
sub $s0, $t3, $t1
sub $t3, $a0, $s0
goBack:
jr $ra
end:
[back to top]

Little-endian versus Big-endian

This program demonstrates the difference between big-endian and little-endian machines. Run it through first as one, inspect the memory contents of 0x28, then run it through again selecting the other -endian.
addi $t0, $t0, 18
sb $t0, 40($r0)
addi $t1, $t1, 52
sb $t1, 41($r0)
[back to top]

An infinite loop

An infinite loop! Try it; you'll like it! (It will be stopped when the iterations become greater than the figure set in the "Max. iterations" box).
addi $t0, $zero, 0
loopStart:
addi $t0, $t0, 1
j loopStart
[back to top]

A switch statement using registers

This program places an 'a', 'b', or 'c' in $s0 depending on the value placed by the user into $t0. In pseudocode, this is:
switch ($t0) {
    case 0 to 49   : $s0 = 'a'; break;
    case 50 to 99  : $s0 = 'b'; break;
    case 100 to 149: $s0 = 'c'; break;
    default: do nothing;
}
Hex values for 49, 99, and 149 are 0x31, 0x63, and 0x95 respectively.

Assembly language for the switch statement is:
addi $t1, $zero, 50 #first limit#
addi $t2, $zero, 100 #second limit#
addi $t3, $zero, 150 #final limit#
slt $t4, $t0, $zero
bne $t4, $zero, Exit #test for < 0#
slt $t4, $t0, $t3
beq $t4, $zero, Exit #test for > 149#
slt $t4, $t0, $t1
bne $t4, $zero, L0
slt $t4, $t0, $t2
bne $t4, $zero, L1
slt $t4, $t0, $t3
bne $t4, $zero, L2
j Exit
L0: addi $s0, $zero, 10
j Exit
L1: addi $s0, $zero, 11
j Exit
L2: addi $s0, $zero, 12
Exit: 
[back to top]

A bubble sort

This sorts an array of integers in memory, initially in descending value, into ascending value. They start at 0x154, and are sorted in position.
#set up array#
addi $t0, $r0, 15
addi $t1, $r0, 340
top:
sw $t0, 0($t1)
addi $t0, $t0, -1
addi $t1, $t1, 4
beq $t0, $r0, endLoop
j top
endLoop:
#start sort#
addi $t0, $r0, 0 #outer counter#
addi $t1, $r0, 14 #outer target#
addi $t2, $r0, 1 #inner counter#
addi $t3, $r0, 15 #inner target#
addi $s0, $r0, 340 #base address#
outer: nop
inner:
add $s1, $r0, $t0
add $s1, $s1, $s1
add $s1, $s1, $s1
add $s1, $s1, $s0
lw $s3, 0($s1)
add $s2, $r0, $t2
add $s2, $s2, $s2
add $s2, $s2, $s2
add $s2, $s2, $s0
lw $s4, 0($s2)
slt $s5, $s4, $s3
beq $s5, $r0, skipSwap
sw $s3, 0($s2)
sw $s4, 0($s1)
skipSwap:
addi $t2, $t2, 1
bne $t2, $t3, inner
addi $t0, $t0, 1
addi $t2, $t0, 1
bne $t0, $t1, outer
[back to top]
Valid HTML 4.01
Last updated on 27 November, 2003 by Paul Morris