340 likes | 463 Views
52.223 Low Level Programming Lecturer: Duncan Smeed. Low Level Program Control Structures. Low Level Program Control Structures. Most computer algorithms contain decision points where code is conditionally executed depending on the status of program variables.
E N D
52.223 Low Level ProgrammingLecturer: Duncan Smeed Low Level Program Control Structures
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. Low Level Program Control Structures
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. Low Level Program Control Structures
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. Low Level Program Control Structures
Unconditional transfer... JMP Instruction SyntaxJMP <label> ActionProgram control passes directly to the instruction located at the address <label>. Low Level Program Control Structures
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. Low Level Program Control Structures
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. Low Level Program Control Structures
EFLAGS Register This register contains a group of status flags, a control flag, and a group of system flags. Low Level Program Control Structures
...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. • TheOverflow bit is set when a signed arithmetic operation generates a result that is out of range. Low Level Program Control Structures
Boolean Instructions • Boolean instructions are based on boolean algebra operations. These operations allow modification of individual bits in binary numbers: 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. Syntaxdestination,source Low Level Program Control Structures
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. Low Level Program Control Structures
...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. Low Level Program Control Structures
...Conditional Jumps Low Level Program Control Structures
...Conditional Jumps Low Level Program Control Structures
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. SyntaxTEST destination,source Action 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. Low Level Program Control Structures
...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. SyntaxCMP destination,source Flag Conditions. Generally only three flags are important outcomes from the instruction: 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' Low Level Program Control Structures
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. Low Level Program Control Structures
IF...THEN Construct High-Level Language Construct IF <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: Low Level Program Control Structures
...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. Low Level Program Control Structures
IF...THEN...ELSE Construct IF <Boolean expression> THEN code(T) ELSE code(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> Low Level Program Control Structures
WHILE…DO Construct WHILE <Boolean expression> DO code... modify the expression go back to the WHILE...DO <rest of code> Low-Level Language Construct <set CCR bit(s)> <while> J!cond <endwhile> code... modify condition JMP <while> <endwhile> <rest of code> Low Level Program Control Structures
...WHILE…DO Construct Alternative Low-Level Language Constructs <set CCR bit(s)> JMP <whiletest> <while> code... <set CCR bit(s)> <whiletest> Jcond <while> Or JMP <whiletest> <while> code... <whiletest> <set CCR bit(s)> Jcond <while> Low Level Program Control Structures
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> Low Level Program Control Structures
DO…WHILE Construct • The equivalent of the REPEAT...UNTIL Construct in C is the do...while: do code... while <Boolean expression> <rest of code> Low-Level Language Construct <do> code... <set CCR bit(s)> Jcond <do> <rest of code> Low Level Program Control Structures
FOR...DO FOR <number of iterations> DO code... modify the iteration counter go 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 counter JMP <for> <endfor> <rest of code> Low Level Program Control Structures
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> Low Level Program Control Structures
...LOOP...EXIT Construct Low-Level Language Construct <loop> code... <set CCR bit(s)> Jcond <endloop> more code... JMP <loop> <endloop> <rest of code> Low Level Program Control Structures
...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); Low Level Program Control Structures
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); Low Level Program Control Structures
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> Low Level Program Control Structures
...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; } Low Level Program Control Structures
...CASE Construct Low-Level Language Equivalent <case> MOV AL,argc CMP AL,1 JNE <case02> <prompt statement code> JMP <endcase> <case02> CMP AL,3 JNE <default> <assign statement code> JMP <endcase> <default> <default statement code> <endcase> <rest of code> Low Level Program Control Structures
Low Level Conditional LoopsLOOPZ / 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. Low Level Program Control Structures
...Low Level Conditional LoopsLOOPNZ / 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. Low Level Program Control Structures