assemblymarie

testing in a while loop (MARIE)


I'm trying to write a while loop, while N > 5.

While I understand just making a loop point and jumping back to it, how should I write a skipcond test for N > 5?

I looked at the one other MARIE question regarding while loops, but didn't quite get an answer out of it.

EDIT: Forgot to mention my idea was to subtract 5 from N, then skipcond 800 to see if it is true or not. Is this a proper way of doing it?


Solution

  • Here is what a while loop close to yours looks like in MARIE.

    Regarding your question, subtracting 5 from N after you write Loop, Load N, should work just fine.

     X := 1;
     while X < 10 do
         X := X + 1;
     endwhile;
    

    ----- Marie Code of above

    ORG 100
             Load One
             Store X      /Initialize X
    Loop,    Load X       /Load loop constant
             Subt Ten     /Compare X to 10
             SkipCond 000 /If AC<0 (X is less than 10), continue loop
             Jump Endloop /If X is not less than 10, terminate loop
             Load X       /Begin body of loop
             Add One      /Add 1 to X
             Store X      /Store new value in X
             Jump Loop    /Continue loop
    Endloop, Halt         /Terminate program
    X,       Dec 0        /Storage for X
    One,     Dec 1        /The constant value 1
    Ten,     Dec 10       /The loop constant