Converting numerical input to corresponding string using MIPS

Click For Summary
The discussion centers around a MIPS SPIM program designed to convert an integer input into its corresponding word representation. The user successfully inputs an integer but encounters an issue where the output is consistently "zero" repeating infinitely. The source code provided includes various sections for input handling, output strings, and conversion logic, but it lacks proper handling for the conversion loop and stack management.Key points of concern include the infinite loop caused by incorrect branching and the failure to properly process the digits of the input integer. Suggestions for troubleshooting include using a debugger to step through the program to identify why the output is incorrect and to ensure that the loop iterates based on the number of digits in the input rather than entering an infinite cycle. The discussion emphasizes the need for careful examination of the logic flow, particularly in the conversion and loop handling sections of the code.
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · 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
10K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 22 ·
Replies
22
Views
5K