0

I am using a Little Man Computer simulator program called visible virtual machine to learn the basics of coding in assembly language format. Currently I am trying to multiply any two numbers (x & y) but in an efficient way by grabbing the greatest number and adding to it how many times the lesser number is equal to. How can I swap the numbers so it takes the greatest number and adds how many times the lesser number is equal to?

For example

The input can be either:

5 * 12 or 12 * 5

Efficient calculation:

12 + 12 + 12 + 12 +12 = 60

Not efficient:

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

Code:

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
tc.
  • 33,468
  • 5
  • 78
  • 96

1 Answers1

1

Looking at the command listing, it looks like you could achieve this by subtracting the first input from the second and then use conditional branching to see which is the greater. Something like this:

     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)

Note that this is completely untested, so it's highly possible that it contains a bug! However, I've commented the source, so you can see the idea.


EDIT Turns out there are no bugs -- not bad for a stab in the dark ;) Anyway, the following is the slightly modified syntax that runs on the Java-based simulator mentioned in the comments:

      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)
Xophmeister
  • 8,884
  • 4
  • 44
  • 87
  • This is really great help. Everything seems to be correct but when running it there is error in `Line 2 cannot be interpreted`. Address map: `01: ERROR! --> BRZ QUIT` –  Feb 26 '13 at 20:20
  • Honestly: No idea! I mocked this up looking at the instruction set on Wikipedia; I don't have an LMC assembler to debug this. I'm sure you get the basic premise of the algorithm, though :) – Xophmeister Feb 26 '13 at 23:34
  • Thank you, I have accepted the answer. If you have time, here is an online simulator that I tested the code: [HERE](http://www.java-gaming.org/index.php?topic=24821.0) –  Feb 27 '13 at 05:33
  • 1
    If you look at that applet's help, you'll see it uses a slightly modified syntax to that codified on the Wikipedia page. See my edit for that flavour, which turns out to work as expected. – Xophmeister Feb 27 '13 at 09:57
  • Thank you, you have gone the extra mile to help me. One last request: It works in that applet mentioned above. But in class we are using the software Visible Virtual Machine and I still cant get it to work there with the modifications. [HERE](http://www.cba.uri.edu/vvm/) is the page of that simulator, it requires installation. –  Feb 27 '13 at 14:14
  • This is left as an exercise! You won't learn anything by me changing the flavour of the syntax so it works in whatever assembler you're working with. As a hint: I've never used VVM, so the approach I would take is to look at the documentation and, preferably, some example code and try to match things up. It's a simplified assembly language, so it's not a hard task. For example, in my original, the labels are naked, whereas in the Java applet, it's looking for a colon (i.e., `LOOP` vs. `LOOP:`); similarly for referencing memory (`TEMP` vs. `:TEMP`), etc... Experiment :) – Xophmeister Feb 27 '13 at 15:00