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

Click For Summary

Discussion Overview

The discussion revolves around a MIPS program designed to read a sequence of numbers input by the user as a string, convert those characters to integers, and compute their sum. The conversation touches on the implementation details, the necessity of certain operations, and the constraints imposed by the assignment.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their approach to reading a string of integers and summing them, noting they are struggling with the summation logic.
  • Another participant questions the need for multiplication in the summation process, suggesting it may be unnecessary.
  • A participant explains that multiplication is used to convert characters read as digits into their corresponding integer values for summation.
  • Concerns are raised about whether the assignment specifically requires reading numbers as strings, with a suggestion that integers could be read directly.
  • Clarification is sought regarding the use of the value 48 in the code, with one participant asserting it converts ASCII characters to their numeric equivalents.
  • Another participant confirms that the subtraction of 48 is indeed to convert ASCII values of characters '0' through '9' into their respective integer values.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of multiplication in the summation process, with some supporting its use while others question it. There is no consensus on the best approach to reading and processing the input data.

Contextual Notes

Participants note that the assignment constraints limit the use of multiplication, which may affect the implementation of the program. The discussion also highlights potential misunderstandings about the requirements of the assignment.

tpluscomb
Messages
4
Reaction score
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
If you are supposed to sum a sequence of numbers, why is there any multiplication involved?
 
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.
 
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?
 
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!
 
Where does the 48 come from in this line:

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

?
 
I believe it's to change the character to an integer
 
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, ...
 

Similar threads

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