1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Arrays and recursion in mips assembly language

  1. May 17, 2012 #1
    1. The problem statement, all variables and given/known data
    Write a MIPS assembly language program that accomplishes the following tasks:
    1. The program will prompt the user to enter an Integer between 1 and 10.
    If the entered number doesn’t satisfy the above condition, use a loop and prompt the
    user for a new entry (until a valid number is entered)
    2. Depending on the n value implement the following cases:
    case 1: if 1< = n < = 6 compute F(n): if (n=1 or n=2) then F(n) = 10
    else F(n) = 2n*F(n-1);
    Display a result_message together with the numeric value of the result.
    NOTE: use recursive function call.
    case 2: if 7 <= n < 10
    1. Declare an array of 10 elements.
    2. Prompt the user to enter one by one a sequence of 10 characters. For each entered
    character:
    a. Read in the character.
    b. Display the character.
    3. Display the contents of the array in reverse order.
    Your program should be well documented with comments.
    Your console output should include helpful prompts for the user.
    Case 3: if n = 10
    Display a joke.


    2. Relevant equations



    3. The attempt at a solution
    Code (Text):
            .text
            addi $s0, $zero, 0  # register to transfer the user input.
            addi $s1, $zero, 10 # register to hold integer value 10.
            addi $s2, $zero, 2  # register to hold integer value 2.
            addi $s3, $zero, 0  # initialize register for store of multiplication.
            addi $s4, $zero, 0  # initialize register for case1b result.
            addi $t0, $zero, 0  # initialize register for store of lower 32 bits of multiplication.
            addi $t1, $zero, 0  # initialize register for store of lower 32 bits of multiplication  2nd time.
            addi $t9, $zero, 0  # initialize register for array.       
    LOOP0: 
            la $a0, Prompt      # load Prompt string into $a0.
            li $v0, 4       # system call to print Prompt string.
            syscall
       
            li $v0, 5       # system call to read integer from user.
            syscall
       
            bgt $v0, 10, LOOP0  # branch if greater than 10, force user to enter lower number.
            blt $v0, 1, LOOP0   # branch if less than 1, force user to enter a positive number.
            ble $v0, 6, CASE1   # branch to CASE1 if input is less than or equal to value 6.
            blt $v0, 10, CASE2  # branch to CASE2 if input is less than value 10.
            beq $v0, 10, CASE3  # branch to CASE3 if input value is equal to value 10.
       
    CASE1:              # CASE1
            move $s0, $v0       # move the input value from $v0 to $s0.
            ble $s0, 2, CASE1A  # branch to CASE1A if $s0 (the input value) is less than or equal to 2.
            ble $s0, 6, CASE1B  # branch to CASE1B if s$0 is less than or equak to 6.

    CASE1A:             # CASE1A
            la $a0, MSG1        # Load MSG1 string to $a0.
            li $v0, 4       # system call to print MSG1 string.
            syscall
            la $a0, 0($s1)      # Load $s1 to $a0, the value is 10.
            li $v0, 1       # system call to print the integer.
            syscall
            j EXIT          # jump to EXIT.

    CASE1B:
            mult $s0, $s2       # multiply $s0 by $s2.
            mfhi $v0        # move the upper 32 bits to $v0.
            mflo $v1        # move the lower 32 bits to $v1.
            move $t0, $v1       # move $v1 to $t0.
            addi $s3, $s0, -1   # $s3 = $s0 - 1 or n-1.
       
            mult $s3, $s2       # multiply $s3 by $s2 or 2(n-1).
            mfhi $v0        # move the upper 32 bits to $v0.
            mflo $v1        # move the lower 32 bits to $v1.
            move $t1, $v1       # move $v1 to $t1.
       
            mult $t0, $t1       # multiply $t0 by $t1.
            mfhi $v0        # move the upper 32 bits to $v0.
            mflo $v1        # move the lower 32 bits to $v1.
            move $s4, $v1       # move $v1 to $s4
       
            la $a0, MSG1        # Load MSG1 string into $a0.
            li $v0, 4       # system call to print MSG1 string.
            syscall
            la $a0, 0($s4)      # Load $s4 into $a0.
            li $v0, 1       # system call to print integer.
            syscall
            j EXIT          # jump to EXIT.
       
    CASE2:
            la $s7, ARRAY       # Load base address of array into $t9
            li $s6, 36      # Initialize loop counter to 36

    LOOP:  
            la $a0, Prompt2     # Load Prompt2 string into $a0.
            li $v0, 4       # system call to print string Prompt2.
            syscall
       
            li $v0, 12      # read character input.
            syscall
            move $s6($s7), $v0  # move input character to an array element.
       
            la $a0, $s6($s7)    # load array element into $a0.
            li $v0, 11      # system call to print array element.
            syscall
            addi $s6, $s6, -4   # decrement loop counter.
            bgtz $s6, LOOP
       
            la $a0, MSG2        # load MSG2 string into $a0.
            li $v0, 4       # system call to print MSG2 string.
            syscall
    LOOP2:
            la $a0, $s6($s7)    # load element of array into $a0.
            li $v0, 11      # system call to print char.
            addi $s6, $s6, 4    # increment $t8.
            blt $s6, 36, LOOP2  # branch if $t8 is less than 36
            j EXIT          # when $t8 reaches 36 jump to EXIT.
           
    CASE3:
            la $a0, JOKE        # Load JOKE string into $a0.
            li $v0, 4       # system call to print JOKE string.
            syscall
           
    EXIT:               # EXIT
            li $v0, 10      # system call to exit the program.
            syscall
       
        .data
    Prompt: .asciiz "Enter an integer between 1 and 10: "
    MSG1:   .asciiz "\nThe result is: "
    MSG2:   .asciiz "\nThe reverse is: \n"
    Prompt2:.asciiz "\nEnter a character: "
    JOKE:   .asciiz "\nAdam and Eve Virus: Takes a couple of bytes out of your Apple\n"
    ARRAY:  .space 10       # 10 bytes of storage to hold an array of 10 characters
    I got the program to respond to inputs 1,2,10 but 3,4,5,6,7,8,9 aren't producing the outputs that i need.
    I think it might be the part where i move the input into the array. The recursive function part has been difficult for me.
    Any suggestions and help would be greatly appreciated.
     

    Attached Files:

  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted



Similar Discussions: Arrays and recursion in mips assembly language
  1. Assembly Language (Replies: 0)

Loading...