Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MIPS help

  1. Feb 26, 2008 #1
    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 (Text):

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

                .globl        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

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

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

            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

            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

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

            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!
  2. jcsd
  3. Feb 27, 2008 #2
    If you are supposed to sum a sequence of numbers, why is there any multiplication involved?
  4. Feb 27, 2008 #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.
  5. Feb 27, 2008 #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?
  6. Feb 27, 2008 #5
    The user enters a buch of integers seperated 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 untill 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 thats how he wants it!
  7. Feb 27, 2008 #6
    Where does the 48 come from in this line:

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

  8. Feb 27, 2008 #7
    I believe it's to change the character to an integer
  9. Feb 27, 2008 #8


    User Avatar
    Homework Helper

    That would covert an ASCII "0" to a numeric 0, an ASCII "1" to a numeric 1, ...
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook