簡體   English   中英

小主計算機程序顯示商后跟余數

[英]Little Main Computer program to display quotient followed by remainder

這是我到目前為止所擁有的,但我無法讓它發揮作用。 我需要讓它輸入一個被除數和一個除數以及 output 結果以及余數。 示例:如果輸入是 33 后跟 6,則 output 將是 5 后跟 3,因為 33/6 是 5 余數 3。

00          INP                      //ask the user
01          BRZ     QUIT            // halt the execution if input zero
02          STA     DIVIDEND        // store in dividend variable
03          INP                     // input dividor
04          BRZ     QUIT            // halt the execution if input zero
05          STA     DIVISOR         // store in divider variable
06          LDA  DIVIDEND          // load into acc
07  LOOP    STA  RESULT            // store the temp result
08          LDA  RESULT             // load the result
09          SUB  DIVISOR            // subtract the dividor to acc BRP
10          BRP  LOOP                //loop if acc is positive or zero
11          LDA  RESULT             // load the result into acc
12          OUT                     // display the result
13  QUIT    HLT                     // halt if zero
14          HLT                     // halt the execution
15  DIVIDEND    DAT                 //declare variable
16  DIVISOR     DAT 
17  RESULT      DAT

目前你正確地得到輸入,計算余數和 output 吧。 您只是錯過了計算商和 output 的部分。 商實際上是您跳回循環開頭的次數。 因此,在循環的每次迭代中增加一個計數器。 然后,當您退出循環時,您將計數太多,因此對於商,您將 output 比該值少一。

其他備注:

  • 如果LDA RESULT緊跟在STA RESULT之后,則沒有必要執行它,因為該值仍在累加器中——無需重新加載它。

  • 不需要HLT后跟HLT 第二個永遠不會被執行。

  • 在注釋中說 LMC 指令的作用是沒有用的……例如,“詢問用戶”不是INP旁邊的有用注釋。 評論應該解釋更多的東西——特定於這個程序的東西。 您的大多數評論只是說某人可以在 LMC 語言規范中查找什么。 這不是評論的目的。

因此,這是您的代碼,其中包含用於獲取商的額外計數器,並考慮了上述說明。 你可以在這里運行它。

 #input: 19 9 INP // Input the dividend BRZ QUIT // Zero is not considered a valid input STA DIVIDEND INP // Input divisor BRZ QUIT // Division by zero is not allowed STA DIVISOR LDA ZERO // Initialise quotient STA QUOTIENT LDA DIVIDEND // Let dividend be the initial value of the remainder // Repeat as long as remainder would be non-negative: LOOP STA REMAINDER LDA QUOTIENT // Increment quotient ADD ONE STA QUOTIENT LDA REMAINDER // Reduce remainder SUB DIVISOR BRP LOOP // Output the results LDA QUOTIENT // quotient is one too great now SUB ONE OUT LDA REMAINDER OUT QUIT HLT // constants: ZERO DAT 0 ONE DAT 1 // variables: DIVIDEND DAT DIVISOR DAT QUOTIENT DAT REMAINDER DAT <script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.813/lmc.js"></script>

暫無
暫無

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

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