我正在將 C 程式轉換為匯編 MIPS。我在一個 if 陳述句中有多個條件。是if (n == 0 || (n > 0 && n % 10 != 0))。當此條件為真時,它將變數重新分配ans為 0。
這是我到目前為止所做的:
beqz n label1
當我有多種情況時該怎么辦?
uj5u.com熱心網友回復:
陳述句將if執行定向到以下兩個位置之一: THEN 塊的開頭,或 ELSE 塊的開頭(如果沒有 ELSE 塊,則直接在 THEN 塊之后。)
所以我們可以將條件分解如下:
n == 0: 轉到 THEN 塊n <= 0: 轉到 ELSE 塊或 IF 之后n % 10 == 0: 轉到 ELSE 塊或 IF 之后- 轉到 THEN 塊
uj5u.com熱心網友回復:
使用偽代碼,解決方案可能如下所示:
Compare n to zero.
If the comparison result is “equal”, go to TestIsTrue.
If the comparison result is not “greater than”, go to TestIsFalse.
Calculate the remainder of n divided by 10.
Compare the remainder to zero.
If the comparison result is “not equal”, go to TestIsTrue.
Go to TestIsFalse.
TestIsTrue:
Store 0 in ans.
Go to AllDone.
TestIsFalse:
(Put any desired code here. Can be empty.)
AllDone:
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/472524.html
上一篇:裝配-計算算術平均值-分段錯誤
