Question

In: Computer Science

Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...

Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the following sets of values:

18 6
16 5
5 11
21 4
2 16
17 2
0 10
10 0
20 5
6 6
0 0

Hand in the program with output.

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for Question . Be safe .

Note : check attached image for output ,code compiled and tested in MARS MIPS simulator .

Program Plan :

  1. read two number input $s0= first number $s1=second number
  2. now if second number is zero jump to dividebyzero label and check first number is also zero than exit program .
  3. if not print divide by zero message and again ask for user input .
  4. if all value are value for multipication add first number to first number second number times
  5. for divide subtract second number from first number till first number number not zero or less than second number .
  6. code fully commented

Program :

.data #variable section

   #string declaration to print and prompt user
   number1 : .asciiz "\nEnter first non negative number : "
   number2 : .asciiz "Enter second non negative number : "
   byZero : .asciiz "divide by zero error.\n"
   multiply : .asciiz "multipication is : "
   divide : .asciiz "\ndivide is : "
   done : .asciiz "Good bye!"
  
  

.text #data section
   main:
       #load address of number 2 in $a0 to print string   
       la $a0,number1
       li $v0,4 #syscall 4 to print string
       syscall
      
       li $v0,5 #syscall 5 to read integer from user
       syscall
      
       addi $s0,$v0,0 #move user input from $v0 to $s0 = $s0=$v0+0
      
       #load address of number 2 in $a0 to print string
       la $a0,number2
       li $v0,4 #syscall 4 to print string
       syscall
      
       li $v0,5 #syscall 5 to read input from user
       syscall
      
       addi $s1,$v0,0 #move user input from $v0 to $s1 = $s1=$v0+0      
      
       beq $s1,0,divideByZero #if s1==0 go to divideByZero s1 = secondnumber
      
       li $t0,1 #t0=1
       addi $t1,$s0,0 #t1=$s0(first number )
   loop1: #loop1 label
       bge $t0,$s1,printMultiply #if $t0>=$s1 go to printMultiply label
      
       add $t1,$t1,$s0 #add first number to $t1 = $t1+$s0
      
      
       addi $t0,$t0,1 #increment loop count
       j loop1 #jump to loop1
   #print multiply
   printMultiply:
           #print multiply string
           la $a0,multiply
           li $v0,4 #syscall 4
           syscall
          
           #move multipication value from $t1 to $a0
           addi $a0,$t1,0
           li $v0,1 #syscall 1 to print integer
           syscall
          
          
       li $t0,0 #t0=0
       addi $t1,$s0,0 #$t1=$s0 (first number )
      
          
   loop2:   #loop2 label
       blt $t1,$s1,printDivide #if $t1<$s1 go to printDivide label
       sub $t1,$t1,$s1 #subtract $t1=$t1-s1
       addi $t0,$t0,1 #increment quotient
       j loop2
  
   printDivide: #print divide label
           #print divide string
           la $a0,divide
           li $v0,4 #syscall 4
           syscall
          
           #move quotient value from $t0 to $a0
           addi $a0,$t0,0
           li $v0,1 #syscall 1 to print integer
           syscall
      
       j main #jump to main again
  
  

      
  
   divideByZero: #divide by zero
       #we already know second number is zero
       #if first number is also zero exit program and print good bye message
       beq $s0,0,exit
       #if first number not zero but second number is zero print divide by Zero message
       la $a0,byZero #load address of byZero string in $a0
       li $v0,4 #syscall 4 to print string
       syscall
      
       j main #again jump to main label to get input
  
      
  
   exit:#exit label
       la $a0,done #print good bye message
       li $v0,4 #syscall 4 to print string
       syscall
      
       li $v0,10 #syscall 10 to terminate program
       syscall #syscall call

Output :

Please up vote ,comment if any query .


Related Solutions

Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product...
Write a MIPS assembly program to repeatedly read two non-negative integers and print the integer product and quotient without using the multiplication and division instructions. Each input number will be entered on a separate line. Your program will terminate when two zeroes are entered by the user. If only the second number in a pair is zero, then a divide-by-zero error message should be printed for the division operation. Use comments generously in your program. Test your program with the...
Use MIPS assembly language program to swap two of the integers in an integer array. The...
Use MIPS assembly language program to swap two of the integers in an integer array. The program should include the Swap function to swap the integers and the main function to call the Swap function. The main function should: • Pass the starting address of the array in $a0. • Pass the indices of the two elements to swap in $a1 and $a2. • Preserve (i.e. push onto the stack) any T registers that it uses. • Call the Swap...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a...
Write a MIPS assembly language program to read an arbitrary number of integer pairs from a file. Each pair will then be multiplied together with the results being accumulated (added together) forming a sum-of-products operation. Submit your report and code here.
Write a mips assembly language program to ask the user to enter two integers A and...
Write a mips assembly language program to ask the user to enter two integers A and B and then display the result of computing the expression: A + 2B - 5.
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays the sum and the difference. In the .data section, define two variables num1 and num2 both words. Initialize num1 to 92413 10 and num2 to D4B 16 (use 0xD4B to initialize, Note that D4B is a hexadecimal number). Your main procedure/function should load the values of num1 and num2 into two temporary registers, and display them on the console window. Then add the values...
Write a MIPS assembly program that prompts the user for some number of cents (integer) and...
Write a MIPS assembly program that prompts the user for some number of cents (integer) and read the user input. Then translate that number of into a number of quarters, dimes, nickels and pennies (all integers) equal to that amount and outputs the result. The output should adequately tell the user what is being output (not just the numeric results).
Write a mips assembly code program that ask the user to enter an integer value, and...
Write a mips assembly code program that ask the user to enter an integer value, and then print the result of doubling that number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT