0

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

例如

输入可以是:

5 * 12或者12 * 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
4

1 回答 1

3

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

     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)

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


编辑结果证明没有错误——在黑暗中刺伤还不错;)无论如何,以下是在评论中提到的基于 Java 的模拟器上运行的稍作修改的语法:

      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)
于 2013-02-26T15:10:55.203 回答