MIPS user input as output

  • Thread starter lina29
  • Start date
  • #1
85
0

Homework Statement


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.


Homework Equations





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?
 

Answers and Replies

  • #2
34,977
6,731

Homework Statement


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.


Homework Equations





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?

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.
 
  • #3
85
0
# 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
 
  • #4
34,977
6,731
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:
.data
intI: .word 0
intJ: .word 0
prompt1: .ascii "\nEnter i: "
prompt2: .ascii "\nEnter j: "
str1: ...
etc.
 
  • #5
85
0
To be honest I'm still having trouble

Code:
# 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?
 

Related Threads on MIPS user input as output

Replies
19
Views
2K
  • Last Post
Replies
15
Views
2K
  • Last Post
Replies
0
Views
1K
  • Last Post
Replies
14
Views
13K
Replies
7
Views
2K
Replies
5
Views
6K
  • Last Post
Replies
2
Views
802
  • Last Post
Replies
6
Views
6K
Replies
6
Views
1K
Top