Presentation on theme: "LAB Flow Control Instructions"— Presentation transcript:
1 LAB Flow Control Instructions CHAPTER 3LABFlow Control Instructions
2 OverviewFor assembly language programs to carry out useful tasks, there must be a way to make decisions and repeat sections of code –> jump and loop instructions.The jump and loop instructions transfer control to another part of the program.This transfer can be unconditional or can depend on a particular combination of status flag settings.
3 1. CONDITIONAL JUMPSJNZ is an example of a Conditional Jumps Instruction. The syntax is:Jxxx dest_labelIf the condition for the jump is true, the next instruction to be executed is the one at destination_label, which may precede or follow the jump instruction itself.If the condition is false, the instruction immediately following the jump is done next.For JNZ, the condition is that the result of the previous operation is not zero.
4 How the CPU Implements a Conditional Jumps The CPU looks at the FLAGS register.If the conditions for the jump (expressed as a combination of status Flag setting) are true, the CPU adjusts the IP to point to the destination label, so that the instruction at this label will be done next.If the jump condition is false, then IP is not altered; this mean that the next instruction in line will de done.
5 The CMP instructionThe jump condition is often provided by the CMP (compare) instruction .CMP dest, sourceThis instruction compared destination and source by computing destination contents minus source content.The result is not stored, but the flags are affected.The operand of CMP may not both be memory location. Destination may not be a constant.Note: CMP is just like SUB, except that destination is not changed.
6 The CMP instruction Eg: CMP AX, BX JG BELOW Where AX = 7FFFh and BX = 0001h.Result of CMP AX, BX is 7FFFh – 0001h = 7FFEh.AX is greater than BX (in signed sense), then JG (jump if greater than) transfer to BELOW.From table 6.1 shows that the jump condition for JG is satisfied -> ZF=SF=OF = 0, so control transfer to label BELOW.
7 Signed VS Unsigned Jumps Eg: AX = 7FFFh and BX = 8000h, and executeCMP AX, BXJA BELOWeven though 7FFFh > 8000h in signed sense, program does not jump to BELOW.Reason: 7FFFh < 8000h in unsigned sense, and we are using the unsigned jump JA.
8 2. The JMP InstructionThe JMP (jump) instruction causes an unconditional transfer of control (unconditional jump). The syntax isJMP destinationWhere destination is usually a label in the same segment as the JMP itself.Eg:TOP:; body of the loopMOV CX, 5DEC CX ; decrement counterJNZ BOTTOM ; keep looping if CX>0JMP EXITBOTTOM:JMP TOPEXIT:MOV AX, BX
9 3. HIGH-LEVEL LANGUAGE STRUCTURES BRANCHING STRUCTURES – enable a program to take different paths, depending on conditions. We’ll look at 3 structures:IF-THENIn pseudocodeIF condition is trueTHENexecute true-branch statementsEND_IFThe condition is an expression that is true or false. If true, the true-branch statements are executed. If false, nothing is done, and the program goes on to whatever follows.
10 Example: IF-THEN Replace the number in AX by its absolute value. Sol: A pseudocode algorithm isIF AX < 0THENreplace AX by –AXEND IFIt can be coded as follows:; if AX < 0CMP AX, 0 ; AX < 0?JNL END_IF ; no, exit;thenNEG AX ; yes, change signEND_IFNote: JNL (jump if not less), that is, if AX not less than 0, jump to label END_IF.
11 IF-THEN-ELSEIF condition is trueTHENexecute true-branch statementsELSEexecute false-branch statementsEND_IFIf condition is true, the true-branch statements are executed. If condition is false, the false-branch statements are executed
12 Example: IF-THEN-ELSE Suppose AL and BL contain extended ASCII characters. Display the one that comes first in the character sequence.Sol: IF AL <= BLTHENdisplay the character in ALELSEdisplay the character in BLEND_IF
13 MOV AH, 2 ; prepare to display It can be coded as follows:MOV AH, 2 ; prepare to display;if AL <= BLCMP AL, BL ; AL <= BL?JNBE ELSE_ ; no, display char in BL; AL <= BL;thenMOV DL, AL ; move char to be displayedJMP DISPLAY ; go to display; BL < ALELSE_:MOV DL, BLDISPLAY:INT 21h ; display itEND_IFNote: the label ELSE_ is used because ELSE is a reserved word.The condition AL <= BL is expressed by CMP AL, BL.
14 CASEA CASE is multiway branch structure that tests a register, variable, or expression for a particular values or a range of values. The general form is as follows:CASE expressionvalue_1: statements_1value_2: statements_2.value_n: statements_nEND CASE.In this structure, expression is tested; if its value is a member of the set values_i, then statements_i are executed. We assume that sets values-1,…., values_n are disjoint.
15 Example: CASEIf AX contains a negative number, put -1 in BX; if AX contains 0, put 0 in BX; if AX contains a positive number, put 1 in BX.Sol: CASE AX< 0: put -1 in BX= 0: put 0 in BX> 0: put 1 in BXEND CASE
16 It can be coded as follows: ; case AXCMP AX, 0 ; test AXJL NEGATIVE ; AX < 0JE ZERO ; AX = 0JG POSITIVE ; AX > 0NEGATIVE:MOV BX, -1 ; put -1 in BXJMP END_CASE ; and exitZERO:MOV BX, 0 ; put 0 in BXPOSITIVE:MOV BX, 1 ; put 1 in BXEND_CASE:Note: only one CMP is needed, because jump instruction do not affect the flags.
17 Exercise:Sketch the flowchart for IF-THEN, IF-THEN-ELSE and CASE structure.Sketch the flowchart for each previous example.Prob: If AL contains 1 or 3, display “o”; if AL contains 2 or 4, display “e”. Write the high level language pseudocode and code in Assembly language.