MARIE Assembly If-Then

14,130

Solution 1

It does the comparison by subtracting 1 from x, leaving the result in the accumulator. We can then use a conditional branch on whether the resulting value in AC is zero, positive or negative.

Look up what SKIPCOND 800 does: How does `Skipcond` work in the MARIE assembly language?

Unlike most architectures where add/subtract instructions set flags and conditional branches test them, MARIE's conditional branch instruction is a test-and-branch, like MIPS bgtz / beq with $zero / bltz

Solution 2

I think the reason they add

SUBT ONE 

is because we don't have a skip condition for x > 1, but we do have a skip condition for x > 0, which is

skipcond 800 / meaning X > 0

Since that's the case, I think they just subtracted one from both sides, making them both equal. (x - 1) > (1 - 1) / same as (x - 1) > 0. from here, we can use skipcond.

That's my best educated guess for why that's included. Hope this helps five years later.

Share:
14,130
silver
Author by

silver

Updated on June 04, 2022

Comments

  • silver
    silver almost 2 years

    Pseudocode:

    if x > 1 then
       y = x + x;
       x = 0;
    endif;
       y = y + 1;
    

    I am tracing the MARIE code below based from the pseudocode:

    ORG 100
    IF,     LOAD X
            SUBT ONE / What is this for?
            SKIPCOND 800
            JUMP ENDIF
    THEN,   LOAD X
            ADD X
            STORE Y
            LOAD ZERO
            STORE X
    ENDIF,  LOAD Y
            ADD ONE
            STORE Y
            HALT
    X,      DEC ?
    Y,      DEC ?
    ONE,    DEC 1
    ZERO,   DEC 0
    

    Why is the SUBT ONE needed there?

  • Peter Cordes
    Peter Cordes over 5 years
    Yup, this is correct, and a more accurate description than @starblue's answer. Skipcond tests the value in AC, and doesn't care about the result of a previous subtraction (How does `Skipcond` work in the MARIE assembly language?)
  • Peter Cordes
    Peter Cordes over 5 years
    Unlike many other assembly languages / ISAs, MARIE doesn't have flags. It's more like MIPS, where SKIPCOND is a test-and-branch on AC. Since we're using it on the SUBT result, the logic of testing the SUBT result is correct, but the mechanism is explained wrong. (How does Skipcond work in the MARIE assembly language?). I decided to go ahead and edit the answer to fix it.