【问题标题】:Multiplying two numbers by finding out the greatest and lesser value通过找出最大和较小的值来将两个数字相乘
【发布时间】:2013-02-26 14:20:01
【问题描述】:

我正在使用一个名为可见虚拟机的小人计算机模拟器程序来学习汇编语言格式的编码基础知识。目前,我正在尝试将任意两个数字相乘(xy),但以一种有效的方式通过抓取最大的数字并将较小的数字等于多少次添加到其中。如何交换数字,以便取最大数字并加上较小数字等于多少倍?

例如

输入可以是:

5 * 1212 * 5

高效计算:

12 + 12 + 12 + 12 +12 = 60

效率不高:

5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 = 60

代码:

IN 
STO 99
IN 
STO 98 
STO 96
LDA 99
SUB 97
STO 99
LDA 96
ADD 98
BRP 07
LDA 96
OUT 96
*95
DAT 001
HLT

【问题讨论】:

    标签: assembly little-man-computer


    【解决方案1】:

    查看the command listing,您似乎可以通过从第二个输入中减去第一个输入然后使用条件分支来查看哪个更大。像这样的:

         INP       // Get input into accumulator
         BRZ QUIT  // If zero, we're finished
         STA A     // Accumulator to A
         INP       // Get input into accumulator
         BRZ QUIT  // If zero, we're finished
         STA B     // Accumulator to B
         SUB A     // Subtract A from accumulator (B)
         BRP LOOP  // Jump to LOOP if B > A already, otherwise swap
         LDA A     // Put A into accumulator
         STA TEMP  // Accumulator to TEMP
         LDA B     // Put B into accumulator
         STA A     // Overwrite A with B
         LDA TEMP  // Put TEMP (A) into accumulator
         STA B     // Overwrite B with A: now B > A
    LOOP LDA AB    // Put result into accumulator (starts off as zero)
         ADD B     // Add larger input to accumulator
         STA AB    // Update result
         LDA A     // Put loop counter (A) into accumulator
         SUB ONE   // Decrement
         STA A     // Update loop counter
         BRZ DONE  // Jump to DONE if loop counter is zero
         BRP LOOP  // Jump to LOOP if loop counter is positive
    DONE LDA AB    // Put result into accumulator
    QUIT OUT       // Output
         HLT       // Finish
    ONE  DAT 1     // ONE = 1
    A    DAT       // First input
    B    DAT       // Second input
    AB   DAT       // A * B
    TEMP DAT       // Temporary (needed for swap)
    

    请注意,这是完全未经测试的,因此它很可能包含错误!不过,我已经评论了来源,所以你可以看到这个想法。


    编辑 原来没有错误——在黑暗中刺伤还不错;)无论如何,以下是在 cmets 中提到的Java-based simulator 上运行的稍作修改的语法:

          INP       // Get input into accumulator
          BRZ :QUIT // If zero, we're finished
          STA :A    // Accumulator to A
          INP       // Get input into accumulator
          BRZ :QUIT // If zero, we're finished
          STA :B    // Accumulator to B
          SUB :A    // Subtract A from accumulator (B)
          BRP :LOOP // Jump to LOOP if B > A already, otherwise swap
          LDA :A    // Put A into accumulator
          STA :TEMP // Accumulator to TEMP
          LDA :B    // Put B into accumulator
          STA :A    // Overwrite A with B
          LDA :TEMP // Put TEMP (A) into accumulator
          STA :B    // Overwrite B with A: now B > A
    LOOP: LDA :AB   // Put result into accumulator (starts off as zero)
          ADD :B    // Add larger input to accumulator
          STA :AB   // Update result
          LDA :A    // Put loop counter (A) into accumulator
          SUB :ONE  // Decrement
          STA :A    // Update loop counter
          BRZ :DONE // Jump to DONE if loop counter is zero
          BRP :LOOP // Jump to LOOP if loop counter is positive
    DONE: LDA :AB   // Put result into accumulator
    QUIT: OUT       // Output
          HLT       // Finish
    ONE:  1         // ONE = 1
    A:    0         // First input
    B:    0         // Second input
    AB:   0         // A * B
    TEMP: 0         // Temporary (needed for swap)
    

    【讨论】:

    • 这真的很有帮助。一切似乎都是正确的,但是在运行它时,Line 2 cannot be interpreted 出现错误。地址图:01: ERROR! --> BRZ QUIT
    • 老实说:不知道!我通过查看 Wikipedia 上的指令集对此进行了嘲笑;我没有 LMC 汇编器来调试它。不过,我相信你已经了解了算法的基本前提:)
    • 谢谢,我已经接受了答案。如果有时间,这里有一个我测试过代码的在线模拟器:HERE
    • 如果您查看该小程序的帮助,您会发现它使用了对维基百科页面上编纂的语法稍作修改的语法。请参阅我对这种风味的编辑,结果证明可以按预期工作。
    • 谢谢你,你已经加倍努力帮助我。最后一个请求:它在上面提到的那个小程序中工作。但是在课堂上,我们使用的是 Visible Virtual Machine 软件,但我仍然无法通过修改让它在那里工作。 HERE是那个模拟器的页面,需要安装。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2018-05-09
    • 1970-01-01
    • 2014-07-30
    • 2019-10-20
    • 2020-11-06
    • 1970-01-01
    相关资源
    最近更新 更多