52.223 low level programming lecturer: duncan smeed low level program control structures

34
52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Upload: vivien-watson

Post on 30-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

52.223 Low Level Programming Lecturer: Duncan Smeed

Low Level Program Control Structures

Page 2: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/2

Low Level Program Control Structures

Most computer algorithms contain decision points where code is conditionally executed depending on the status of program variables.

Conditional execution and loop constructs are available in high-level languages.

The equivalent constructs in assembly language use branch instructions.

This section introduces the various forms of IA-32 branch instructions and discusses the low level language implementation of high-level language control constructs.

Page 3: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/3

Low Level Branches

There are two general categories of low level branch:

Unconditional Transfer. The program branches to a new location in all cases; a new value is loaded into the Instruction Pointer (EIP), causing execution to continue at the new address.

Conditional Transfer. The program branches if a certain condition is true. IA-32 provides a wide range of conditional transfer instructions that may be combined to make up conditional logic structures.

Page 4: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/4

Instruction Pointer

The instruction pointer (EIP) register contains the offset in the current code segment for the next instruction to be executed.

It is advanced from one instruction boundary to the next in straightline code or it is moved ahead or backwards by a number of instructions when executing JMP, Jcc, CALL, RET, and IRET instructions.

Page 5: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/5

Unconditional transfer... JMP Instruction

SyntaxJMP <label>

ActionProgram control passes directly to the instruction located at the address <label>.

Page 6: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/6

LOOP Instruction

The LOOP instruction is the easiest way to repeat a block of statements a specific number of times. ECX is automatically used as a counter and is decremented each time the loop repeats.

SyntaxLOOP<label>

Action 1 is subtracted from the ECX. If ECX is greater than

zero control transfers to <label>. If ECX = 0 after having been decremented, no jump takes place and control passes to the instruction following the loop.

Page 7: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/7

Conditional Processing

We have seen that JMP is unconditional in that control is always transferred. Conditional transfers of control are a common requirement, typically of the form (in C):

if (x == 0) y = 20 ELSE y = 30;

To support this the IA-32 has a register called the EFLAGS REGISTER with individual bit positions assigned to control the CPU or show the results of arithmetic operations.

Page 8: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/8

EFLAGS Register

This register contains a group of status flags, a control flag, and a group of system flags.

Page 9: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/9

...Conditional Processing

For conditional processing we are concerned with the way the Zero, Carry and Sign flags show the results of boolean and comparison instructions:• The Zero bit (flag) is set to 1 if the previous instruction gave

a zero result, otherwise its 0• The Sign bit takes on the value of the MSBit of the result

(given twos complement). If it is 1 then this indicates a negative result.

• The Carry bit is set when the result of an unsigned addition is too large for the destination operand or when a subtraction requires a borrow.

• The Overflow bit is set when a signed arithmetic operation generates a result that is out of range.

Page 10: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/10

Boolean Instructions

Boolean instructions are based on boolean algebra operations. These operations allow modification of individual bits in binary numbers:Operation CommentAND Result is 1 only when both input bits are 1OR Result is 1 when either input is 1XOR Result is 1 only when the input bits differNOT Result is the reverse of the input (1 <-> 0)The first three instructions perform their operations on two operands - only one of which may be a memory operand - and place their result in the destination operand.

Syntax destination,source

AND

OR

XOR

⎨ ⎪

⎩ ⎪

⎬ ⎪

⎭ ⎪

Page 11: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/11

Conditional Jumps

These instructions jump (transfer control to a destination address) according to values in the flags register. Their general syntax is:

Jcond <label> If the condition cond is TRUE then control is

transferred to the address <label>, otherwise execution continues to the next instruction in sequence.

Page 12: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/12

...Conditional Jumps

Many of the test conditions (cond) are described in two different ways. For example, LE (less or equal) and NG (not greater) describe the same test condition

Alternate mnemonics are provided to make code more intelligible.

The terms “above” and “below” are associated with the CF flag and refer to the relation between two unsigned integer values.

The terms “greater” and “less” are associated with the SF and OF flags and refer to the relation between two signed integer values.

Page 13: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/13

...Conditional JumpsJumps Based on Unsigned Comparisons

