### Thread: Help my mips program code!!

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2005
Posts
15
Rep Power
0

#### Help my mips program code!!

Use the frame pointer and stack-based variables for this. Use registers only where need for immediate calculation work, or for pointers into the array to sort it. Write a program prompts the user for ten numbers and stores them in an array. Then sort the array using the ripple sort algorithm and a swap() subroutine that takes two pointers to integers and swaps the values the pointers point at.

Print the values in the array with tabs between them before and after the sort. Do not print a trailing tab after the row of numbers. For example, with <\t> representing a tab and <\n> representing a line feed, a row might look like this:

0 <\t> 1 <\t> 2 <\t> 3 <\t> 4 <\t>5 <\t> 6 <\t> 7 <\t>8 <\t>9 <\n>

The ripple sort algorithm works like this:

int *p, *ql;
for( p = &a[0]; p < &a[9]; p += sizeof(int) )
for( q = p + sizeof(int); q <= &a[9]; q += sizeof(int) )
if( *p > *q )
swap( p, q ) // pass in p and q, swap *p and *q
end if
end for( q )
end for( p )

In this algorithm, p and q are register pointers. All other variables should be held on a stack frame and fetched and stored as needed.

Code:
```.text
.globl __start

__start:
sub     \$sp,\$sp,4     # push \$ra
sw     \$ra,(\$sp)
sub     \$sp,\$sp,4
sw     \$fp,(\$sp)     # push \$fp
sub     \$sp,\$sp,8     # \$fp = \$sp - space_for_variables (8)
move     \$sp,\$fp     # \$sp = \$fp

jal     input     # input the integer
nop

sub     \$sp, \$sp, 4     # push &a  4(\$sp)
move     \$t0, \$fp     # save address of a in register \$t0
sw     \$t0, (\$sp)     # move pointer (*a) to stack
nop

jal     output     # output the integer
nop

sub     \$sp, \$sp, 4     # push &a  4(\$sp)
move     \$t0, \$fp     # save address of a in register \$t0
sw     \$t0, (\$sp)     # move pointer (*a) to stack
nop

sub     \$sp, \$sp, 4     # push &b  0(\$sp)
move     \$t0, \$fp     # save address of b in register \$t0
sw     \$t0, (\$sp)     # move pointer (*b) to stack
nop

jal     body
nop

sub     \$sp, \$sp, 4     # push &a  4(\$sp)
move     \$t0, \$fp     # save address of a in register \$t0
sw     \$t0, (\$sp)     # move pointer (*a) to stack
nop

jal     result
nop

add     \$fp, \$fp, 8     # de-allocate local variables
move     \$sp, \$fp     #
lw     \$fp, (\$sp)     # pop \$fp
lw     \$ra, (\$sp)     # pop \$ra
nop               #

.text
.globl input

input:

la     \$8,array     # init base register
li     \$9,1     # init count
li     \$10,10     # get 10

li     \$v0,4     # call to output string
la     \$a0,pr1
syscall

in:     bgt     \$9,\$10,exit
nop
li     \$v0,5     # call to input integer
syscall

move     \$a0,  \$v0
sw     \$a0,0(\$8)     # store word
addiu     \$8,\$8,4     # increment the array

j     in
nop
exit:
nop

.data
array:     .word 10

.data
pr1:     .asciiz "Input integer:"

.text
.globl output

output:
la     \$8,array     # init base register
li     \$9,1     # reinit cout
li     \$10,10     # get 10

li     \$v0,4     # call to print string
la     \$a0,pr2
syscall

out:     bgt     \$9,\$10,exit1
nop

lw     \$a0,0(\$8)     # load integer to \$a0
nop
li     \$v0,1     # call to output integer
syscall

li     \$v0,4     # call to print <\t>
la     \$a0,sp1
syscall

addiu     \$8,\$8,4     # increment the array
j     out
nop

exit1:
jr     \$ra
nop

.data
pr2:     .asciiz "Your input integer are:"
sp1:     .asciiz "\t"

# int *p, *ql;
# for( p = &a[0]; p < &a[9]; p += sizeof(int) )
#      for( q = p + sizeof(int); q <= &a[9]; q += sizeof(int) )
#            if( *p > *q )
#                swap( p, q )  // pass in p and q, swap *p and *q
#            end if
#      end for( q )
# end for( p )

.text
.globl body

body:
sub     \$sp,\$sp,4     # push \$ra
sw     \$ra,(\$sp)
sub     \$sp,\$sp,4     # push \$fp
sw     \$fp,(\$sp)
sub     \$fp,\$sp,4     # \$fp = \$sp - space_for_variables (4)
move     \$sp,\$fp

lui     \$8,0x1000     # init base register

nop
li     \$10,1     # init count
li     \$11,8     # get 8
bgt     \$10,\$11,ex1
nop

jal     body1
nop

move     \$s0,\$8
addiu     \$8,\$8,4     # increment the array

ex1:     add     \$fp, \$fp, 4     # de-allocate local variables
move     \$sp, \$fp     #
lw     \$fp, (\$sp)     # pop \$fp
lw     \$ra, (\$sp)     # pop \$ra
jr     \$ra
nop

.text
.globl body1

body1:
sub     \$sp,\$sp,4     # push \$ra
sw     \$ra,(\$sp)
sub     \$sp,\$sp,4     # push \$fp
sw     \$fp,(\$sp)
sub     \$fp,\$sp,4     # \$fp = \$sp - space_for_variables (4)
move     \$sp,\$fp

nop
li     \$13,1     # init count
li     \$14,9     # get 9
bgt     \$13,14,ex2
nop

jal     body2
nop

addiu     \$8,\$8,4     # increment the array

ex2:     add     \$fp, \$fp, 4     # de-allocate local variables
move     \$sp, \$fp     #
lw     \$fp, (\$sp)     # pop \$fp
lw     \$ra, (\$sp)     # pop \$ra
jr     \$ra
nop

.text
.globl body2

body2:
sub     \$sp,\$sp,4     # push \$ra
sw     \$ra,(\$sp)
sub     \$sp,\$sp,4     # push \$fp
sw     \$fp,(\$sp)
sub     \$fp,\$sp,4     # \$fp = \$sp - space_for_variables (4)
move     \$sp,\$fp

bgt     \$9,\$12,lo1
nop

add     \$fp, \$fp, 4     # de-allocate local variables
move     \$sp, \$fp     #
lw     \$fp, (\$sp)     # pop \$fp
lw     \$ra, (\$sp)     # pop \$ra
jr     \$ra
nop

lo1:     move     \$s2,\$9     # move p to \$s2
move     \$9,\$12     # copy q to p
move     \$12,\$s2     # copy \$s2 to q

sw     \$12,0(\$8)     # store p

move     \$8,\$s0
sw     \$9,0(\$8)     # store q

addiu     \$8,\$8,4     # increment the array

add     \$fp, \$fp, 4     # de-allocate local variables
move     \$sp, \$fp     #
lw     \$fp, (\$sp)     # pop \$fp
lw     \$ra, (\$sp)     # pop \$ra
jr     \$ra
nop

.text
.globl result

result:
la     \$8,array     # init base register
li     \$9,1     # reinit cout
li     \$10,10     # get 10

li     \$v0,4     # call to print string
la     \$a0,pr3
syscall

out1:     bgt     \$9,\$10,exit2
nop

lw     \$a0,0(\$8)     # load integer to \$a0
nop
li     \$v0,1     # call to output integer
syscall

li     \$v0,4     # call to print <\t>
la     \$a0,sp2
syscall

addiu     \$8,\$8,4     # increment the array
j     out1
nop

exit2:
jr     \$ra
nop

.data
sp2:     .asciiz "\t"```
My problem is when i input 10 integer, then the program won't work, the program cannot continue to run. Please help!!!Thanks! And Any problem on my code?
2. Not knowing much about MIPS assembly, and not seeing any obvious faults from what I've learned through the references listed below, I would recommend writing a smaller version that just reads the input and prints it back out, like so:

Code:
```.text
#   .globl __start

__start:
# handle the minimum required activation record
sub     \$sp,\$sp,4      # push \$ra
sw     \$ra,(\$sp)
sub     \$sp,\$sp,4
sw     \$fp,(\$sp)       # push \$fp
sub     \$sp,\$sp,8      # \$fp = \$sp - space_for_variables (8)
move     \$sp,\$fp       # \$sp = \$fp

jal     input          # input the array of integers
nop

jal     output         # output the array of integers

# followed by the code the two functions```
If that doesn't isolate the problem, try re-writing the input function so that it retrieves each integer from the array and prints it out before reading the next. Finally, if you haven't already, you should single-step through the code in the debugger and observe the registers as they are changed.

When I've got a better grasp on how SPIM is supposed to work, I may try testing some of it myself. Since I haven't looked seriously at MIPS assembly before, it should be interesting.

MIPS Architecture and Assembly Language Overview MIPS architecture
MIPS Instruction Reference
Myrkraverk's Extremely Short MIPS Assembly HOWTO
SPIM: A MIPS32 Simulator