Вы находитесь на странице: 1из 2

.

data
prompt0: .asciiz "Enter the count of the pool: "
prompt1: .asciiz "Enter the size of the pool: "
prompt2: .asciiz "Enter the count for the second pool: "
prompt3: .asciiz "Enter the size for the second pool: "
message: .asciiz "The odds are 1 in "
stopped: .asciiz "\nThe program has stopped."
number1: .word 1
.text
main:
#prompt the count of the first pool
li $v0, 4
la $a0, prompt0
syscall
li $v0, 5
syscall
move $s0, $v0
#prompt the user for the size of the pool
li $v0, 4
la $a0, prompt1
syscall
li $v0, 5
syscall
move $s1, $v0
#prompt the user and ask for the count of second pool
li $v0, 4
la $a0, prompt2
syscall
li $v0, 5
syscall
move $s2, $v0
#prompt the user and ask for the size of second pool
li $v0, 4
la $a0, prompt3
syscall
li $v0, 5
syscall
move $s3, $v0
sub $s4, $s1, $s0
addi $s4, $s4, 1
sub $s5, $s3, $s2
addi $s5, $s5, 1

#calculating factorial for k in n!/((k!*((n-k)!)


move $a0, $s0
jal factrl
move $s0, $v0
move $a0, $s2
jal factrl
move $s2, $v0

#setting value 1 so I can store the multiplication here


lw $s6, number1($zero)
lw $s7, number1($zero)
while:
bgt $s4, $s1, exit
mul $s6, $s4, $s6
addi $s4, $s4, 1
j while
exit:

while2:
bgt $s5, $s3, exit2
mul $s7, $s5, $s7
addi $s5, $s5, 1
j while2
exit2:
div $s0, $s6, $s0
div $s2, $s7, $s2

mul $s0, $s0, $s2


#Print the message part
li $v0, 4
la $a0, message
syscall

#Print the odds


li $v0, 1
move $a0, $s0
syscall
#Print the stop part
li $v0, 4
la $a0, stopped
syscall
#Ending the program
li $v0, 10
syscall

factrl:
sw $ra, 4($sp) # save the return address
sw $a0, 0($sp) # save the current value of n
addi $sp, $sp, -8 # move stack pointer
slti $t0, $a0, 2 # save 1 iteration, n=0 or n=1; n!=1
beq $t0, $zero, L1 # not less than 2, calculate n(n-1)!
addi $v0, $zero, 1 # n=1; n!=1
jr $ra # now multiply
L1:
addi $a0, $a0, -1 # n = n-1
jal factrl # now (n-1)!
addi $sp, $sp, 8 # reset the stack pointer
lw $a0, 0($sp) # fetch saved (n-1)
lw $ra, 4($sp) # fetch return address
mul $v0, $a0, $v0 # multiply (n)*(n-1)
jr $ra # return value n!

Вам также может понравиться