Converting numerical input to corresponding string using MIPS

In summary, the conversation is about a MIPS SPIM program that takes integer input and outputs the corresponding words. However, the program is currently not functioning correctly, as it outputs zero repeatedly and gets stuck in an infinite loop. The speaker is seeking help in finding the errors in their code.
  • #1
JAO012
3
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
  • #2
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.
 

1. How do I convert a numerical input to a string in MIPS?

To convert a numerical input to a string in MIPS, you can use the li (load immediate) instruction to load the numerical value into a register, then use the syscall instruction to print the value as a string. Alternatively, you can use the itoa function to convert the numerical value to an ASCII string.

2. What is the syntax for converting numerical input to a string in MIPS?

The syntax for converting numerical input to a string in MIPS depends on which method you choose to use. For the first method, the syntax would be li $t0, numerical_value followed by syscall. For the second method, the syntax would be itoa $t0, $t1, numerical_value, where $t0 is the destination register for the converted string and $t1 is the source register for the numerical value.

3. Can I convert a numerical input to a string without using the syscall instruction?

Yes, it is possible to convert a numerical input to a string without using the syscall instruction. As mentioned before, you can use the itoa function to convert the numerical value to an ASCII string. You can also write your own function to manually convert the numerical value to a string, using bitwise operations and ASCII codes.

4. How do I handle decimal numbers when converting numerical input to a string in MIPS?

When converting decimal numbers to a string in MIPS, you will need to take into account the decimal point and the digits after it. You can use the div (divide) instruction to separate the integer and fractional parts of the decimal number, then convert each part separately to a string using the methods mentioned above. Finally, you can combine the two strings with a decimal point in between.

5. Are there any limitations to converting numerical input to a string in MIPS?

There may be some limitations to converting numerical input to a string in MIPS, depending on the method you use. If using the syscall instruction, there may be a limit to the length of the string that can be printed. If using the itoa function, there may be limitations on the range of numerical values that can be converted. Additionally, converting decimal numbers to a string may result in loss of precision due to the limitations of representing floating-point numbers in MIPS.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
5
Views
6K
Back
Top