#Copyright 2021 by John Bowman #Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), #to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, #and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: #The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, #WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #This program performs multiplication by implementing the Russian peasant's algorithm #Developed using MARS 4.5 MIPS Assembler and Runtime Simulator .data lblFirst: .asciiz "Enter a number: " lblSecond: .asciiz "Enter another number: " lblResult: .asciiz "The product of the two numbers is: " .text #Ask for first number li $v0, 4 la $a0, lblFirst syscall #Receive first number from console li $v0, 5 syscall #Fetch return value and store in $t0 move $t0, $v0 #Ask for second number li $v0, 4 la $a0, lblSecond syscall #Receive second number from console li $v0, 5 syscall #Fetch return value and store in $t1 move $t1, $v0 #Initialize variables for branching tests addi $t2,$zero,2 addi $t5,$zero,1 loop: beqz $t0, exit #When $t0 equals zero, terminate loop divu $t0,$t2 mfhi $t3 #Fetch remainder beq $t3, $t5, incrementProduct #increment product if remainder is equal to 1 continueLoop: sll $t1,$t1,1 #Double $t1 by two srl $t0,$t0,1 #Divide $t2 by two j loop incrementProduct: add $t4, $t4, $t1 j continueLoop exit: #Print result description li $v0, 4 la $a0, lblResult syscall #Print result li $v0, 1 la $a0, ($t4) syscall #Terminate program li $v0, 10 syscall