How to implement Combination and Permutation in RISC-V

38 Views Asked by At

Like the code below, I have problem with how to implement Combination and Permutation in RISC-V

I want the output to be:

s1113310

input number M=4

input number N=2

P(M,N)=12

C(M,N)=6

but instead, the result is:

s1113310

input number M=4

input number N=2

P(M,N)=24

C(M,N)=6

can someone can show me where the code is wrong and how to fix it.

.data
str1: .string "s1113310"
m: .string "\ninput number M="
n: .string "input number N="
p_result: .string "P(M,N)="
c_result: .string "C(M,N)="

.text
main:
    la a0, str1
    li a7, 4
    ecall

    la a0, m
    li a7, 4
    ecall
    li a7, 5
    ecall
    mv s0, a0  # s0 = M

    la a0, n
    li a7, 4
    ecall
    li a7, 5
    ecall
    mv s1, a0  # s1 = N

    mv a0, s0
    jal factorial
    mv s2, a0  # s2 = M!

    sub a0, s0, s1
    jal factorial
    mv s3, a0  # s3 = (M-N)!

    div s4, s2, s3  # s4 = P(M,N) = M! / (M-N)!
    jal pp
    
    mv a0, s1
    jal factorial
    mv s5, a0  # s5 = N!

    mul s6, s3, s5  # s6 = (M-N)! * N!
    div s7, s2, s6  # s7 = C(M,N) = M! / ((M-N)! * N!)
    jal pc

    j end

pp:
    la a0, p_result
    li a7, 4
    ecall
    mv a0, s4
    li a7, 1
    ecall
    li a0, '\n'
    li a7, 11
    ecall
    ret

pc:
    la a0, c_result
    li a7, 4
    ecall
    mv a0, s7
    li a7, 1
    ecall
    li a0, '\n'
    li a7, 11
    ecall
    ret

factorial:
    li t0, 1
    mv t1, a0
loop:
    bge t0, t1, endfactorial
    mul a0, a0, t1
    addi t1, t1, -1
    j loop
endfactorial:
    ret

end:
    li a7, 10
    ecall

Edit: I have solved this issue, here's the code:

.data
str1: .string "s1113310\\n"
m: .string "input number M="
n: .string "input number N="
p_result: .string "P(M,N)="
c_result: .string "C(M,N)=" 
h_result: .string "H(M,N)="
mn_result: .string "M^N="

.text
main:
    la a0, m
    li a7, 4
    ecall
    li a7, 5
    ecall
    mv s0, a0

    # 輸入 n
    la a0, n
    li a7, 4
    ecall
    li a7, 5
    ecall
    mv s1, a0

    li t0,0
    #P(n, k) = n! / (n-k)!
    mv a0, s0
    jal factorial
    mv s2, a0
    sub a0, s0, s1
    jal factorial
    div s2, s2, a0
    mv s3, s2
    jal pp

    #C(n, k) = n! / (k! * (n-k)!)
    mv a0, s1
    jal factorial
    div s3, s3, a0
    jal pc

    #H(M,N) = C(M+N-1,N) = (M+N-1)! / (N! * (M-1)!)
    add t0, s0, s1
    addi t0, t0, -1
    mv a0, t0
    jal factorial
    mv s4, a0
    mv a0, s1
    jal factorial
    mv t1, a0
    mv a0, s0
    addi a0, a0, -1
    jal factorial
    mul t1, t1, a0
    div s4, s4, t1
    jal ph

    #M^N
    mv a0, s0
    mv a1, s1
    jal power
    mv s5, a0
    jal pmn

    j end

pp:
    la a0, p_result
    li a7, 4
    ecall
    mv a0, s3
    li a7, 1
    ecall
    li a0, '\n'
    li a7, 11
    ecall
    ret

pc:
    la a0, c_result
    li a7, 4
    ecall
    mv a0, s3
    li a7, 1
    ecall
    li a0, '\n'
    li a7, 11
    ecall
    ret

ph:
    la a0, h_result
    li a7, 4
    ecall
    mv a0, s4
    li a7, 1
    ecall
    li a0, '\n'
    li a7, 11
    ecall
    ret

pmn:
    la a0, mn_result
    li a7, 4
    ecall
    mv a0, s5
    li a7, 1
    ecall
    li a0, '\n'
    li a7, 11
    ecall
    ret

factorial:
    li t0, 1
    mv t1,a0
    addi t1,t1,-1
loop:
    bge t0, t1, endfactorial
    mul a0, a0, t1
    addi t1, t1, -1
    jal x0, loop
endfactorial:
    jr ra

power:
    li t0, 1
    mv t1, a1
loop_pow:
    beq t1, zero, end_pow
    mul t0, t0, a0
    addi t1, t1, -1
    j loop_pow
end_pow:
    mv a0, t0
    jr ra

end:
    li a7, 10
    ecall

I am still trying to solve H(M,N)

0

There are 0 best solutions below