程序代写代做代考 assembly Java assembler Microsoft PowerPoint – SN-2017-Sec06.pptx

Microsoft PowerPoint – SN-2017-Sec06.pptx

6. Control Structures

Systems and Networks 6 Control Structures 2

The goto Statement
• Most older programming languages (and many new ones) have a goto statement:

x = 37;
y = x+1;
goto Finish;

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 3

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 language

– 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 4

The JUMP Instructions
• JUMP tells the machine unconditionally to go to an instruction somewhere else,

instead of the next one.

• 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.

JUMP loop[R0] ; goto loop
lab ADD R2,R5,R6 ; R2 = R5 + R6


loop SUB R3,R7,R8

Systems and Networks 6 Control Structures 5

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 Finish statement can be

implemented using a compare and a conditional jump.

• We now look at how the other constructs can be implemented.

; Assume x is in R3 and y in R4
CMPEQ R2,R3,R4 ;R2:=(x=y)
JUMPT R2,loop[R0] ;if (R2<>0)goto loop


loop SUB R3,R7,R8

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 assembly

language – you just have even more instructions in a section of code!

Systems and Networks 6 Control Structures 7

Systems and Networks 6 Control Structures 8

Example of if

x = 2
if (y>x)

{a = 5;}
b = 6;

; x = 2;
LEA R1,2[R0] ; R1 = 2
STORE R1,x[R0] ; x = 2

; if (y>x)
LOAD R1,y[R0] ; R1 = y
LOAD R2,x[R0] ; R2 = x
CMPGT R3,R1,R2 ; R3 = (y>x)
JUMPF R3,skip[R0] ; goto skip if not (y>x)

; {a = 5;}
LEA R1,5[R0] ; R1 = 5
STORE R1,a[R0] ; a = 5

; b = 6;
skip LEA R1,6[R0] ; R1 = 6

STORE R1,b[R0] ; b = 6

IT Computer Systems 6 Control Structures 9

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]

else instructions for statement 2
done instructions for statement 3

IT Computer Systems 6 Control Structures 10

Example of if-then-else

x = 2;
if (y>x)

{a = 5;}
else {c = 7;}
b = 6;

; x = 2;
LEA R1,$0002[R0] ; R1 = 2
STORE R1,x[R0] ; x = 2

; if (y>x)
LOAD R1,y[R0] ; R1 = y
LOAD R2,x[R0] ; R2 = x
CMPGT R3,R1,R2 ; R3 = (y>x)
JUMPF R3,else[R0] ; goto else if not(y>x)

; {a = 5;}
LEA R1,$0005[R0] ; R1 = 5
STORE R1,a[R0] ; a = 5
JUMP done[R0] ; skip the else part

; {c=7;}
else LEA R1,$0007[R0] ;R1 = 7

STORE R1,c[R0] ; c = 7
; b = 6;
done LEA R1,$0006[R0] ; R1 = 6

STORE R1,b[R0] ; b = 6

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:

– and

Systems and Networks 6 Control Structures 11

while (x