MIPS program which reads in a sequence of numbers entered by the user

In summary: ASCII "9" to a numeric 9. Why would you want to do that?It's in case you have a string that includes those characters and you want to make sure those characters are treated as numbers. For instance, if you were inputting "123" and wanted to make sure the "3" was treated as a number (not a space), this line would change it to "123.
  • #1
tpluscomb
4
0
I've been working on a program which reads in a sequence of numbers entered by the user. It's read in as a string and then converted to numbers. Then I am suppose to get the sum of those numbers and report it back. I got most done and I know I'm close (hope!) but I am stuck at trying to get the number to sum up correctly. This is what I have so far:

Code:
            .data
Prompt1:    .asciiz       "\nPlease enter a list of integers."
newline:	.asciiz		  "\n"
Buffer:		.space		  400

            .globl        main
            .text
main:                                   
		li	  	$v0, 4				# syscall for Print String
		la	  	$a0, Prompt1  		# load address of prompt into $a0
		syscall						# print the prompt

		li	  	$v0, 8		    	# syscall for Read String
		la 	  	$a0, Buffer
		li	  	$a1, 400
		syscall						# read the value of a into $v0
		la    	$s0, Buffer     	# $s0 <= Buffer

		li    	$v0, 4				# output entered string
		la	  	$a0, Buffer
		syscall

		li 		$v0, 4				# output a newline
		la 		$a0, newline
		syscall

		li 	  	$t1, 32         	# set $t1 to ' '
		move 	$s1, $zero			# set value to 0
		move	$s3, $zero			# set temp value to 0

while:
	    lb      $t2, 0($s0)       	# set p*
  		beq  	$t2, $zero, exit	# exit if p* is null
		move	$s2, $zero			# reset current character value
		addi	$s2, $t2, -48		# set current character value

		addi	$s4, $s0, 1			
		lb		$t3, 0($s4)			# set (p+1)*
		
		bne		$t3, $t1, multiply	# Go to multiply if the next character is not whitespace

		add		$s3, $s3, $t2		# temp value = temp value + character value

		add		$s1, $s1, $s3		# value = value + temp value	

		addi	$s0, $s0, 1			# increment pointer p*
		move	$s3, $zero			# reset temp value
		j		while				# go to beginning of while loop


multiply:
		
		sll		$t4, $s3, 3			# x = value multiplied by eight
		sll		$t5, $s3, 1			# y = value multiplied by two
		add		$s3, $t4, $t5		# value = x + y = value * 10

		add		$s3, $s3, $t2		# add the current character to the multiplied value

		addi	$s0, $s0, 1			# increment pointer p*

		j		while				# go to beginning of while loop


exit:
		li 		$v0, 1				# output the value for the sum
		move 	$a0, $s1 
		syscall

		li       $v0, 10           	# terminate run
        syscall                     # return to operating system

# note : the teacher said we are not allowed to use mutiplication yet because he hasn't taught us that yet so that is the cause for the shifting.

Any help is appreciated thanks!
 
Technology news on Phys.org
  • #2
If you are supposed to sum a sequence of numbers, why is there any multiplication involved?
 
  • #3
The mutiplication is used to change the number, which is read in as a character, to an actual number so that you can sum it. For instance if the first number was a 24 you would read in the 2, mutiply it by 10 to make it 20 and then add the next number, 4, to get 24.
 
  • #4
You do know that you can directly read in integers, right? Is the assignment specifically to read in the numbers as strings?
Also, how do you know the program is not working? What does the output give you that is incorrect?
 
  • #5
The user enters a buch of integers separated by spaces. Then we are suppose to read it in as a string, go character by character and determine if it's a space or a character. If it a character we are suppose to take that character mutiply it by 10 and then add the next charater, and keep doing this until we reach a space. At the space we should have the sum and print it out and then move on to the next character which would be the start of a new number. I know it's weird but that's how he wants it!
 
  • #6
Where does the 48 come from in this line:

addi $s2, $t2, -48 # set current character value

?
 
  • #7
I believe it's to change the character to an integer
 
  • #8
ehrenfest said:
Where does the 48 come from in this line:

addi $s2, $t2, -48 # set current character value

?

That would covert an ASCII "0" to a numeric 0, an ASCII "1" to a numeric 1, ...
 
1.

What is the MIPS program?

The MIPS (Microprocessor without Interlocked Pipeline Stages) program is a computer architecture designed for the efficient execution of computer programs. It is commonly used in embedded systems, such as routers and gaming consoles, and is known for its simple and streamlined instruction set.

2.

What is the purpose of a MIPS program that reads in a sequence of numbers entered by the user?

The purpose of this program is to allow the user to input a series of numbers, which are then stored in the computer's memory for further processing. This can be useful for performing calculations or manipulating data in a specific order.

3.

How does the MIPS program read in user input?

The MIPS program uses a specific set of instructions, known as system calls, to read in user input. These instructions allow the program to access the input and store it in a designated location in the computer's memory.

4.

What happens if the user enters a non-numeric value in the MIPS program?

If the user enters a non-numeric value, the MIPS program will typically produce an error message and prompt the user to input a valid number. Alternatively, the program may terminate or loop until a valid input is entered, depending on how it is designed.

5.

Can the MIPS program read in multiple sequences of numbers from the user?

Yes, the MIPS program can be designed to read in multiple sequences of numbers from the user. This can be achieved through the use of loops or by prompting the user for input multiple times within the program.

Similar threads

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