-1

I always get stuck in a loop when I try to make a division with a negative number in LMC. For exemple 10/2 will give me 5 and -10/-2 will also work and give 5. The problem is when there is a single negative number in the division for exemple 10/-2 and -10/2... 10-2-2-2-2-2=0 ; -10--2--2--2--2--2--2=0 but -10-2-2-2-2-2 does not equal 0. I thought of doing the absolute value but brings me nowhere... really in a pinch right now. If some bright mind could show me the way to enlightement and end my suffering please.

  • 1
    Note that LMC doesn't support negative numbers - e.g. the results of a subtract are undefined if the result would have been less than zero. It's not a binary computer, it only does things that work with pure numbers without a dependency on format. https://en.wikipedia.org/wiki/Little_man_computer#Instructions. Presumably you're using an LMC simulator that does define the behaviour of negative numbers. – Peter Cordes Apr 10 '20 at 03:14
  • If you still need an answer to your question, please mention which LMC flavour you are working with, as input of negative numbers into the accumulator is not defined. It could result in 10s complement, or the setting of the negative flag, or still something else. Without clear specification on how your LMC behaves, this question cannot be answered. Posting your current code would also be good, so we can suggest where to alter it, instead of producing completely new code. – trincot Apr 16 '20 at 06:55
  • *"end my suffering please"*: One month, and no reaction to the comments here. – trincot May 08 '20 at 09:48

1 Answers1

1

I'm not sure how you're doing it (since you've posted no code) but the usual way to do division is via repeated subtraction of just the magnitudes (no signs), followed by an adjustment for the signs.

For example, with 10 / -2, just use repeated subtraction of two from ten (to get five), then adjust the sign based on original signs. If divisor and dividend have different signs, the result is negative. Otherwise the result is non-negative.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I think it would be clearer to say "of absolute values". If you don't take the sign into account, `dividend -= divisor` will be adding 2 which will never cross zero (except via overflow) – Peter Cordes Apr 10 '20 at 01:11
  • How can I achieve the absolute value? If i input 8 and -8 how to i get 8 as the absolute value in both cases? – tarik laouisset Apr 10 '20 at 01:20
  • @tariklaouisset: depends on the instruction set. You'll almost certainly have a `negate` instruction which can set a value to its negative counterpart, and a `branch if negative` which can be used to select different code paths for negative values. – paxdiablo Apr 10 '20 at 02:52
  • 1
    @PeterCordes, changed to "use magnitudes" rather than "without taking sign into account". – paxdiablo Apr 10 '20 at 02:53
  • @tariklaouisset: https://en.wikipedia.org/wiki/Little_man_computer#Instructions says that sub sets flags if the result would have been negative. But that LMC doesn't support negative numbers in the first place so the accumulator result is undefined in that case for SUB. But if you have a version of LMC that does support negatives, you'd implement `0 - x` and then branch on that being negative (original was already non-negative). – Peter Cordes Apr 10 '20 at 03:16