Jcond Description Flag Condition(s) JZ Jump if Zero ZF =1 JE Jump if Equal (if op1 == op2) JNZ Jump if Not Zero ZF = 0 JNE Jump if Not Equal (if op1 != op2) JA Jump if Above CF = 0 and ZF = 0 JNBE Jump if Not Below or Equal (if op1 > op2) JAE Jump if Above or Equal CF = 0 JNB Jump if Not Below if (op1 >= op2) JB Jump if Below CF = 1 JNAE Jump if Not Above or Equal JC Jump if Carry if (op1 < op2) JBE Jump if Below or Equal CF = 1 or ZF = 1 JNA Jump if Not Above (if op1 <= op2)

Page 14: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/14

...Conditional JumpsJumps Based on Signed Comparisons

Jcond Description Flag Condition(s) JG Jump if Greater ZF = 0 and SF = OF JNLE Jump if Not Less or Equal (if op1 > op2) JGE Jump if Greater than or Equal SF = OF JNL Jump if Not Less (if op1 >= op2) JL Jump if Less SF != OF JNGE Jump if Not Greater or Equal (if op1 < op2) JLE Jump if Less or Equal ZF = 1 or SF != OF JNG Jump if Not Greater if (op1 <= op2) JS Jump if Signed SF = 1 JNS Jump if Not Signed SF = 0 JO Jump if Overflow OF = 1 JNO Jump if Not Overflow OF = 0

Page 15: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/15

Comparison Instructions: TEST and CMP

Before executing a conditional jump instruction, the flag bits must be set by executing a previous instruction. Often the instruction will be an arithmetic operation that produces some result.

Other instructions, such as TEST, permit an explicit test of a register or a memory location.

The TEST instruction performs an implied (temporary) AND on the destination operand, using the source operand. The flags are affected but neither operand is changed.Syntax TEST destination,sourceAction If any matching bit positions are set in both operands, the Zero flag is cleared. It is particularly valuable when you want to know if individual bits in an operand are set.

Page 16: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/16

...Comparison Instructions: TEST and CMP

The CMP instruction performs an implied subtraction of the source operand from the destination operand, but neither operand is actually changed.

Syntax CMP destination,sourceFlag Conditions. Generally only three flags are important

outcomes from the instruction:

After CMP Flag ResultsDestination < source CF = 1Destination = source ZF = 1Destination > source CF = 0, ZF = 0

The sense of the comparison and resultant branch is<source> cond <destination>, as in:

CMP AL,'9' ; compare AL with '9'JLE LABEL ; jump to LABEL if

; AL LE '9',i.e. AL <= '9'

Page 17: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/17

Program Control Structures

High-level languages provide constructs for conditional code execution and loops.

These constructs translate into comparison and jump instructions in assembly language.

Page 18: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/18

IF...THEN Construct

High-Level Language ConstructIF <Boolean expression> THEN

code(T)<rest of code>

Instructions such as TEST, CMP, or arithmetic operations set the CCR bits. If the result is FALSE, a branch to a label located at the <rest of code> is executed. In other words, the branch tests the condition under which the code(T) block should not be executed. Therefore, the following type of code sequence would be used:

Page 19: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/19

...IF...THEN Construct

Low-Level Language Construct<set CCR bit(s)>J!cond <endif>

code(T)

<endif> <rest of code>

Note The notation !cond means jump on NOT condition cond. For instance, if cond = GE then use the jump JL.

Page 20: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/20

IF...THEN...ELSE Construct

IF <Boolean expression> THENcode(T)

ELSEcode(F)

<rest of code>

Low-Level Language Construct<set CCR bit(s)>J!cond <else>code(T)JMP <endif>

<else> code(F)<endif> <rest of code>

Page 21: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/21

WHILE…DO Construct

WHILE <Boolean expression> DOcode...modify the expressiongo back to the WHILE...DO

<rest of code>

Low-Level Language Construct<set CCR bit(s)>

<while> J!cond <endwhile>code...modify conditionJMP <while>

<endwhile> <rest of code>

Page 22: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/22

...WHILE…DO Construct

Alternative Low-Level Language Constructs<set CCR bit(s)>JMP <whiletest>

