180 likes | 298 Views
Flow Control Instructions. Jump Instructions (1). The unconditional jump instruction , branches to a new address in the program code. JMP NEXT ; jumps to address of "NEXT"
E N D
Jump Instructions (1) • The unconditional jump instruction, branches to a new address in the program code. JMP NEXT ; jumps to address of "NEXT" • Conditional jump instructions test the condition of one or two FLAGS in the flags register, then "jump" to a new location in the program based on the result of the test. • Example: TOP: SUB AX, BX ;AX = AX - BX JC EXIT: ;IF AX < BX then jump to EXIT JMP TOP ;else, repeat until AX < BX EXIT: MOV AX, 4C00H INT 21H
Jump Instructions (2) • CMP Instruction (1)The CMP instruction has the same effect on the flags as SUB, but the value of the destination operand is NOT changed, i.e. it sets the flags in the FLAGS register the same as if subtraction had been performed. (2)CMP is frequently used to determine relative values of operands without changing the values • Example: MOV CX, 5 TOP_2: CMP CX, 0 ; CX = 0 ? JE EXIT_: ; IF CX = 0, then EXIT_ SUB AX, BX ; AX = AX - BX DEC CX JMP TOP_2 ;else, repeat until CX = 0 EXIT_: MOV AX, 4C00H INT 21H
Examples of Flow Control Using Jump Instructions (1) • IF-THEN-ELSE Structures • START: • MOV AH, 1 • INT 21H ; input AL • CMP AL, 'Q' ; if AL = 'Q' • JE EXIT ; then, branch to EXIT • MOV AH, 2 ; else output AL • MOV DL, AL • INT 21H • JMP START ; repeat • EXIT: • MOV AX, 4C00H • INT 21H
Examples of Flow Control Using Jump Instructions (2) • IF-THEN-ELSE • If AL > BL, display BL • else display AL • CMP AL, BL ; AL > BL? • JG ELSE_ ; yes, go to "else" • MOV DL, BL ; then, result = BL • JMP DISPLAY • ELSE_: ; else, AL >=BL • MOV DL, AL ; result = AL • DISPLAY: ; display result • MOV AH, 2 • INT 21H
Examples of Flow Control Using Jump Instructions (3) • CASE Structure • MOV AH, 1 • INT 21H • CMP AL, 'A' • JE CASE_A • CMP AL, 'B' • JE CASE_B • CMP AL, 'C' • JE CASE_C • CMP AL, 'D' • JE CASE_D • CASE_A: • ; code for case A goes here • JMP NEXT_ • CASE_B: • ; code for case B goes here • JMP NEXT_ • CASE_C: • ; code for case C goes here • JMP NEXT_ • CASE_D: • ; code for case D goes here • NEXT_: • ; code continues here
Examples of Flow Control Using Jump Instructions (4) • AND Conditions: Is AL >= 30H and <= 39H? • I.e. is it the code for a decimal digit? • MOV AH, 1 • INT 21H • CMP AL, 30H ; AL >= 30h? • JL ERROR ; no, error • CMP AL, 39H ; AL <= 39h? • JG ERROR ; no, error • ... ; continue: AL >= 30h and AL <= 39h • ERROR: • ... ; code for handling an error
Examples of Flow Control Using Jump Instructions (5) • OR Conditions Input until the character is Q or q. • START: • MOV AH, 1 • INT 21H • CMP AL, 'Q' • JE EXIT_ ; if AL = 'Q', then exit • CMP AL, 'q' • JNE START ; if AL != 'Q' and AL != 'q', repeat input • EXIT_: ; if AL = 'Q' or AL = 'q' • MOV AX, 4COOH • INT 21H
Examples of Flow Control Using Jump Instructions (6) • FOR Loops • Input the value of AL. • For I = 1 to 10 output this value+I. • START: • MOV AH, 1 • INT 21H • MOV CX, 10 • TOP_: • JCXZ EXIT_ ; If CX = 0, exit • INC AL • MOV DL, AL • MOV AH, 2 • INT 21H • LOOP TOP_ ; repeats for 10 times • EXIT_: • MOV AX, 4C00H • INT 21H
Examples of Flow Control Using Jump Instructions (7) • WHILE Loops • input Al • while AL != ‘Q’ • output AL • input Al • end while • START: • MOV AH, 1 • INT 21H • CMP AL, 'Q' • JE EXIT_ ; repeat while AL != 'Q' • MOV DL, AL • MOV AH, 2 • INT 21H • JMP start • EXIT_: • MOV AX, 4C00H • INT 21H
Examples of Flow Control Using Jump Instructions (8) • REPEAT-UNTIL Loops • Set Dl to ‘A’ • TOP: output DL • add 1 to DL • repeat top until DL = ‘[‘ (ascii code 5Bh) • MOV AH, 2 • MOV DL, 'A' • TOP_: • INT 21H • INC DL • CMP DL, '[' • JNE top_ ; repeat until DL = '[' • EXIT_: • MOV AX, 4C00H • INT 21H
Loop Instruction • Used to implement "for loops" more conveniently than above. • Decrements CX register and jumps to specified label as long as CX != 0 after being decremented. • Example: Input a number, and display that number of asterisks .586 ; required for MOVSX or MOVZX instructions START: MOV DL, '*" MOV AH, 1 INT 21H SUB AL, 30H MOVSX CX, AL ; sign extend into CX register MOV AH, 2 TOP_: INT 21H ; display asterisks LOOP TOP_ ; repeats CX times EXIT_: MOV AX, 4C00H INT 21H
MOVSX Instruction Move with sign extension. Copies a byte or word from a source operand to a register, and sign extends into the upper half of the destination. This is used to copy an 8-bit or 16-bit operand into a larger destination.
JMP Instructions • For signed arithmetic J[N] {G | L | Z} [E] eg: JG, JGE, JNZ, JLE
Example on JE Count the number of characters entered which terminate in a CR (0DH), don’t count CR. Use BL to store that number. MOV BL, 0 REPEAT: MOV AH, 1 INT 21H CMP AL, 0DH JE OUT INC BL JMP REPEAT OUT: …
Example on JGE To put the absolute value of AX into AX. CMP AX, 0 JGE OUT ; jump if greater than or equal NEG AX OUT: …
Example on JE & JL If AX < 0, output – ; If AX = 0, output 0; If AX > 0, output + CMP AX, 0 JE zerocase JL lesscase MOV DL, ‘+’ JMP display zerocase: MOV DL, 0 JMP display lesscase: MOV DL, ‘-’ display: MOV AH, 2 INT 21H
Example on JCXZ JCXZ – Jump if cx is zero, cx used as a counter Assume that CX contains some number and you want to Output this number of asterisks JCXZ OUT place: MOV AH, 2 MOV DL, ‘*’ INT 21H LOOP place OUT: …