MARIE Assembly If-Then

17.4k Views Asked by At

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?

3

There are 3 best solutions below

1
On BEST ANSWER

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

1
On

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.

0
On

Use MARIE Simulator to enter and run the following program:

Input

Store TestVal

If, Skipcond 800

Jump Else

Then, Store Y

Add Y

Jump EndIf

Else, Load Y

EndIf, Add X

Add X

Store Z

Output

Halt

X, Dec 5

Y, Dec 2

Z, Dec 0

TestVal, Dec -1

instructions:

  • Use ORG instruction to start your program at address 100.

  • Use your last university ID number when you are asked to input a number. For example, if your ID is1415161678532, then you will use the number 2.

b) Suppose that the value, say a, has been entered. What are the instructions in the above program that will be executed? Your answer should explain the flow of execution for a<0, a=0, and a>0.

c) Based on your answer in part b, formulate what happens in the three cases, by stating the output as a function of the variables (for example, Output= 3x-2a+y)