How to Store User Input as Memory in MIPS Assembly

In summary, this program takes two integers, i and j, as input and divides them to calculate the quotient and remainder. It then generates a string in memory that stores "The quotient of i and j is i/j and the remainder is i%j". This string is returned as the 3rd argument of the routine. A main program is also included to call the routine and test it using MARS.
  • #1
lina29
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 $s1div $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 $t2li $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
syscallWhat 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?
 
Physics news on Phys.org
  • #2
lina29 said:

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
# 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 $s1div $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 that's what it was asking
 
  • #4
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
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 $s1div $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 let's 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?
 

1. How can I get user input in MIPS?

To get user input in MIPS, you can use the syscall service call with the appropriate system call number (5 for integer input, 8 for string input). This will prompt the user to enter their input, which can then be stored in a register or memory location for later use.

2. How do I display user input as output in MIPS?

To display user input as output in MIPS, you can use the syscall service call with system call number 1 (for integer output) or system call number 4 (for string output). Make sure to store the user input in a register or memory location before using the syscall to display it.

3. Can I use MIPS to get input from multiple users?

Yes, MIPS can be used to get input from multiple users. You can use a loop to prompt each user for input and store their responses in separate registers or memory locations for later use.

4. Is there a limit to the amount of user input that can be processed in MIPS?

Yes, there is a limit to the amount of user input that can be processed in MIPS. This limit depends on the memory size available for the program and the data type used to store the input. It is important to allocate enough memory for the input and to check for potential overflow errors when processing the input.

5. How can I handle errors or invalid input when using user input as output in MIPS?

You can handle errors or invalid input when using user input as output in MIPS by implementing error-checking code. This can involve checking for invalid data types or range errors before processing the input. You can also use conditional statements to handle specific errors and provide appropriate output or prompts to the user.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
20K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
10K
Back
Top