2

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?

k_rollo
  • 5,304
  • 16
  • 63
  • 95
  • See also [MARIE Assembly if else](https://stackoverflow.com/q/69230779) for a detailed guide to understanding the logic of an `if` and how that can map to `if()goto` with conditional jumps like SKIPCOND over a JUMP. – Peter Cordes Apr 03 '22 at 00:59

3 Answers3

3

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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
starblue
  • 55,348
  • 14
  • 97
  • 151
  • 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?](https://stackoverflow.com/q/5136860)). I decided to go ahead and edit the answer to fix it. – Peter Cordes Nov 06 '18 at 15:25
1

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.

  • 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?](https://stackoverflow.com/q/5136860)) – Peter Cordes Nov 06 '18 at 15:20
0

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)

Bunny
  • 1,180
  • 8
  • 22