![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Location: CT
Posts: 3
Rep Power: 0
![]() |
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.
syscallLast edited by tsgrimey; Feb 8th, 2005 at 9:38 PM. |
|
|
|
|
|
#2 |
|
Programmer
|
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.
__________________
I can pick my friends. And I can pick my nose. So, why can't I pick my friend's nose? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|