簡體   English   中英

小人電腦-最高數字顯示

[英]Little Man Computer - highest number display

我需要編寫一個程序來計算最高數字,並在輸入為 0 時將其顯示在 output 中。

這是我到目前為止所做的:

start INP
      STO max
      BRZ end
      STO temp
      SUB max
end   LDA max
------------
      OUT
      HLT

input DAT
max   DAT

我假設您需要獲得多個輸入,直到輸入為 0,然后 output 必須是這些輸入中的最高數字。

您的代碼中存在一些問題:

  • 有一個STO temp ,但temp不是您定義的 label。
  • 有一個 label input ,但從未使用過。
  • 沒有循環:用戶只能輸入一個數字。 你需要一個BRA start某個地方開始到 go 回到INP指令並詢問第二個數字,......等等,直到BRZ執行 go 到最后。
  • 輸入后,您的代碼將輸入值存儲在max中,但只有在確定它確實是最大數字時才會發生這種情況。 所以這不應該總是發生。 首先將max初始化為可能的最小數字 (0),然后使用SUB檢查當前輸入的數字是否應存儲在max中。
  • 當數字大於當前最大值或不是時,您的代碼沒有提供不同的處理。 您需要在SUB之后使用BRP來進行區分,當減法不是負數時,就可以首先取回輸入的值,然后將其存儲在max中。

這是一個更正的程序。 你可以在這里運行它。

 #input:2 5 1 9 4 3 7 0 LDA zero # initialise max to zero update STO max start INP STO input # use correct label BRZ end SUB max BRP ismax # distinction BRA start # repeat the input cycle ismax LDA input # now we know it is the current maximum BRA update # update max and repeat the input cycle end LDA max OUT HLT input DAT max DAT zero DAT 0 <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