|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
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
addiu $t0, $t0, 4
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
add $sp, $sp, 4 #
lw $ra, ($sp) # pop $ra
add $sp, $sp, 4 #
jr $ra # return to caller
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
addiu $9,$9,1 # count++
j in
nop
exit:
jr $ra # return to caller
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
addiu $9,$9,1 # count++
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
lw $9,0($8) # load p
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
addi $10,$10,1 # count++
ex1: add $fp, $fp, 4 # de-allocate local variables
move $sp, $fp #
lw $fp, ($sp) # pop $fp
add $sp, $sp, 4 #
lw $ra, ($sp) # pop $ra
add $sp, $sp, 4
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
lw $12,0($8) # load q
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
addiu $13,$13,1 # count++
ex2: add $fp, $fp, 4 # de-allocate local variables
move $sp, $fp #
lw $fp, ($sp) # pop $fp
add $sp, $sp, 4 #
lw $ra, ($sp) # pop $ra
add $sp, $sp, 4
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
add $sp, $sp, 4 #
lw $ra, ($sp) # pop $ra
add $sp, $sp, 4
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
add $sp, $sp, 4 #
lw $ra, ($sp) # pop $ra
add $sp, $sp, 4
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
addiu $9,$9,1 # count++
j out1
nop
exit2:
jr $ra
nop
.data
pr3: .asciiz "\nYour result are:"
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
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF #define KINSEY (rand() % 7) λ Scheme is the Red Pill Scheme in Short • Understanding the C/C++ Preprocessor Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1 FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Help my mips program code!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|