我编写的这个程序在MARS上完美运行,但是当我使用QTSIMS加载它时,它会停止工作并为每次计算返回零,有人可以告诉我为什么吗?
我不确定两者之间的区别是否诚实。
.globl main
主:
.text
li $v0, 4 # print string
la $a0, large # load string
syscall
li $v0, 5 # prompt for integer
syscall
move $s0, $v0 # store input
#Below is the same but different strings and inputs
li $v0, 4
la $a0, largepicked
syscall
li $v0, 5
syscall
move $s1, $v0
li $v0, 4
la $a0, small
syscall
li $v0, 5
syscall
move $s2, $v0
li $v0, 4
la $a0, smallpicked
syscall
li $v0, 5
syscall
move $s3, $v0
move $a0, $s0 # save large value to function
move $a1, $s1 # save small value to function
jal factmod # compute factorial of (large!/small!)
move $s4, $v0 # store value
move $a0, $s1 # save small value to original factorial
jal factrl # compute factorial
move $s5, $v0 # store value
#Repeat for second lottery
move $a0, $s2
move $a1, $s3
jal factmod
move $s6, $v0
move $a0, $s3
jal factrl
move $s7, $v0
div $s4, $s5
mflo $s4
div $s6, $s7
mflo $s6
mul $s4, $s4, $s6
li $v0, 4
la $a0, result
syscall
li $v0, 1
move $a0, $s4
syscall
li $v0, 4
la $a0, stopped
syscall
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!
大的阶乘!/小!
factmod: sw $ra, 8($sp) # save the return address
sw $a0, 4($sp) # save the value of large
sw $a1, 0($sp) # save the value of small
addi $t0, $zero, 1 # set counter
lw $t1, 4($sp) # set large
lw $t2, 0($sp) # set small
move $v0, $t1 # set large as the numerator
loop: addi $t0, $t0, 1 # increment counter
addi $t1, $t1, -1 # decrement large
mul $v0, $t1, $v0 # large * large - 1
bge $t0, $t2, done # if counter less than small, loop
j loop
done: lw $ra, 8($sp) # fetch return address
jr $ra # return value
。数据
large: .asciiz "Please enter the number of balls in the large pool: "
largepicked: .asciiz "Please enter the number of balls picked from the large pool: "
small: .asciiz "Please enter the number of balls in the second pool: "
smallpicked: .asciiz "Please enter the number of balls picked from the second pool: "
result: .asciiz "The odds are 1 in "
stopped: .asciiz "\nStopped"
答案 0 :(得分:0)
没关系,我用另一台电脑运行QtSpim并且它有效