How to Store User Input as Memory in MIPS Assembly

Click For Summary
The discussion focuses on creating a MIPS assembly routine to accept two integers, calculate their quotient and remainder, and generate a formatted output string. Participants highlight the need to store the integers properly in memory, suggesting using `.word` for integer storage instead of `.asciiz` for strings. There is confusion regarding how to incorporate the input integers into the output string, with suggestions to use multiple string variables for formatting. The main issue raised is that the program prints prompts before allowing user input, indicating a need for proper sequencing of system calls. Overall, the conversation emphasizes the importance of correctly managing data types and the sequence of operations in MIPS assembly programming.
lina29
Messages
84
Reaction score
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
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.
 
# 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
 
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.
 
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?
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
21K
  • · Replies 12 ·
Replies
12
Views
11K
  • · Replies 3 ·
Replies
3
Views
13K
  • · Replies 1 ·
Replies
1
Views
3K