显示商和余数的小主计算机程序

Little Main Computer program to display quotient followed by remainder

这是我目前拥有的,但我无法让它发挥作用。我需要让它输入一个股息和一个除数,然后输出结果和余数。示例:如果输入是 33 后跟 6,则输出将是 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

目前你正确得到输入,计算余数并输出。您只是错过了计算商并输出它的部分。商实际上是您跳回循环起点的次数。因此,在循环的每次迭代中增加一个计数器。然后,当您退出循环时,您将计算得太多,因此对于商,您将输出比该值少一的值。

其他备注:

  • 如果 LDA RESULT 紧跟在 STA RESULT 之后,则没有必要执行 LDA RESULT,因为该值仍在累加器中 -- 无需重新加载它。

  • 不需要 HLT 后跟 HLT。第二个永远不会被执行。

  • 在注释中说明 LMC 指令的作用是没有用的...例如,“询问用户”在 INP 旁边的注释不是有用的。评论应该解释更多的东西——一些特定于 this 程序的东西。您的大部分评论只是说一些人可以在 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>