2

I am trying to write a program in the MARIE assembly language that will divide two numbers using repeated subtraction. I need to count the number of subtractions needed before reaching zero or minus numbers. I am having a few problems with this, here is my code so far:

    Load    A
    Store   A

Load,   Load    A 
    Subt    B
    Skipcond 800
    Jump    Endloop


    Load    C
    Add     One
    Store   C

Endloop,    Output
    Halt

A,  DEC     10
B,  DEC     2
C,  DEC     0
One, DEC    1

Any help would be much appreciated as I have been struggling with this for a while

UPDATE:

I have changed my code but now get an infinite loop - any ideas how I can fix this?

    Input
    Store   A
    Input
    Store   B
    Load    A
    Skipcond    800
    Jump    Endloop
Loop,   Subt    B
    Store   A
    Load    X
    Add     One
    Store   X
    Load    A
    Skipcond    400
    Jump    Loop
    Load    X
Endloop,    Halt
A,  DEC         0
B,  DEC         0
X,  DEC         0
One, DEC        1
Ryan Dodd
  • 23
  • 1
  • 7
  • 1
    I don't see you looping back. Other than that, it looks kinda sensible. You will want to output `C` of course. – Jester Apr 08 '16 at 23:51
  • I have changed my code but now get an infinite loop - any ideas what I'm doing wrong? – Ryan Dodd Apr 16 '16 at 22:29

1 Answers1

1

Using Skipcond 400 if the number is not divisible by the divider there will be an endless loop as it will go negative not zero.

Care needs to be taken not to increment X when the remainder is not divisible by B. So a check is made for the remainder equalling zero, so X can be incremented when A is no longer positive.

    Input
    Store   A
    Input
    Store   B

Loop,   Load    A  
    Subt    B
    Store   A
    Skipcond 800
    Jump    Endloop / While X is positive it will continue
    Load    X
    Add     One
    Store   X
    Jump Loop

IncrementX, Load    X
    Add     One
    Store   X
    Load    A
    Subt    B
    Store   A

Endloop, Load   A
    Skipcond 000 /Skip if negative
    Jump    IncrementX

    Load    X
    Output   
    Halt
A,  DEC     0
B,  DEC     0
X,  DEC     0
One, DEC    1