2015-02-06 71 views
2

我開始在ARM程序集,我一直在嘗試寫一個簡單的整數除法子程序。到目前爲止,我有以下幾點:ARM整數除法算法

.text 
start: 
    mov r0, #25 
    mov r1, #5 
    bl divide 
    b stop 
divide: 
    cmp r0, r1 
    it lo 
    mov pc, lr 
    sub r0, r0, r1 
    add r2, r2, #1 
    b divide 
stop: 
    b stop 

我寫它的基礎上,我想出了該算法的僞代碼:

Is the Divisor (bottom) larger than the Dividend (top)? 
Yes: 
    -Return the remainder and the counter(quotient) 
No: 
    -Subtract the Divisor from the Dividend 
    -Increment the counter by 1 
    -Repeat the method 

r0包含分子和R1的分母。 算法結束時,r0應包含餘數,r2應包含商。然而,在運行時,r0包含19並且r2包含0.

我的邏輯中是否有任何謬論我只是失蹤?

+1

這是組裝到ARM還是Thumb?無論哪種方式,我很驚訝它甚至得到了這麼多 - 如果在「it」指令之後的指令沒有相應的條件代碼(即使「it」本身不能彙編成ARM中的任何東西州)。想想'mov pc,lr'無條件執行時會發生什麼(我的精神力量告訴我你的模擬器/調試器以十六進制顯示寄存器)。 – Notlikethat 2015-02-07 00:23:33

+0

謝謝。我用movlo替換了mov,它工作正常。 – watswat5 2015-02-07 00:36:14

回答

1

我刪除了it lo並將mov更改爲movlo,它工作正常。