6. Control Structures
The goto Statement
• Most older programming languages (and many new ones) have a goto statement:
Copyright By PowCoder代写 加微信 powcoder
y = x+1; goto Finish; …
• 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
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
; if (y>x)
LOAD LOAD CMPGT JUMPF
; {a = 5;} LEA
STORE ; b = 6;
R1,y[R0] R2,x[R0] R3,R1,R2 R3,skip[R0]
; R1 = 2 ;x= 2
; R3 = (y>x)
; goto skip if not (y>x)
; R1 = 5 ;a= 5
; R1 = 6 ;b= 6
{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]
{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
; {a = 5;} LEA
JUMP ; {c=7;}
else LEA STORE
; b = 6; done LEA
R1,y[R0] R2,x[R0] R3,R1,R2 R3,else[R0]
; skip the else part
; R1 = 6 ;b= 6
; R1 = 2 ;x= 2
; R3 = (y>x)
; goto else if not(y>x)
x = 2; if (y>x)
else {c = 7;}
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
;Using statement by statement style here let’s use R1 for all initialisations. If part of larger program, register variable might be better.
;Data Area. Reserve space for variables used but all except x are initialised by the code
Systems and Networks
;only variable input to this fragment
6 Control Structures 13
Solution to UA2
;Program sumsum
;This program doubles sum from initial value 1 until it exceeds limit. ;
; limit = 300
; while (sum < limit)
; { sum = sum + sum}
;Assignment to registers as follows:
; R1 = sum
; R2 = limit
; R3 for temporary boolean
; Initialise registers
LOAD R1,sum[R0]
LOAD R2,limit[R0] ;Load value of limit
;While loop
WHILE CMPLT
JUMPF R3,OUT[R0]
ADD R1,R1,R1 JUMP WHILE[R0]
;Exit loop and store result
;If sum >=limit
;exit loop
;sum = 2 x sum
;Store result
OUT STORE TRAP
R1,sum[R0]
sum DATA 1 limit DATA 300
Systems and Networks
6 Control Structures 14
;Load initial value of sum
;Program: factorial.
;This program computes n! (factorial).
; HLL algorithm
for (i = 1; i<=n; i++)
{ x = x * i; }
;Assignment to registers as follows:
; R1 = constant 1
; R5 for temporary boolean
; Initialise registers
; For loop follows FORLOOP
LEA LEA LOAD LEA
CMPGT JUMPT MUL ADD JUMP
R1,1[R0] R2,1[R0] R3,n[R0] R4,1[R0]
R5,R2,R3 R5,OUT[R0] R4,R4,R2 R2,R2,R1 FORLOOP[R0]
R4,x[R0] R0,R0,R0
;R1 = 1 (constant) ;i=1
; EXIT Loop ;x = x*i
;i = i + 1 ;Loop again
; Store x ; Finish
; Exit Loop and store result.
OUT STORE TRAP
;Data Area
n DATA x DATA
Systems and Networks
6 Control Structures 15
Solution to UA3
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com