<while> code...<set CCR bit(s)>

<whiletest> Jcond <while>

OrJMP <whiletest>

<while> code...<whiletest> <set CCR bit(s)>

Jcond <while>

Page 23: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/23

REPEAT…UNTIL Construct

REPEAT

code...

UNTIL <Boolean expression>

<rest of code>

Low-Level Language Construct

<repeat> code...

<set CCR bit(s)>

J!cond <repeat>

<rest of code>

Page 24: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/24

DO…WHILE Construct

The equivalent of the REPEAT...UNTIL Construct in C is the do...while:

docode...

while <Boolean expression><rest of code>

Low-Level Language Construct<do> code...

<set CCR bit(s)>Jcond <do><rest of code>

Page 25: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/25

FOR...DOFOR <number of iterations> DO

code...modify the iteration countergo back to the FOR...DO

<rest of code>Low-Level Language Construct

<Initialise iteration counter><for> <set CCR bit(s)>

J!cond <endfor>code...modify the iteration counterJMP <for>

<endfor> <rest of code>

Page 26: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/26

LOOP...EXIT Construct

High-level languages generally provide a variety of loop constructs. The modern trend in languages is to unify these constructs into a single, all-purpose LOOP...EXIT construct. The exit condition can be tested at any appropriate point in the code block and effect an exit if the condition is TRUE. The general form of the construct is:

LOOP

code...

EXIT WHEN <Boolean expression>

more code...

ENDLOOP

<rest of code>

Page 27: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/27

...LOOP...EXIT Construct

Low-Level Language Construct

<loop> code...

<set CCR bit(s)>

Jcond <endloop>

more code...

JMP <loop>

<endloop> <rest of code>

Page 28: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/28

...LOOP...EXIT Construct

The nearest thing to this form of construct in C is the conditional execution of a break statement:

do

{

c = getchar();

if (c == '.') break;

putchar(c);

}

while (c != EOF);

Page 29: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/29

LOOP...CONTINUE Construct

C is one of the few high-level languages that provide a mechanism to return to the start of a loop prematurely.

The nearest thing to this form of construct in C is the conditional execution of a continue statement:

do{

c = getchar();if (c == '.') continue;putchar(c);

}while (c != EOF);

Page 30: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/30

CASE Construct

This allows a multiway branch by comparing a single value to a list of values. Similar to nested if..then..else constructs.

Low-Level Language Construct<case> <set CCR bit(s)>

J!cond <case02><first case statement>JMP <endcase>

<case02><set CCR bit(s)>J!cond <case...><third case statement>JMP <endcase>

<case...> <repeats as above>...

<endcase> <rest of code>

Page 31: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/31

...CASE Construct

A CASE construct in the style of C:switch (argc){

case 1: /* No arguments so...*/<prompt statements>; break;

case 3: /* Arguments given so...*/<assign statements>; break;

default: /* Incorrect arguments */<default statements>; break;

}

Page 32: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/32

...CASE Construct

Low-Level Language Equivalent

<case> MOV AL,argcCMP AL,1JNE <case02><prompt statement code>JMP <endcase>

<case02> CMP AL,3JNE <default><assign statement code>JMP <endcase>

<default> <default statement code><endcase> <rest of code>

Page 33: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/33

Low Level Conditional Loops LOOPZ / LOOPE

This loop while zero (equal) instruction is related to the LOOP instruction. It has the following syntax:

SyntaxLOOPZ <label>LOOPE <label>

Action 1 is subtracted from ECX. If ECX is greater than zero

and the Zero flag is set control transfers to <label>. If ECX = 0 after having been decremented or the Zero flag is clear, no jump takes place and control passes to the instruction following the loop.

Page 34: 52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures

Low Level Program Control Structures52223_09/34

...Low Level Conditional Loops LOOPNZ / LOOPNE

This loop while not zero (not equal) instruction is the reverse of LOOPZ. It has the following syntax:

SyntaxLOOPNZ <label>LOOPNE <label>

Action 1 is subtracted from ECX. If ECX is greater than zero

and the Zero flag is clear control transfers to <label>. If ECX = 0 after having been decremented or the Zero flag is set, no jump takes place and control passes to the instruction following the loop.