Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures."— Presentation transcript:

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

2 52223_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.

3 Low Level Program Control Structures 52223_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.

4 Low Level Program Control Structures 52223_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.

5 Low Level Program Control Structures 52223_09/5 Unconditional transfer... JMP Instruction Syntax JMP Action Program control passes directly to the instruction located at the address.

6 Low Level Program Control Structures 52223_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. Syntax LOOP Action  1 is subtracted from the ECX. If ECX is greater than zero control transfers to. If ECX = 0 after having been decremented, no jump takes place and control passes to the instruction following the loop.

7 Low Level Program Control Structures 52223_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.

8 Low Level Program Control Structures 52223_09/8 EFLAGS Register This register contains a group of status flags, a control flag, and a group of system flags.

9 Low Level Program Control Structures 52223_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.

10 Low Level Program Control Structures 52223_09/10 Boolean Instructions  Boolean instructions are based on boolean algebra operations. These operations allow modification of individual bits in binary numbers: OperationComment ANDResult is 1 only when both input bits are 1 ORResult is 1 when either input is 1 XORResult is 1 only when the input bits differ NOTResult 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

11 Low Level Program Control Structures 52223_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  If the condition cond is TRUE then control is transferred to the address, otherwise execution continues to the next instruction in sequence.

12 Low Level Program Control Structures 52223_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.

13 Low Level Program Control Structures 52223_09/13...Conditional Jumps

14 Low Level Program Control Structures 52223_09/14...Conditional Jumps

15 Low Level Program Control Structures 52223_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 TESTdestination,source ActionIf 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.

16 Low Level Program Control Structures 52223_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 CMPdestination,source Flag Conditions. Generally only three flags are important outcomes from the instruction: After CMPFlag Results Destination < sourceCF = 1 Destination = sourceZF = 1 Destination > sourceCF = 0, ZF = 0 The sense of the comparison and resultant branch is cond, as in: CMPAL,'9'; compare AL with '9' JLELABEL; jump to LABEL if ; AL LE '9',i.e. AL <= '9'

17 Low Level Program Control Structures 52223_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.

18 Low Level Program Control Structures 52223_09/18 IF...THEN Construct High-Level Language Construct IF THEN code(T)  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 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:

19 Low Level Program Control Structures 52223_09/19...IF...THEN Construct Low-Level Language Construct J!cond code(T) NoteThe notation !cond means jump on NOT condition cond. For instance, if cond = GE then use the jump JL.

20 Low Level Program Control Structures 52223_09/20 IF...THEN...ELSE Construct IF THEN code(T) ELSE code(F) Low-Level Language Construct J!cond code(T) JMP code(F)

21 Low Level Program Control Structures 52223_09/21 WHILE…DO Construct WHILE DO code... modify the expression go back to the WHILE...DO Low-Level Language Construct J!cond code... modify condition JMP

22 Low Level Program Control Structures 52223_09/22...WHILE…DO Construct Alternative Low-Level Language Constructs JMP code... Jcond Or JMP code... Jcond

23 Low Level Program Control Structures 52223_09/23 REPEAT…UNTIL Construct REPEAT code... UNTIL Low-Level Language Construct code... J!cond

24 Low Level Program Control Structures 52223_09/24 DO…WHILE Construct  The equivalent of the REPEAT...UNTIL Construct in C is the do...while: do code... while Low-Level Language Construct code... Jcond

25 Low Level Program Control Structures 52223_09/25 FOR...DO FOR DO code... modify the iteration counter go back to the FOR...DO Low-Level Language Construct J!cond code... modify the iteration counter JMP

26 Low Level Program Control Structures 52223_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 more code... ENDLOOP

27 Low Level Program Control Structures 52223_09/27...LOOP...EXIT Construct Low-Level Language Construct code... Jcond more code... JMP

28 Low Level Program Control Structures 52223_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);

29 Low Level Program Control Structures 52223_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);

30 Low Level Program Control Structures 52223_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 J!cond JMP J!cond JMP...

31 Low Level Program Control Structures 52223_09/31...CASE Construct  A CASE construct in the style of C: switch (argc) { case 1: /* No arguments so...*/ ; break; case 3: /* Arguments given so...*/ ; break; default: /* Incorrect arguments */ ; break; }

32 Low Level Program Control Structures 52223_09/32...CASE Construct Low-Level Language Equivalent MOVAL,argc CMPAL,1 JNE JMP CMPAL,3 JNE JMP

33 Low Level Program Control Structures 52223_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: Syntax LOOPZ LOOPE Action  1 is subtracted from ECX. If ECX is greater than zero and the Zero flag is set control transfers to. 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.

34 Low Level Program Control Structures 52223_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: Syntax LOOPNZ LOOPNE Action  1 is subtracted from ECX. If ECX is greater than zero and the Zero flag is clear control transfers to. 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.


Download ppt "52.223 Low Level Programming Lecturer: Duncan Smeed Low Level Program Control Structures."

Similar presentations


Ads by Google