0

Write a program that asks the user to enter 2 numbers. The program then displays to the screen (OUT) the result of taking the remainder (modulus) of dividing the second number into the first number. For example, if the first number entered was 14 and the second number entered was 5 then the program would display 4.

14 mod 5 = 14 - (2 * 5 ) = 14 - 10 = 4

14 mod 7 = 14 - (2 * 7) = 14 - 14 = 0

You may assume that the numbers entered are always positive and greater than 0.

Hi this is the question I have no idea how to start this question/do it?

Karan
  • 1
  • 1
  • 1

4 Answers4

1

It's just simple arithmetic - to get x = m MOD n you can do this:

x = m / n    ; integer division (truncates)
x = x * n    ; multiply back up to get exact multiple
x = m - x    ; subtract to get remainder (modulus)

Since Little Man only has ADD and SUB arithmetic instructions you'll need to implement the multiply and divide operations from first principles.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Hi, this is meant to be done on little man computer with the opcodes that are used on little man computer. Do you know that? – Karan Apr 02 '14 at 10:13
  • Sure - you have ADD and SUB instructions for arithmetic, so that should be all you need to implement the above pseudo-code. – Paul R Apr 02 '14 at 10:14
  • Thats actually what I needed help in because i dont know what opcodes to use in what way but okay ill try it anyway thanks. – Karan Apr 02 '14 at 10:34
  • Think back to elementary school and how you used to do long division and long multiplication by hand using just addition and subtraction. – Paul R Apr 02 '14 at 10:39
  • No I think loops are meant to be used in this but im not sure :/ – Karan Apr 02 '14 at 10:45
  • Yes, you need loops - think about how you would do e.g. multiplication using repeated addition. – Paul R Apr 02 '14 at 11:53
  • Hi paul I tried it in little man computer but cant seem to get it to work, can you please help me out with the commands – Karan Apr 03 '14 at 06:28
  • Post the code you've written so far in a new question and then people here can help you to fix the problems with it. – Paul R Apr 03 '14 at 08:30
1

You can solve this by subtracting the second number from the first until you get a negative number. Here's a working version:

    INP
    STA R0
    INP
    STA R1
    LDA R0
L0  STA R0
    SUB R1
    BRP L0
    LDA R0
    OUT
    HLT
R0  DAT
R1  DAT

You can see this working here: Modulo operation on LMC emulator.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
0

Here we go you have to solve this division problem using subtraction method..it works exactly your problem finding the remainder of two number division...

// PRODUCED BY JAMES KHANAL

INP //ask the user
BRZ QUIT // halt the execution if input zero
STA DIVIDEND // store in dividend variable
INP // input dividor
BRZ QUIT // halt the execution if input zero
STA DIVIDOR // store in divider variable
LDA DIVIDEND // load into acc
LOOP STA RESULT // store the temp result
LDA RESULT // load the result
SUB DIVIDOR // subtract the dividor to acc
BRP LOOP //loop if acc is positive or zero 
LDA RESULT // load the result into acc
OUT // display the result
QUIT HLT // halt if brz
HLT // hlt the execution
DIVIDEND DAT //declare variable
DIVISOR DAT //declare variable
Community
  • 1
  • 1
Ishwor Khanal
  • 1,312
  • 18
  • 30
  • If the dividend inputted is 0, shouldn't the divisor be outputted as the result, instead of the program just halting? I agree with the second `BRZ QUIT`. – Kaiylar Feb 16 '17 at 21:34
  • Also, this solution would be for if the question doesn't say to assume that the inputs are positive/greater than 0 (the BRZ parts are unnecessary in this instance). – Kaiylar Feb 16 '17 at 21:42
0

INP STA FIRST INP STA SECOND LOOPTOP LDA FIRST SUB SECOND STA FIRST BRP LOOPTOP ADD SECOND BRA ENDLOOP OUT ENDLOOP HLT FIRST DAT SECOND DAT

it is inefficient way to solve this problem!! I just created a loop that subtracts the second from the first variable until it becomes negative, then I add the second variable to it

everything is based on school knowledge