1

I have been asked to do the following:

Write MARIE code to perform the following program excerpt.

If (x < y + z) {
x = x – y; z=z+1;
}
else y=y-1;

Instructions: - Use “ORG” instruction to start your program at address 200. - The following labels and directives should be included at the end of your program:

X, Dec 4
Y, Dec 2
Z, Dec 5
One, Dec 1

and I wrote this:

ORG 200 
Load X
Subt Y
Subt Z
Skipcond 000
Jump Else
If, Load X
Subt Y
Output
Load Z
Add One
Output
Else, Load Y
Subt One
Output
Halt

X, DEC 4
Y, DEC 2
Z, DEC 5
One, DEC 1

my code excutes both if and else conditions. Why is that? and how can I fix it? is the code I wrote correct?

Abdullah
  • 147
  • 2
  • 12

1 Answers1

0

Why is that? The code executes both the if and else conditions because at the end of the If block there is a need for a JUMP instruction to move to the end of the if statement.

How to Fix it?

Before:

If, Load X
    Subt Y
    Output
    Load Z
    Add One
    Output
Else, Load Y
    Subt One
    Output
    Halt

After:

If, Load X
    Subt Y
    Output
    Load Z
    Add One
    Output
    JUMP END
Else,   Load Y
    Subt One
    Output
END,    Halt

Is the code I wrote correct? You need to store back in memory the value of X Y and Z so you need to replace the OUTPUT instruction with the corresponding STORE instruction as follow:

    / Code Section
    ORG 200 
    Load X
    Subt Y
    Subt Z
    Skipcond 000
    Jump Else
If, Load X
    Subt Y
    STORE X
    Load Z
    Add One
    STORE Z
    JUMP END
Else,   Load Y
    Subt One
    STORE Y
END,    Halt
    / Data Section
X,  DEC 4
Y,  DEC 2
Z,  DEC 5
One,    DEC 1

The result for the Marie simulator running the above program

ghorayeb
  • 23
  • 1
  • 5