0

Can you help me to write a lmc program to multiply 2 negatives (-x)*(-y) = xy? For example, if you input -5 and -6, it should give you 30.

I have done for x*y=xy

INP
STA FIRST
INP
STA SECOND
LOOP LDA COUNT
ADD ONE
STA COUNT
LDA TOTAL
ADD FIRST
STA TOTAL
LDA SECOND
SUB COUNT
BRZ ENDLOOP
BRA LOOP
ENDLOOP LDA TOTAL
OUT
HLT
ONE DAT 001
COUNT DAT
TOTAL DAT
FIRST DAT
SECOND DAT
jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • Welcome to stack overflow! What have you tried to modify your existing code to get it to work for negative numbers? What happens right now with negative numbers? If you want peoples help it's important to show that you've put the time in to understand it yourself first. – whaleberg Dec 07 '16 at 15:43
  • Please have a look at [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – mmushtaq Dec 07 '16 at 15:52
  • Where exactly are you stuck? Here's a hint: if the values are negative, negate them at the beginning of the function. Now you're just multiplying two positive values. This works because a negative times a negative is always a positive, just like a positive times a positive. – Cody Gray - on strike Dec 07 '16 at 19:08

1 Answers1

0

You can always just make your inputs positive before you multiply them. This of course requires the user to enter only negative numbers, or only positive numbers (it will not work with one negative and one postive input).

In the code I have added a variable ZERO which equals to 0. Then I subtract the user's input from zero and we'll be left with the positive version. Then you can use the code you have written to solve the calculation.

        INP
        BRP JUMP
        STA FIRST
        LDA ZERO
        SUB FIRST
        OUT
JUMP    STA FIRST
        INP
        BRP JUMP2
        STA SECOND
        LDA ZERO
        SUB SECOND
        OUT
JUMP2   STA SECOND
LOOP    LDA COUNT
        ADD ONE
        STA COUNT
        LDA TOTAL
        ADD FIRST
        STA TOTAL
        LDA SECOND
        SUB COUNT
        BRZ ENDLOOP
        BRA LOOP
ENDLOOP LDA TOTAL
        OUT
        HLT
ONE     DAT 001
COUNT   DAT
TOTAL   DAT
FIRST   DAT
SECOND  DAT
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 1
    LMC does not define the behaviour of negative numbers. Mailboxes can only hold 0..999. As https://en.wikipedia.org/wiki/Little_man_computer#Instructions says, a SUB that produces a negative number will set FLAGS so you can branch, but the result in the accumulator is undefined. Some simulators do happen to work with negative numbers, but others don't. See also [How do I create a Little Man Computer program that shows how many positive numbers I have and how many negative numbers I have](https://stackoverflow.com/a/58962577) – Peter Cordes Oct 18 '20 at 21:04
  • If you want to handle user input of strings that represent negative numbers, you'd have to store the sign separately from the number (then multiply the magnitudes), or some other method that avoids relying on a mailbox (memory location) to hold a negative value. Unless you only care about an LMC implementation that allows negative numbers, which might be the case here. – Peter Cordes Oct 18 '20 at 21:07
  • [How create an Little Mans Computer (LMC) code that will get a number. Display 1 if the number is odd, display 0 if the number is even](https://stackoverflow.com/a/59022938) mentions that some LMC implementations produce `-1` in the accumulator for any would-be-negative result. – Peter Cordes Oct 18 '20 at 21:09