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!

MIPS user input as output

  1. Apr 13, 2012 #1
    1. The problem statement, all variables and given/known data
    Write a routine that accepts two integers i and j as arguments, calculates the quotient and remainder of i and j, and generates a string in memory that stores "The quotient of i and j is i/j and the remainder is i%j". For example if the arguments was 15 and 7 the string in memory would be "The quotient of 15 and 7 is 2 and the remainder is 1". The address of the string is returned as the 3rd argument of your routine. Write a main program that calls your routine, test run it using MARS and show the output.


    2. Relevant equations



    3. The attempt at a solution

    # division.asm

    .data
    intI: .asciiz "\nEnter i: "
    intJ: .asciiz "\nEnter j: "
    quotient: .asciiz "\nThe quotient is = "
    remainder: .asciiz "\n\nThe remainder is = "
    .text

    main: li $v0, 4 # system call code for print_string
    la $a0, intI # address of intI
    syscall # print intI

    #get the first number from user, put it into $s0

    li $v0, 5 # system call code for read_int
    syscall # read an integer into $v0 from console
    add $s0, $v0, $zero # copy $v0 into $s0


    #read print_string for str2
    li $v0, 4 # system call code for print_string
    la $a0, intJ # address of intJ
    syscall # print str1

    # get second number from user, put it into $t1
    li $v0, 5 #load syscall for read_int
    syscall #make the syscall
    move $s1, $v0 #move the number read into $s1


    div $s0, $s1 #diving $s0 by $s1
    mflo $t0 #storing value of lo(quotient) in
    #register $t0
    mfhi $t1 #storing value of hi(remainder) in
    #register $t1

    mult $s0, $s1
    mflo $t2


    li $v0, 4 # Print string
    la $a0, quotient # address of quotient
    syscall

    #print a/b
    li $v0, 1 #load syscall print_int into $v0
    move $a0, $t0 #move the number to print into $t2
    syscall

    # read print string for remainder
    li $v0, 4
    la $a0, remainder
    syscall

    # print remainder
    li $v0, 1
    move $a0, $t1
    syscall

    #end of program
    li $v0, 10 #system call code for exit
    syscall


    What I'm having a problem with is the output. I'm not sure how I'm supposed to do "For example if the arguments was 15 and 7 the string in memory would be 'The quotient of 15 and 7 is 2 and the remainder is 1' "

    Also am I storing the numbers as strings not integers?
     
  2. jcsd
  3. Apr 13, 2012 #2

    Mark44

    Staff: Mentor

    I wouldn't store the numbers as strings.

    To get the output you need you'll need to display several strings and four numbers.

    str1 - "'The quotient of "
    str2 - " and "
    str3 - " is "
    str4 - " and the remainder is "
    str5 - "."

    num1 - 15
    num2 - 7
    num3 - 2 (your program calculates this)
    num4 - 1 (and this)

    Use the appropriate syscall for printing a string to display each of the five strings in the right order, with syscalls in the right places for printing an int.
     
  4. Apr 13, 2012 #3
    # division.asm

    .data
    intI: .asciiz "\nEnter i: "
    intJ: .asciiz "\nEnter j: "
    str1: .asciiz "\nThe quotient of "
    str2: .asciiz "and "
    str3: .asciiz "is "
    str4: .asciiz " and the remainder is "
    str5: .asciiz "."
    .text

    main: li $v0, 4 # system call code for print_string
    la $a0, intI # address of intI
    syscall # print intI

    #get the first number from user, put it into $s0

    li $v0, 5 # system call code for read_int
    syscall # read an integer into $v0 from console
    add $s0, $v0, $zero # copy $v0 into $s0


    #read print_string for intJ
    li $v0, 4 # system call code for print_string
    la $a0, intJ # address of intJ
    syscall # print str1

    # get second number from user, put it into $t1
    li $v0, 5 #load syscall for read_int
    syscall #make the syscall
    move $s1, $v0 #move the number read into $s1


    div $s0, $s1 #diving $s0 by $s1
    mflo $t0 #storing value of lo(quotient) in
    #register $t0
    mfhi $t1 #storing value of hi(remainder) in
    #register $t1

    mult $s0, $s1
    mflo $t2





    li $v0, 1 #load syscall print_int into $v0
    move $a0, $t0 #move the number to print into $t2
    syscall

    # read print string for remainder
    li $v0, 4
    la $a0, str1
    syscall

    li $v0, 4
    la $a0, str2
    syscall

    li $v0, 4
    la $a0, str3
    syscall

    # print quotient
    li $v0, 1
    move $a0, $t0
    syscall

    li $v0, 4
    la $a0, str4
    syscall

    # print remainder
    li $v0, 1
    move $a0, $t1
    syscall

    li $v0, 4
    la $a0, str5
    syscall



    #end of program
    li $v0, 10 #system call code for exit
    syscall

    I figured out how to put the remainder and quotient into the string. I'm still confused on how I would add i or j into the string. Are my numbers stored as strings? Based on the prompt I though thats what it was asking
     
  5. Apr 13, 2012 #4

    Mark44

    Staff: Mentor

    You need to store the two input integers in memory. Your data segment is somewhat confusing as you have intI and intJ as variables. Their names would suggest that they are to be used to store integer values. Instead, you have strings stored in them.

    I would set things up more like this:
    Code (Text):

    .data
    intI: .word 0
    intJ: .word 0
    prompt1: .ascii "\nEnter i: "
    prompt2: .ascii "\nEnter j: "
    str1: ...
    etc.
     
     
  6. Apr 14, 2012 #5
    To be honest I'm still having trouble

    Code (Text):


    # division.asm

      .data
    intI: .word 0
    intJ: .word 0
    prompt1: .ascii "\nEnter i: "
    prompt2: .ascii "\nEnter j: "
    str1: .asciiz "\nThe quotient of  "
    str2: .asciiz "and "
    str3: .asciiz "is "
    str4: .asciiz " and the remainder is "
    str5: .asciiz "."
      .text

    main: li   $v0, 4      # system call code for print_string
      la   $a0, prompt1      # address of intI
      #la $a1, prompt1 #gets the length of the space in $a1 so we can't go over the memory limit
      syscall                # print intI

    #get the first number from user, put it into $s0

    li   $v0, 5            # system call code for read_int
      syscall                # read an integer into $v0 from console
      add  $s0, $v0, $zero   # copy $v0 into $s0
     
    li   $v0, 4      # system call code for print_string
      la   $a0, intI     # address of intI
      #la $a1, prompt1 #gets the length of the space in $a1 so we can't go over the memory limit
      syscall  
           




    #read print_string for intJ
    li   $v0, 4            # system call code for print_string
      la   $a0, prompt2        # address of intJ
      syscall                # print str1

    # get second number from user, put it into $t1  
    li  $v0, 5      #load syscall for read_int
    syscall         #make the syscall
    move $s1, $v0       #move the number read into $s1


    div $s0, $s1        #diving $s0 by $s1
    mflo    $t0         #storing value of lo(quotient) in
                    #register $t0
    mfhi    $t1         #storing value of hi(remainder) in
                    #register $t1

    mult $s0, $s1
    mflo $t2


                   


    #li  $v0, 1      #load syscall print_int into $v0
    #move $a0, $t0   #move the number to print into $t2
    #syscall

    # read print string for remainder
    li $v0, 4
    la $a0, str1
    syscall

    li $v0, 4
    la $a0, str2
    syscall



    li $v0, 4
    la $a0, str3
    syscall

    # print quotient
    li $v0, 1
    move $a0, $t0
    syscall

    li $v0, 4
    la $a0, str4
    syscall

    # print remainder
    li $v0, 1
    move $a0, $t1
    syscall

    li $v0, 4
    la $a0, str5
    syscall



    #end of program
    li  $v0, 10     #system call code for exit
    syscall
     
    Right now my code is printing out

    Enter i:
    Enter j:
    The quotient of

    before it even lets me enter the first integer. I know I need to store the two integers as memory I'm just completely lost on how to approach it. Is there any specific command I should use?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: MIPS user input as output
  1. Input-output relation (Replies: 0)

  2. Input/Output Impedance (Replies: 15)

Loading...