Unit 4.1 -- Translation of if-then-else
PROG
Maximum of Two Numbers
PED
To learn how to convert if statements to assembler
To learn about comparison instructions, conditional and
unconditional branches, and the condition code
CONCEPTS
Condition Code is a two-bit entity (stores the numbers
between 0 and 3)
After many instructions, particulary the C instruction, this
will be set to a value. See POP for each instruction to
find out what each value of the condition code means in a
given context.
The BC instruction will cause the machine to branch to the
"location" indicated, if the condition code is evaluated to
be true.
BC 15,location
or
B location
will cause an unconditional branch
How to convert an if statement
if var1 ro var2 then
statements 1
else
statements 2
endif
"ro" is a "relational operator" such as < <= = <> > >=
Result:
L regA,var1
L regB,var2
CR regA,regB
BC mask,Innn
statements 1
BC 15,FInnn
Innn EQU *
statements 2
FInnn EQU *
Determining the mask condition--function of the ro
The compare instruction will generate the following
condition codes (see Principles of Operation):
0 if var1 = var2
1 if var1 < var2
2 if var1 > var2
1) Determine which of these conditions the branch should
occur on.
(These conditions are the exact opposite of the
relational operator "ro".)
2) Set mask to zero
3) If we should branch when var1 = var2, add 8 to mask
if we should branch when var1 < var2, add 4 to mask
if we should branch when var1 > var2, add 2 to mask
4) Make the result of mask the first operand of the BC
instruction
SF
label equ *
We can now use "label" in branch or other instructions as
the address of whatever follows the "label" in the labeled
statement
Usual use -- to establish a branch point when translating if
and do statements.
Compare Instructions
CR and C set the two-bit condition code as indicated below.
"cc" refers to the condition code.
"Operand 2" refers to
- the memory location in the case of C
- the second register in the case of CR.
"Operand 1" refers to the register for both C and CR.
cc:=0 when Operand1 = Operand2
cc:=1 when Operand 1 < Operand2
cc:=2 when Operand 1 > Operand2
Branch Instruction
BC Mask,Address
"Address" usually specified as the "label" defined in a name
field of EQU or an instruction
The "Mask" is four bits, which corresponds to the 8-bit, the
4-bit, the 2-bit, and the 1-bit (for reference, see drawing
below)
E.G., if we add 8 and 2 to get the mask, then we say the 8-
bit and the 2-bit are set (for reference, see drawing
below)
The branch will occur to the "address" if any of the
following four conditions are true:
a) the 8-bit of the mask is set AND the condition code is
equal to zero
b) the 4-bit of the mask is set AND the condition code is
equal to one
c) the 2-bit of the mask is set AND the condition code is
equal to two
d) the 1-bit of the mask is set AND the condition code is
equal to three
= < >
---------------------------------
| 8-bit | 4-bit | 2-bit | 1-bit |
---------------------------------
cc:= 0 1 2 3
4.html
Section Four, If Statements
If Pascal didn't have any conditional statements such as
"if" and "case" and it didn't have any loop statements such
as "while," "for," or "repeat until," it would be a pretty
weak language.
Likewise, in ASSEMBLER, we must learn how to convert
ifs--and later for's and while's into ASSEMBLER. In order
to do this we must learn how to assembler makes
conditional branches. That is, we need an instruction that
will change the flow of control from going to the next
statement, as it normally would. This conditional statement
would go to some other statement, if some condition is
reached, such as we found that one quantity is less
than the other. Thus, the first part of this explication
will describe the two statements, what they do, and how to
code them. The second part will describe some techniques for
mechanically translating if statements to ASSEMBLER
language.
The two statements we will learn about are the compare
instruction and the branch on condition instruction.
The compare instruction sets the "condition code." The
conditional branch, BC, inspects the condition code. It may
or may not branch, depending upon what the "condition code"
was set to.
The condition code is a two-bit entity. Thus, it can
contain the values 0 to 3. (You have noticed that it was
displayed in when you hit a breakpoint when running an
ASSEMBLER program.)
The condition code can be changed with the Compare (C) or
Compare Register Instruction (CR).
The format is
C R,Mx
CR R1,RA
We talk about the first argument and the second argument of
the compare. The first argument is always the register.
The second argument is the memory location for the Compare
(C) instruction. It is RA for the Compare Register (CR)
instruction.
The C or CR instruction will change the condition code to 0,
1, or 2. (Some instructions change the condition code to
three, but compare never will.)
The condition code will be zero if the first argument is
equal to the second argument.
The condition code will be 1 if the first argument is less
than the second argument
The condition code will be 2 if the first argument is
greater than the second argument.
The conditional branch takes two arguments, a mask field and
the address of an instruction. For example, the
instruction:
BC 7,ZING
7 would be the "mask." Zing is a label, the address of the
instruction to branch to.
The conditional branch takes two arguments, a mask field and
the address of an instruction. For example, look at the
instruction:
BC 12,LABEL001
A 7,X
LABEL001 equ *
L 9,X
It looks at the condition code and the four bit integer,
twelve. Depending upon these, it will either continue
executing with the the L 9,X--the branch will occur. Or,
it will execute the A 7,X instruction--the branch will
fall through.
The rules for how the BC will work as follows:
The mask is a four-bit quantity. That means it has an
eight-bit, a four-bit, a two-bit
and a one-bit which means that the number that can be stored
there is from zero to fifteen.
If the condition code is 0 and the eight-bit is on, it will
branch
If the condition code is 1 and the four-bit is on, it will
branch
If the condition code is 2 and the two-bit is on, it will
branch
If the condition code is 3 and the one-bit is on, it will
branch
(Since the compare instruction will never change the
condition code to three, it doesn't matter whether the
one-bit is on or off after a C or CR. For example, BC
6,BLAH and BC 7,BLAH will do the same thing after a C or CR
instruction.)
That explains how compare and branch instructions work. I
would like to provide you with some guidance on how to
convert IF statements--or more precisely a mechanism by
which you can mechanically convert PASCAL if statements to
ASSEMBLER.
The example in this unit is the first simple case is PASCAL
if statements to do:
if var1 ro var2 then
statements 1
else
statements 2
endif
An example is the simple statements to compute MAX= the
maximum of A and B This illustrated by the code included in
the class notes.
ro represents a "relational operator" -- <,<=,>,>=,<>,or =
The template is illustrated on page 66 of your class notes.
The two Load instructions load variable one into RegA and
variable two into RegB.
Then we compare them.
If the relational operator is not true, in other words if A
not ro B, we branch to Innn. This is where the ELSE part
gets executed.
If it is true we fall through and execute the ASSEMBLER that
will do the statements in the "then" part.
When those are finished, the BC 15 will take us around the
ELSE part--we DON'T want to execute the ELSE part after
doing the THEN part.
Then there is the statements for the ELSE part. Note that
the Branch would have landed here if the ELSE part is not
true.
After the FInnn EQU *, we put the ASSEMBLER corresponding to
the translation of any statements that follow the if the
else statement in the PASCAL code.
Remember that we must put the opposite branch than that
which appears in the PASCAL program.
E.G., if we have a if a=b, we put a branch that would branch on a