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

AI Thread Summary
The discussion revolves around a programming challenge where the user is tasked with reading a sequence of integers as a string, converting them to numbers, and calculating their sum. The code provided attempts to achieve this but encounters issues with correctly summing the numbers. The user explains the logic behind their approach, which includes reading characters one by one, checking for spaces, and using bitwise operations to simulate multiplication by 10, as direct multiplication is not allowed per the assignment guidelines.Key points include the method of converting ASCII characters to integers by subtracting 48, which is necessary to transform character representations of digits into their numeric values. The user also seeks clarification on why multiplication is involved in the process, emphasizing that it is essential for constructing multi-digit numbers from individual characters. The discussion highlights the specific requirements of the assignment, including the necessity to handle input as strings and process each character accordingly to compute the final sum.
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, ...
 
Back
Top