[問題]mips combination(C幾取幾)的程式問題

看板Programming作者 (shivako)時間18年前 (2007/11/26 09:46), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
我之前用遞迴的方式去寫 不過速度實在是不夠快 後來就把腦筋動到乘法除法上面 想法是:假設要從X之中取Y個數(ex:從五之中挑三個數) 我就先檢查是否 3*2 -1 < 5 因為不是 我就把它換乘從五之中取 (5-3) = 2個數 然後用陣列把X 到 X-Y+1的數(這題是5,4)存進去 接下來依序把陣列中的X值讀出來看是否可以被Y整除 可以就把除過的商存回陣列 拿Y-1去重新再 進行一次 一直做到Y=2也結束之後 把陣列中的所有X值全部相乘 以下是寫出來的code: .data X: .word, 0 Y: .word, 0 str: .asciiz "please type two integers, and each with the enter key\n" str2: .asciiz " is the combination results.\n" strt: .asciiz "got a problen\n" Calc: .space 64 .text .globl main main: #print string li $v0, 4 la $a0, str syscall #read X Y li $v0, 5 syscall sw $v0, X li $v0, 5 syscall sw $v0, Y #start to calc lw $s0,X #s0 = x lw $s1,Y la $s2,Calc step1: #test if x<y slt $t0,$s0,$s1 bne $t0,$zero,change j step2 step2: #test if y*2-1 is bigger than x or not add $t1,$s1,$s1 addi $t1,$t1,-1 slt $t0,$t1,$s0 beq $t0,$zero,minus add $t2,$s0,$zero #X add $t3,$s1,$zero #Y add $t4,$s2,$zero #arrayadress add $s3,$zero,$zero #arraysize step3: #create an array to store sw $t2,0($t4) addi $t2,$t2,-1 addi $t3,$t3,-1 addi $t4,$t4,4 addi $s3,$s3,1 syscall add $t4,$s2,$zero add $t3,$s1,$zero step4: #start to divide lw $t2,0($t4) rem $t0,$t2,$t3 #calc t0 = t2$t3 beq $t0,$zero,divide addi $t4,$t4,4 j step4 step5: #get ready to calc addi $s3,$zero,1 step6: #calc lw $t2,0($s2) mul $s3,$s3,$t2 addi $s2,$s2,4 addi $s3,$s3,-1 bne $s3,$zero,step6 step7: #output li $v0,1 move $a0,$s3 syscall li $v0,4 la $a0,str2 syscall li $v0,10 syscall change: #X,Y中較大的 為從中選擇的 add $t5,$s0,$zero add $s0,$s1,$zero add $s1,$t5,$zero j step2 minus: #如果是C7取4這種 換成C7取3 addi $s3,$zero,1 beq $s0,$s1,step7 #if X=Y answer would be 1 sub $t5,$s0,$s1 add $s1,$t5,$zero j step3 divide: #得到商數 存回陣列 檢查Y=2是否已經做完 addi $t1,$zero,1 div $t0,$t2,$t3 sw $t0,0($t4) addi $t3,$t3,-1 beq $t3,1,step5 add $t4,$s2,$zero j step4 可是連一開始都會有問題 可以告訴我是哪裏寫錯嗎 感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.106.60
文章代碼(AID): #17IYM1OO (Programming)
文章代碼(AID): #17IYM1OO (Programming)