You are on page 1of 2

Additional MARIE examples:

Write MARIE code to perform the following C program excerpts


1. if(x >= y + z) {
x = x y;
z++;
}
else y--;
There are a couple of difficulties with the above code. First, how do we compare x to y + z?
Previously, we had either compared a variable to 0 or to another variable. Second, notice the
condition is >=, we do not have a code for >= for our skipcond statement. We solve the first by
subtract y from x and then subtracting z from the difference (that is, we do x y z). We can
solve the >= problem in two ways, first we can reverse the condition and rearrange our logic, or
second we can first test > and then test =. The two solutions are given below.

If:

Else:
Next:
Or

Next:

Else:

Load X
Subt Y
Subt Z
Skipcond
Jump If
Jump Else
Load X
Subt Y
Store X
Load Z
Add
#1
Store Z
Jump Next
Load Y
Subt #1
Store Y

00

Load X
Subt Y
Subt Z
Skipcond
01
Jump Next
Skipcond
10
Jump Else
Load X
Subt Y
Store X
Load Z
Add
#1
Store Z
Jump Next2
Load Y

// skip the next instruction if x < y + z


// we reach here if x < y + z is false, so x >= y+z, branch to ifclause
// we reach here if x < y + z is true, so go to else clause

// skip the next instruction if x = y + z


// Next will perform the comparison for >
// skip the next instruction if x > y + z

Subt
Store
Next2:

#1
Y

2. While(x > y)
For(i=x;i<n;i++)
z=z+i;
Top:

Load X
Subt Y
Skipcond
Jump Out
Top2: Load X
Store I
Subt N
Skipcond
Jump Top
Load Z
Add
I
Store Z
Load I
Add
#1
Store I
Jump Top2
Out:

// test while loop condition


10

// if x > y continue to inner loop


// i = x

00

// compare i < n
// if i < n continue to inner loop body
// otherwise, inner loop terminates, return to condition of outer
//
loop

// jump to top of inner loop

You might also like