MARIE Assembly If-Then

silver picture silver · Dec 8, 2013 · Viewed 14.1k times · Source

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?

Answer

starblue picture starblue · Dec 8, 2013

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