【问题标题】:Little Main Computer program to display quotient followed by remainder小主计算机程序显示商后跟余数
【发布时间】:2020-11-28 18:00:41
【问题描述】:

这是我目前所拥有的,但我无法让它工作。我需要让它输入一个被除数和一个除数,然后将结果与余数一起输出。示例:如果输入是 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

【问题讨论】:

  • 它只显示剩余部分。但我需要它同时显示

标签: divide little-man-computer


【解决方案1】:

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

其他说明:

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

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

  • 在 cmets 中说明 LMC 指令的作用是没有用的……例如,“询问用户”在 INP 旁边不是有用的注释。评论应该解释更多的东西——特定于 this 程序的东西。您的大多数 cmets 只是在说某人可以在 LMC 语言规范中查找的内容。这不是 cmets 的目的。

因此,这是您的代码,其中包含用于获取商的额外计数器,并考虑了上述说明。你可以在这里运行它。

#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>

【讨论】:

  • 天哪!谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-01-04
  • 2016-09-30
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 2018-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多