300 likes | 407 Views
Chapter 7 Program Logic And Control. Instructions introduced in this chapter :. Compare Operations. Transfer Operations. Logical Operations. Shift and Rotate. CMP TEST. CALL JMP LOOP RETn Jnnn. AND NOT OR XOR. SAR/SHR SAL/SHL RCR/ROR RCL/ROL. Conditional Jump: JNE, JL.
E N D
Chapter 7 Program Logic And Control
Instructions introduced in this chapter: Compare Operations Transfer Operations Logical Operations Shift and Rotate CMP TEST CALL JMP LOOP RETn Jnnn AND NOT OR XOR SAR/SHR SAL/SHL RCR/ROR RCL/ROL Conditional Jump: JNE, JL
The JMP Instruction Short Near Far -128 to 127 Same segment -32,768 to 32,767 Same segment Over 32K or in Another segment Instruction JMP Jnnn LOOP CALL Yes Yes Yes N/A Yes Yes (80386+) No yes Yes No No yes The format for JMP: [label:] JMP short/near/far address A jump operation reaches a short address by a 1-byte offset and reaches a near address by a one- or two-word offset. A far address is reached by a segment address and an offset; CALL is the normal instruction for this purpose.
The JMP Instruction Short and Near Jumps: A JMP operation to a label within -128 to 127 bytes is a short jump. The assembler generates one byte for the operation (EB) and one byte for the operand, which acts as an offset added to the IP when executing. A jump over -128 to +127 bytes and within 32K becomes a near jump. The assembler generates machine code (E9)+ a 2-byte (8086/80286) or 4-byte operand (80386+). Backward and Forward Jumps: L10: ;jump address … JMP L10;backward jump … JMP L20;forward jump … L20: ;jump address EBxx EBxx90 90 is machine code for NOP: no operation
The JMP Instruction page 60,132 TITLE A07JUMP (COM) Illustration of JMP for looping .MODEL SMALL 0000 .CODE ORG 100H 0100 A10MAIN PROC NEAR 0100 B8 0000 MOV AX,00 ;Initialize AX and 0103 BB 0000 MOV BX,00 ; BX to zero, 0106 B9 0001 MOV CX,01 ; CX to 01 0109 A20: 0109 83 C0 01 ADD AX,01 ;Add 01 to AX 010C 03 D8 ADD BX,AX ;Add AX to BX 010E D1 E1 SHL CX,1 ;Double CX 0110 EB F7 JMP A20 ;Jump to A20 label 0112 A10MAIN ENDP END A10MAIN Use DEBUG to trace the program and observe the effect of execution on AX, BX, CX and IP.
The LOOP Instruction The format for LOOP is: [label:] LOOP short address page 60,132 TITLE A07LOOP (COM) Illustration of LOOP .MODEL SMALL 0000 .CODE ORG 100H 0100 A10MAIN PROC NEAR 0100 B8 0000 MOV AX,0 ;Initialize AX and 0103 BB 0000 MOV BX,0 ; BX to zero, 0106 BA 0001 MOV DX,1 ; DX to 01 0109 B9 0008 MOV CX,8 ; CX for 8 loops 010C A20: 010C 40 INC AX ;Add 01 to AX 010D 03 D8 ADD BX,AX ;Add AX to BX 010F D1 E2 SHL DX,1 ;Double DX 0111 E2 F9 LOOP A20 ;Decrement CX, ; loop if nonzero 0113 B8 4C00 MOV AX,4C00H ;End processing 0116 CD 21 INT 21H 0118 A10MAIN ENDP END A10MAIN Use DEBUG to trace through the eight loops and observe AX, BX, CX, DX and IP.
The Flags Register The Flags register contains 16 bits to indicate the effect of instructions’ operations. Bit number: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Flag: O D I T S Z A P C CF (Carry Flag) contains a carry out of the high-order bit following an unsigned arithmetic operation and some shift and rotate operations. JC and JNC test this flag. PF (Parity Flag) contains a check of low order 8 bits after an arithmetic operation. PF = 0 if even and PF = 1 if odd. JP and JPO test this flag.
AF (Auxiliary Carry Flag) is set when 1-byte arithmetic operation causes a carry out of bit 3 into bit 4. ZF (Zero Flag) JE and JZ test the ZF. SF (Sign Flag) SF = 0 when (+ ) and SF = 1 when (-), JG and JL test the SF. TF (Trap Flag) cause the processor to execute in single-step mode when debugging. INT 03 used to set the TF. IF (Interrupt Flag) external interrupt enabled = 1, disabled = 0. set by STI and cleared by CLI.
DF (Direction Flag) determine the direction of data transfer. DF = 0 => left-to-right data transfer for string operation. OF (Overflow Flag) a carry out of leftmost sign after signed arithmetic Operation. JO and JNO test the OF.
The CMP Instruction [label:] CMP register/memory, register/memory/immediate The result of a CMP operation affects the AF,CF,OF, PF, SF and ZF flags CMP DX, 00 ;DX = zero? JE L10 ;if yes, jump to L10 … … L10: … ;jump point if DX = zero If DX = 0, CMP set ZF to 1. JE tests only the ZF. Because ZF = 1, JE Transfers control (jumps) to the address indicated by operand L10.
Conditional JUMP Instruction Conditional jump instructions transfer control depending on settings in the Flags register. [label:] Jnnn short-address DEC CX JNZ A20 Just as for JMP and LOOP, the machine code operand for JNZ contains the Distance from the end of the instruction to the address of A20. example 1 example 2 .386 .386 CMP BX, CX CMP BX, CX JE L20 JE SHORT L20 4-byte machine code 2-byte code
Signed and Unsigned Data An unsigned numeric item (logical data) treats all bits as data bits. A signed numeric item (arithmetic data) treats the leftmost bit as a sign. CX =1100 0110 DX =0001 0110 CMP CX, DX If data are unsigned, CX is larger and if data are signed, CX is smaller, Jumps based on unsigned (Logical) Data JE/JZ jump equal or jump zero ZF JNE/JNZ jump not equal or jump not zero ZF JA/JNBE jump above or jump not below/equal CF, ZF JAE/JNB jump above/equal or jump not below CF JB/JNAE jump below or jump not above/equal CF JBE/JNA jump below/equal or jump not above AF, CF
Jumps based on signed (Arithmetic) Data JE/JZ jump equal or jump zero ZF JNE/JNZ jump not equal or jump not zero ZF JG/JNLE jump greater or jump not less/equal OF, SF, ZF JGE/JNL jump greater/equal or jump not less OF, SF JL/JNGE jump less or jump not greater/equal OF, SF JLE/JNG jump less/equal or jump not greater OF, SF, ZF Special Arithmetic Tests JCXZ jump if CX is zero none JC jump carry CF JNC jump no carry CF JO jump overflow OF JNO jump no overflow OF JP/JPE jump parity or jump parity even PF JNP/JPO jump no parity or jump parity odd PF JS jump sign (negative) SF JNS jump no sign (positive) SF
Testing Multiple Conditions Example 1: any condition true (OR) Example 2: all condition true (AND) CMP AL, BL CMP AL, BL JE equal JNE not-equal CMP AL, BH CMP AL, BH JE equal JNE not-equal CMP AL, CL CMP AL, CL JE equal JNE not-equal Not-equal: … equal:… … … equal: … not-equal:… In example 1: any test equal will jump to equal label. In example 2: all test need be equal to execute instructions in equal label.
CALL and RETn Operations: [label:] CALL procedure-name [label:] RET[n] [immediate] Near Call and Return: 1a. Push content of IP into stack, decrement SP by 2 1b. Insert the offset of the called procedure into IP 2. Pop old IP value from the stack, increment SP by 2
page 60,132 TITLE A07CALLP (EXE) Calling procedures .MODEL SMALL .STACK 64 0000 .DATA ;------------------------------------------- 0000 .CODE 0000 A10MAIN PROC FAR 0000 E8 0005 (0008R) CALL B10 ;Call B10 ; ... 0003 B8 4C00 MOV AX,4C00H ;End processing 0006 CD 21 INT 21H 0008 A10MAIN ENDP ;------------------------------------------- 0008 B10 PROC NEAR 0008 E8 0001 (000CR) CALL C10 ;Call C10 ; ... 000B C3 RET ;Return to 000C B10 ENDP ; caller ;----------------------------------------- 000C C10 PROC NEAR ; ... 000C C3 RET ;Return to 000D C10 ENDP ; caller ;----------------------------------------- END A10MAIN
CALL B10 (push 0003): 003E 0300 <== SP = 003E 003C xxxx 003A xxxx CALL C10 (push 000B): 003E 0300 003C 0B00 <== SP = 003C 003A xxxx RET (pop 000B): 003E 0300 <== SP = 003E 003C 0B00 003A xxxx <== SP = 0040H RET (pop 0003): 003E 0300 64 003C 0B00 003A xxxx The Effect of Program Execution on the Stack
Passing Parameters: When calling a procedure we need to pass parameters. Passing parameter by value Example 1 pass values in registers: MOV AX, MULTIPLICAND MOV BX, MULTIPLIER CALL M30MULT … M30MULT PROC NEAR MUL BX RET M30MULT ENDP Product in the DX:AX pair
Example 2 pass values in stack PUSH MULTIPLICAND PUSH MULTIPLIER CALL M30MULT … M30MULT PROC NEAR PUSH BP MOV BP, SP MOV AX, [BP+6] MUL WORD PTR [BP+4] POP BP RET 4 M30MULT ENDP MULTIPLICAND [BP+6] MULTIPLIER [BP+4] Return address [BP+2] BP value <= SP,BP RET 4 performs two functions: 1. load return address into IP and increase SP by 2 2. add 4 to SP, effectively removing the two parameters from the stack
Passing parameters by reference Example 3 addresses in registers: LEA BX, MULTIPLICAND LEA SI, MULTIPLIER CALL M30MULT … M30MULT PROC NEAR MOV AX, [BX] MUL WORD PTR [SI] RET M30MULT ENDP
Example 4 addresses in stack .386 ; needed for nest two pushes PUSH OFFSET MULTIPLICAND PUSH OFFSET MULTIPLIER CALL M30MULT … M30MULT PROC NEAR PUSH BP MOV BP, SP MOV BX, [BP+6] MOV DI, [BP+4] MOV AX, [BX] MUL WORD PTR [DI] POP BP RET 4 M30MULT ENDP
BOOLEAN OPERATIONS [label:] operation register/memory, register/memory/immediate AND: AND BL,0FH AND BL,00H ;clear BL OR: OR DX, DX ;test DX JZ exit ;jump if zero OR DX, DX JS exit ;jump if negative XOR: XOR BL,BL ;clear BL TEST: TEST CX, 0FFH ; dose CX contain JZ exit ; a zero value? (CX=0?) If any matching bits are both 1, TEST clears the ZF.
TEST BL, 00000001B ;does BL contain JNZ exit ; an odd number? TEST CL,11110000B ;any of the leftmost JNZ exit ; bits in CL not zero? NOT: reverse bits, 0 <=>1 [label:] NOT register/memory uppercase lowercase Letter A:0100 0001 letter a: 0110 0001 Letter Z:0101 1010 letter z: 0111 1010 XOR AH, 00100000B converts uppercase to lowercase
TITLE A07CASE (COM) Change uppercase to lowercase .MODEL TINY .CODE ORG 100H BEGIN: JMP A10MAIN ; -------------------------------------------------- CONAME DB 'LASER-12 SYSTEMS', '$' ; -------------------------------------------------- A10MAIN PROC NEAR LEA BX,CONAME+1 ;1st char to change MOV CX,15 ;No. of chars to change A20: MOV AH,[BX] ;Character from CONAME CMP AH,41H ;Is it JB A30 ; upper CMP AH,5AH ; case JA A30 ; letter? XOR AH,00100000B ;Yes, convert MOV [BX],AH ;Restore in CONAME A30: INC BX ;Set for next char LOOP A20 ;Loop 15 times ;Done, MOV AH,09H ; display LEA DX,CONAME ; CONAME INT 21H MOV AX,4C00H ;End processing INT 21H A10MAIN ENDP END BEGIN
Exercise: Write a program to display two data items defined as: STUDENTNO DB ‘f9261xx’, 0AH, 0DH input your real student number STUDENTNAME DB ‘TUNG-HAI LIN’,’$’ your name You need to replace upper (lower) case letters into lower (upper) case letters in the above data items before displaying them on the screen. What looks like on the screen : F9261xx Tung-Hai Lin ASCII code for Line feed: 0AH Carriage return: 0DH -: 2DH Spacebar: 20H Turn in your .LST and .EXE files.
SHR: 0 C SAR: S C SHIFTING BITS Shifting bits right: For unsigned data; shift logical right For signed data; shift arithmetic right decimal CF MOV BH,10110111B ; 183 SHR BH,01 ; 91 1 MOV BH, 10110111B ; -73 SAR BH,01 ; -37 1
SHL and SAL: C 0 Shifting bits left: No difference between left shifting signed or unsigned data
Rotating Bits rotating bits right: ROR: C For unsigned data; rotate logical right RCR: C For signed data; rotate with carry right
Rotating bits left: ROL: C For unsigned data; rotate logical left RCL: C For signed data; rotate with carry left
Doubleword Shift and Rotate A 32-bit data as DX:AX pair, SHL AX, 1 ; DX:AX × 2 RCL DX, 1 SAR DX, 1 ; DX: AX÷ 2 RCR AX,1