2012-12-20 20 views
3

我正在爲ASM中的68k處理器編寫一個程序。如何在68k語言中創建if-else控制結構?

,我需要做類似的東西

if (D0 > D1) { 
    do_some_stuff(); 
    do_some_stuff(); 
    do_some_stuff(); 
    do_some_stuff(); 
} else { 
    do_some_stuff(); 
    do_some_stuff(); 
    do_some_stuff(); 
    do_some_stuff(); 
} 

但問題是,它只是讓我任一分支的一些指針或繼續執行。

像這樣:

CMP.L D0,D1  ; compare 
BNE AGAIN  ; move to a pointer 

是什麼力量讓這種結構如上最簡單的方法?

回答

2

你可以嘗試這樣的事:

if (D0>D1) { 
    //do_some_stuff 
} else { 
    //do_some_other_stuff 
} 

應該是:

CMP.L D0,D1  ; compare 
BGT AGAIN 
//do_some_other_stuff 
BR DONE 
AGAIN: 
//do_some_stuff 
DONE: 

瞭解更多關於conditional branching

+0

嗯。還有一些增加 - 它只比較D0 == D1,是否可以直接比較「D0> D1」或「D0 NewProger

+0

@NewProger'BGT'分支如果大於,請看鏈接 – iabdalkader

+0

啊,好的。謝謝:) – NewProger

1

控制結構是最適合這個場景是BGT

BGT分支大於)將在第二個操作數大於第一個操作數時分支。當你看到幕後發生的事情時,這是有道理的。

CMP.W D1,D0 ;Performs D0-D1, sets the CCR, discards the result 

它設置CCR(條件碼寄存器),因爲該分支被採用,只有當(N = 1和V = 1和Z = 0)(N = 0和V = 0和Z = 0)

我們變換:

if (D0 > D1) { 
    //do_some_stuff 
} else { 
    //do_some_other_stuff 
} 

進入68K彙編語言:

 CMP.W D1,D0  ;compare the contents of D0 and D1 
    BGT  IF 
    // do_some_other_stuff 
    BRA  EXIT 
IF 
    // do_some_stuff 
EXIT ...