Karan
Karan

Reputation: 1

Regarding Little man computer remainder (modulus) of dividing numers

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?

Upvotes: 0

Views: 6326

Answers (4)

SSSuppeRank
SSSuppeRank

Reputation: 1

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

Upvotes: 0

Ishwor Khanal
Ishwor Khanal

Reputation: 1370

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

Upvotes: 0

Paul Hankin
Paul Hankin

Reputation: 58280

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.

Upvotes: 1

Paul R
Paul R

Reputation: 212979

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.

Upvotes: 1

Related Questions