Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   SPIM Assembly Programming Simulation Problem (http://www.programmingforums.org/showthread.php?t=2169)

tsgrimey Feb 8th, 2005 9:36 PM

SPIM Assembly Programming Simulation Problem
 
I'm trying to get this small program to work properly, but having problems. All its suppose to do is read in a string from a user, then output to console with the characters reversed. I also must mention that I can only change the code between "now reverse code here"...and..."not beyond here". Anyone know how to solve this? :(


:

# reverse --- Read and echo a reversed copy of it to the console

            .data
line:      .space 80
reversed_line: .space 80
prompt:    .asciiz "Enter a line to be echoed.\n"
nl:        .asciiz "\n"

            .text
            .globl main
main:     

            li $v0, 4        # Syscall code for print string.
            la $a0, prompt    # Starting address of string.
            syscall

            li $v0, 8        # Syscall code for read string.
            la $a0, line      # Starting address of buffer.
            li $a1, 80        # Length of buffer.
            syscall

            li $v0, 4
            la $a0, nl
            syscall

            li $v0, 4
            la $a0, line
            syscall


            # now reverse it your code should go here
           
            la  $s2, reversed_line
            addi $s2, $s2, 79      # end of str2
            addi $t1, $zero, 0      # null
            sb  $t1, 0($s2)
            addi $s2, $s2, -1
            lb  $t2, nl            # newline
            sb  $t2, 0($s2)

            la  $s1, line

loop:
            lb  $t1, 0($s1)        # read char from str1
            beq  $t1, $t2, done    # '\n' - end of str1 ?

            addi $s2, $s2, -1      # update str2 pointer
            sb  $t1, 0($s2)        # write to str2

            addi $s1, $s1, 1        # update str1 pointer
            j    loop
       
done: 

            # and not beyond here 


            # now print the reversed string 
       
            li $v0, 4
            la $a0, reversed_line   
            syscall


            li $v0, 4
            la $a0, nl
            syscall

            j main

            li $v0, 10        # Syscall code for exit.
            syscall


EdSalamander Feb 9th, 2005 2:21 AM

It's been awile since I've done anything with SPIM, but try changing you addi operations to addiu. Since you're adding plain numbers instead of numbers in registers, it might actually be better if they were unsigned.


All times are GMT -5. The time now is 6:06 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC