Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old May 14th, 2006, 12:39 AM
ok_good ok_good is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 15 ok_good User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 4 m 57 sec
Reputation 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
     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?

Reply With Quote
  #2  
Old May 14th, 2006, 03:44 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,038 Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 8 h 32 m 33 sec
Reputation Power: 330
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 ShortUnderstanding the C/C++ Preprocessor
Taming PythonA 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Help my mips program code!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway