所以,我已經設法解決了大部分問題,但是如果輸入值是遞增的,我只能從最大到最小輸出?第一個 = 10,第二個 = 20,第三個 = 30。如果值不同,我將如何正確輸出?(比如說,5,1,3?例如)
這是我到目前為止:
INP
STA first
INP
STA second
INP
STA third
SUB first
BRP branch1
LDA second
STA third
branch1 LDA third
SUB second
BRP branch2
LDA first
STA third
branch2 LDA third
OUT
LDA first
SUB second
BRP branch3
LDA second
OUT
branch3 LDA first
OUT
HLT
first DAT
second DAT
third DAT
uj5u.com熱心網友回復:
我建議你為此撰寫 C 或偽代碼。它只會是一兩行左右,這將澄清你的想法。
您的偽/C 代碼將包含 if-then-else 陳述句。這是高級 C 中的 if-then-else 模式,然后在低級 C 中重復。當您翻譯 if-then-else 陳述句時,請遵循以下模式:
if ( <condition> ) {
<then-part>
}
else {
<else-part>
}
請注意,只有 then-part 和 else-part 之一應該運行。
同樣的控制結構,if-then-else,在匯編語言的 if-goto-label 風格中:
if <condition> is false goto elsePart1;
<then-part>
goto endIf1;
elsePart1:
<else-part>
endIf1:
讓我們首先注意標簽不會執行——處理器看不到它們,因為它們在生成機器代碼時被匯編程式洗掉了。處理器只看到并執行機器碼指令,標簽沒有機器碼。
下面是 if-then-else 在 if-goto-label 中的作業方式:當條件為假時,它將跳過 then-part 來運行 else-part。但在條件為真時,它不會分支,因此執行 then 部分。 執行 then-part 后,我??們需要跳過 else-part,這就是endIf1:if-goto-label version 中無條件分支和標簽的原因。(如果沒有那個無條件分支,它會在 then 部分之后運行 else 部分,這會很糟糕。)同樣重要的是,在 if-then-else 陳述句之后,程式運行下一條陳述句,不管它是否then-part 被解雇或它的 else-part 被解雇。
如果您有多個 if-then-else 陳述句,只需為其標簽使用不同的編號。嵌套的 if-then-else 仍應遵循此模式 - 建議先翻譯外部的,然后是內部的,但其他順序也可以。它也可以按程式順序完成,但這使得遵循這些簡單的模式變得更加困難。
uj5u.com熱心網友回復:
一些問題:
當你這樣做時:
LDA second STA third...您丟失了 的原始輸入
third,因此不可能仍然產生正確的輸出。在您執行的其他地方也會發生同樣的情況STA。相反,您可以考慮交換值。通常,您需要一個臨時變數(附加DAT)。舉例來說,如果你想換second用third,則:LDA second STA temp LDA third STA second LDA temp STA third當您檢測到大于復制到時,您首先比較
first和。奇怪的是你決定在這一點上參與(你沒有得出任何結論)。用 做一些事情會更有意義。如果您將與交換,那么您將獲得現在保證不大于的“不變數” ,無論您是從前一條指令分支到還是到達該標簽。這是對輸入進行排序的第一步。thirdfirstthirdsecondthirdsecondfirstfirstthirdfirstthirdbranch1如果
BRP branch3導致跳轉到branch3您,最終將只有 2 個輸出而不是 3 個OUT。不應跳過三個指令中的任何一個。否則,您應該添加更多OUT指令,以便在每個執行路徑中始終執行 3個指令。
這是更正后的腳本,您可以在此處運行:
#input: 3 1 2
INP
STA first
INP
STA second
INP
STA third
SUB first
BRP branch1
LDA first # don't deal with second, but with first
STA temp # use temp to perform swap first <--> third
LDA third
STA first
LDA temp
STA third
branch1 LDA third # at this point we know first <= third
SUB second
BRP branch2
LDA second # don't deal with first, but with second
STA temp # use temp to perform swap second <--> third
LDA third
STA second
LDA temp
STA third
branch2 LDA third # at this point we know first <= third && second <= third
OUT
LDA first
SUB second
BRP branch3
LDA second
OUT
LDA first # We cannot use branch3 here, as there we still output second
OUT
HLT
branch3 LDA first
OUT
LDA second
OUT
HLT
first DAT
second DAT
third DAT
temp DAT
<script src="https://cdn.jsdelivr.net/gh/trincot/[email protected]/lmc.js"></script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315395.html
下一篇:遞回斐波那契臂組裝
