Reputation:
I am trying to understand the MARIE assembly language. I don't quite understand skipcond
for
doing things like <
, or >
, or multiply or divide.
I am taking this simple program:
x = 1
while x < 10 do
x = x +1
endwhile;
What I don't understand is how to use certain skip conditions:
Skipcond 800 if AC > 0,
Skipcond 400 if AC = 0,
Skipcond 000 if AC < 0
Now, I know I would subtract x from 10 and test using skipcond.
I am not sure which one and why. I guess if I knew how they really work maybe it would be easier to understand. Why is it used to compare to zero?
This is what I have:
100 load one
101 store x
102 subt ten
103 skipcond400 if x-10 = 0? // or skpcond000 x -10 < 0??
Upvotes: 7
Views: 60262
Reputation: 350252
SkipCond
can be confusing. It may help to assume you will always use it in combination with a Jump
that follows right below it. This is anyhow the most common way to use it, but also helps to see it as an if...then
construct -- which is more intuitive.
You have already given this table:
Skipcond 800 if AC > 0,
Skipcond 400 if AC = 0,
Skipcond 000 if AC < 0
Now if we want to understand this as the more intuitive if...then
construct, combine it with a Jump
, invert the meaning of the condition (in a comment), and indent the Jump
so to visually indicate it is executed only when the condition in the comment is true. Here is how that looks for one of those SkipCond
formats:
Skipcond 800 / if AC <= 0, then:
Jump Address
/ Else continue here
I find this easier to read. To stress the point: I propose here to add a comment next to the SkipCond
instruction where you invert the meaning of when it skips, so to express when it does not skip, but actually makes the jump.
If we apply this approach to your pseudocode loop, we get this:
Load One / AC := 1
While, Store X
Subt Ten / AC := X - 10
SkipCond 000 / If X - 10 >= 0, then exit loop
Jump EndWhile
Load X
Add One
Jump While
EndWhile, Halt
Upvotes: 0
Reputation: 1
This may help. There are many ways to write this but I think this is the easiest way to understand what is happening in the loop. Note: usually variables are placed at the bottom of the program.
while x<10
x = x+1
Org 100
Load One / loads accumulator = 1 from a decimal constant
Store X / initialize the var x = 1
loop, Load X / loads x into the accumulator
Subt Ten / compares x to 10
Skipcond 000 / if ac < 0 i.e. if x < 10 run rest of loop body
Jump Endloop / if ac => 10 terminate loop
Load X / begin the Loop
Add One / add 1 to x
Store X / store new value in X
Jump loop / continue loop
Endloop, Halt / ends loop
One, Dec 1 / Constant
Ten, Dec 10 / Constant
X, Dec 0 / Variable
Upvotes: 0
Reputation: 49719
while x < 10 do
x = x + 1
will jump out of the loop as soon as x equals 10. If you subtract 10 from x, you'll get a negative value until x equals 10 (and the value is 0). So using skpcond000
would be wrong as it would jump out too soon. So skpcond400
is correct.
Perhaps it is easier to understand if you change the C code so it will be closer to the assembly code:
Original: while (x < 10) do
Subtract 10: while ((x - 10) < 0) do
Use != instead of <: while ((x - 10) != 0) do
Also note that you have to increase x
after the condition to reproduce identical behaviour to the while
loop.
Upvotes: 2