簡體   English   中英

LMC 輸入三個數 output 從大到小?

[英]Input three numbers into the LMC and output them largest to smallest?

所以,我已經設法解決了大部分問題,但是如果輸入值遞增,我只能從最大到最小 output 嗎? first = 10,second = 20,third = 30。如果值不同,我將如何正確地 output? (例如,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

我建議你為此編寫 C 或偽代碼。 它只會是一兩行左右,這將澄清你的想法。

您的偽/C 代碼將包含 if-then-else 語句。 這是 if-then-else 在高級別 C 中的模式,然后在低級別 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-part。 在執行 then-part 之后,我們需要跳過 else-part ,這就是 if-goto-label 版本中出現無條件分支和endIf1: label 的原因 (如果沒有那個無條件分支,它會在 then 部分之后運行 else 部分,這會很糟糕。)同樣重要的是,在 if-then-else 語句之后,程序運行下一條語句,無論它是否然后部分被解雇或其其他部分被解雇。

如果您有多個 if-then-else 語句,只需為其標簽使用不同的編號即可。 嵌套的 if-then-else 仍應遵循此模式——建議先翻譯外部的,然后再翻譯內部的,但其他順序也可以。 它也可以按程序順序完成,但這使得遵循這些簡單模式變得更加困難。

一些問題:

  • 當你這樣做時:

     LDA second STA third

    ...您丟失了third的原始輸入,因此不可能仍然產生正確的 output。在您執行STA的其他地方也會發生同樣的情況。 相反,您可以考慮交換值。 通常你需要一個臨時變量(一個額外的DAT )。 例如,如果你想交換secondthird ,那么:

     LDA second STA temp LDA third STA second LDA temp STA third
  • 您從firstthird之間的比較開始,當您檢測到first大於third時,您將second復制到third 奇怪的是你決定在這一點上涉及second (你沒有得出任何結論)。 first做點什么會更有意義。 如果您將firstthird交換,那么您將得到first現在保證不大於third的“不變量”,無論您是分支branch1還是從上一條指令到達 label。 這是對輸入進行排序的第一步。

  • 如果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/lmc@v0.816/lmc.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM