0

I'm trying to figure out how to create a basic program in marie simulator, here's the pseudocode

input A
input B

if A < B:
   x = A + B

else:
   x = A - B

after searching the internet, i come up with this

INPUT
store A
INPUT
store B
if, load A
add B
store X
else, load A
subt B
store X
OUTPUT
Halt
A, DEC 0
B, DEC 0
X, DEC 0
Y, DEC 0

can anyone help me figure out the correct and working code?

Ginko
  • 13
  • 4

1 Answers1

0

An if statement breaks down into condition-test, then-part, and else-part.

You have code for the then-part and else-part, but not the condition test.

The purpose of the condition test it to choose one of the then-part and else-part.  We also want to make sure that only one of them executes, so prevent execution of the other as needed.

The way the conditional test is done, is using conditional branching.  The point of conditional branching is to skip ahead, if the condition is false, so as to avoid the then-part when the condition is false, but execute the then-part when the condition is true.

In C using the if-goto-label style of assembly:

    if ( condition is false ) goto elsePartOfIf1;
    <then-part>
    goto endOfIf1;
elsePartOfIf1:
    <else-part>
endOfIf1:

So a condition is tested, and if the condition is false, then goto the else part of the if-statement skipping over the then-part.  In the other case (the condition is true) we want to fail to branch to the else part, and instead simply proceed to execute the then-part.  After the then-part, we must skip over the else-part so as to continue with the next statement after the if-statement, which should execute next whether or not the then-part or the else-part executed.

To do a condition test in MARIE, you can subtract the operands and check the sign of the result.  In other words, we can translate A < B into A - B < 0, by subtracting B from both sides of the < relation.  This subtraction is easy to do, and the < 0 can be done with a MARIE SkipCond instruction.  There are other answers here on StackOverflow that will deal more directly with using MARIE's SkipCond instruction.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53