6. Control Structures
The goto Statement
• Most older programming languages (and many new ones) have a goto statement:
Finish :
x = 37;
y = x+1; goto Finish; …
z = x*y;
…
• goto unconditionally transfers control to a statement other than the next one.
• Used with an “If” statement, transfer of control can be made conditional on some
Boolean variable. For example
if (x=y) then goto Finish;
– will only transfer control to Finish if x = y is TRUE
• Use of goto is discouraged in modern HLL programming because it can always be
replaced by control structures which support structured programming.
• Notice the use of a label applied to the destination statement. This can also be done in
assembly language to label a statement that will be a destination for a JUMP.
Systems and Networks 6 Control Structures 2
Addresses of Instructions
• Already labelled the address of a word containing data (e.g. an integer variable)
LOAD R3,salary[R0] …
salary DATA $02d0 ;salary is address of var
• Can also label the address of a word containing an instruction, jumping to it
JUMP calc[R0] …
calc LOAD R3,x[R0] ;calc is address of instr …
• An address is just a number giving the location of some information in memory – Addresses tend to be hard to remember!
• A label is a symbolic name of and address; it is used only in assembly language, not machine code (where it is translated into a numeric address).
– Put the label at the beginning of a statement, starting in the first column
• Note the use of the indexed addressing mode with R0 to jump to calc. This is
the only way Sigma16 can do absolute addressing.
Systems and Networks 6 Control Structures 3
The JUMP Instructions
• JUMP tells the machine unconditionally to go to an instruction somewhere else, instead of the next one.
lab ADD loop SUB
R2,R5,R6 ; R2 = R5 + R6
…
R3,R7,R8
JUMP loop[R0] ; goto loop
• Notice that the only way the machine can ever execute the ADD instruction is if another instruction jumps to lab
• The JUMPT and JUMPF instructions do conditional jumping. To do this they need an additional operand which is held in a chosen register.
– The register content is interpreted as a Boolean value where 0 is FALSE and any other value is also treated as TRUE.
JUMPT Rn,loop[R0]
– will only jump to loop if the value in Rn is TRUE (not zero).
– JUMPF works exactly the same way but only jumps if the register is zero (FALSE)
– Usually JUMPT and JUMPF are preceded by a compare instruction. These produce Boolean results e.g. CMPEQ R3,R1,R2 will put 0 in R3 unless R1 = R2.
Systems and Networks 6 Control Structures 4
Control Constructs
• HLLs have several ways of controlling program flow.
• As noted many HLLs have a goto statement for unconditional transfer.
• Most modern HLLs have familiar conditional constructs for repetitive computations: for loops, while loops, if…then…else structures etc.
– Obviously a goto statement can be implemented easily in assembly language with a single JUMP.
– An statement like if (x=y)then goto L1 statement can be implemented using a compare and a conditional jump.
; Assume x is in R3 and y in R4 CMPEQ R2,R3,R4 ;R2:=(x=y)
JUMPT R2,L1[R0] ;if (R2<>0)goto loop
…
L1 SUB R3,R7,R8
• We now look at how the other constructs can be implemented.
Systems and Networks 6 Control Structures 5
•
Unconditional control transfers
Apart from goto, simplest HLL structure is the infinite loop, HLL form: While (true)
{ HLL-statement; }
– This is useful in scenarios where some code is intended to run indefinitely (perhaps waiting for an interrupt).
This can easily be implemented using an unconditional JUMP as follows
loop instruction instruction
JUMP loop[R0]
•
Systems and Networks 6 Control Structures 6
The if-then statement
• In Java the “then” is implied.
if (boolean expression) {statement 1};
{statement 2};
• This is compiled into something like:
;Evaluate boolean expression, put result in R5 ;
JUMPF R5,skip[R0]
{instructions for statement 1}
skip {instructions for statement 2}
• Usually it takes several instructions anyway just to perform one HLL statement.
• If you have several statements in a {…} block, this doesn’t affect the structure–
you just have even more assembly language instructions in a section of code!
Systems and Networks 6 Control Structures 7
Example of if
; x = 2; LEA
STORE
; if (y>x)
LOAD LOAD CMPGT JUMPF
; {a = 5;} LEA
STORE ; b = 6;
skip LEA
STORE
R1,2[R0]
R1,x[R0]
R1,y[R0] R2,x[R0] R3,R1,R2 R3,skip[R0]
R1,5[R0]
R1,a[R0]
R1,6[R0]
R1,b[R0]
; R1 = 2 ;x= 2
; R1 = y
; R2 = x
;R3=(y>x)
; goto skip if not (y>x)
; R1 = 5 ;a= 5
; R1 = 6 ;b= 6
x=2
if (y>x)
{a = 5;} b = 6;
Systems and Networks 6 Control Structures 8
The if—then—else Statement
if (boolean expression) {statement 1}; else {statement 2};
{statement 3}; is compiled into:
;evaluate boolean expression, put result in R5 JUMPF R5,else[R0]
else done
{instructions for statement 1} JUMP done[R0] {instructions for statement 2} {instructions for statement 3}
IT Computer Systems
6 Control Structures 9
Example of if-then-else
; x = 2; LEA
STORE
; if (y>x)
LOAD
LOAD CMPGT JUMPF
; {a = 5;} LEA
STORE
JUMP ; {c=7;}
else LEA STORE
; b = 6; done LEA
STORE
R1,2[R0]
R1,x[R0]
R1,y[R0] R2,x[R0] R3,R1,R2 R3,else[R0]
R1,5[R0] R1,a[R0] done[R0]
R1,7[R0]
R1,c[R0]
R1,6[R0]
R1,b[R0]
; R1 = 5
; a= 5
; skip the else part
;R1 = 7
; c= 7
; R1 = 6 ;b= 6
; R1 = 2 ;x= 2
; R1 = y
;R2=x
; R3 = (y>x)
; goto else if not(y>x)
x = 2; if (y>x)
{a = 5;} else {c = 7;}
b = 6;
IT Computer Systems 6 Control Structures 10
while and for loops
• In modern HLLs unbounded iteration is usually implemented by while
statements and bounded iteration by for.
• These are easily translated into assembler forms.
– A useful first step is to re-express them using goto statements
– Then translate into assembly language using CMP and JUMPT or JUMPF instructions.
Exercise: work out assembly language forms for the following examples:
while (x