[英]what must the minimum number of bits in each word of the Little Man computer be?
[英]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。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.