Академический Документы
Профессиональный Документы
Культура Документы
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
while2:
bgt $s5, $s3, exit2
mul $s7, $s5, $s7
addi $s5, $s5, 1
j while2
exit2:
div $s0, $s6, $s0
div $s2, $s7, $s2
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!