Converting numerical input to corresponding string using MIPS

Click For Summary
SUMMARY

The discussion focuses on a MIPS SPIM program designed to convert integer input into corresponding string representations. Users reported issues with the program outputting "zero" repeatedly and entering an infinite loop. Key problems identified include improper handling of the stack and incorrect branching logic in the conversion process. Debugging steps suggested include using the SPIM debugger to trace the program execution and identify the root causes of the output and looping issues.

PREREQUISITES
  • MIPS assembly language programming
  • Understanding of SPIM simulator functionalities
  • Stack operations in assembly language
  • Conditional branching in MIPS
NEXT STEPS
  • Debug MIPS SPIM programs using the built-in debugger
  • Learn about MIPS stack management and memory allocation
  • Study MIPS conditional branching and loops
  • Explore string manipulation techniques in MIPS assembly
USEFUL FOR

This discussion is beneficial for MIPS assembly language programmers, computer science students learning about low-level programming, and developers troubleshooting similar input/output issues in SPIM environments.

JAO012
Messages
3
Reaction score
0
I am writing a MIPS SPIM program that will take integer input, and output the corresponding words. For example, if 123 is input, then one two three will be output.

I can input an integer but I get zero repeating infinite times as the output every time. Any help is appreciated.

My source code is below:


.data
prompt: .asciiz "Please input a positive integer "
echo: .asciiz "\nYou have input "
echo1: .asciiz "\nThis is an acceptable value. The output is "
echo2: .asciiz "\nThis is not an acceptable value. Please try again. "
zero0: .asciiz "zero "
one1: .asciiz "one "
two2: .asciiz "two "
three3: .asciiz "three "
four4: .asciiz "four "
five5: .asciiz "five "
six6: .asciiz "six "
seven7: .asciiz "seven "
eight8: .asciiz "eight "
nine9: .asciiz "nine "
buffer: .space 100

.text
main:

li $v0,4 #print string
la $a0, prompt
syscall

li $v0, 5 #reads in integer
syscall

li $v0, 4 #print string
la $a0, echo
syscall

#li $v0, 1
#syscall

move $v0, $t1 #moves integer into $t1

j checkin

loop1:
beq $t1,0,next #when there are no more values, stop pushing
sub $sp, $sp, 4 #subtract 4 bytes off stack
sw $s0,0($sp) #store values in $s0
j next

next:
bge $t1,0,convert #go through and remove values from stack
lw $v0, 4($sp) #load into v0
add $sp, $sp, 4 #add bytes back to stack
jr $ra #jump register

##check if the input is positive
checkin:
bgez $t1, yes
blez $t1, no
j loop1

convert: #changes value of each integer to respective word value, and stores the result #on the stack
beq $t1,0,zero
beq $t1,1,one
beq $t1,2,two
beq $t1,3,three
beq $t1,4,four
beq $t1,5,five
beq $t1,6,six
beq $t1,7,seven
beq $t1,8,eight
beq $t1,9,nine

j onestring

# each loop takes the value off the stack and then prints the string value corresponding to #each integer

zero:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0, 4
la $a0, zero0
syscall


j convert

one:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0, 4
la $a0, one1
syscall
j convert

two:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0, 4
la $a0, two2
syscall

j convert

three:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0, 4
la $a0, three3
syscall

j convert

four:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0,4
la $a0, four4
syscall

j convert

five:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0,4
la $a0, five5
syscall

j convert

six:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0,4
la $a0, six6
syscall

j convert

seven:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0,4
la $a0, seven7
syscall

j convert

eight:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0,4
la $a0, eight8
syscall

j convert

nine:
sub $sp, $sp, 4
sw $t3, 0($sp)

li $v0,4
la $a0, nine9
syscall

j convert

onestring: #combines all string values of integers into one string
la $t3, buffer
li $a1, 100 #100 max chars
beqz, $t3, end
lw $t3, 4($sp)
add $sp, $sp, 4

j end

yes: #acceptable input
li $v0, 4
la $a0, echo1
syscall

j convert

end:
li $v0, 4 #print full string
la $a0, buffer
syscall

li $v0, 10
syscall

no:
li $v0,4 #not acceptable input
la $a0, echo2
syscall

li $v0,10
syscall
 
Technology news on Phys.org
Your SPIM environment includes a debugger, I'm pretty sure. Use it to single step through your program. You need to check two things:
1) why zero is being displayed every time.
2) why you're getting an infinite loop instead of a loop that runs as many times as the number of digits in your input.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 22 ·
Replies
22
Views
5K