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

Converting numerical input to corresponding string using MIPS

  1. Mar 20, 2012 #1
    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
     
  2. jcsd
  3. Mar 21, 2012 #2

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Converting numerical input to corresponding string using MIPS
  1. MIPS input/output (Replies: 20)

  2. MIPS Input/Output? (Replies: 1)

Loading...