170 likes | 319 Views
Program Logic and Control. Mart 2006. Introduction. Instructions do not follow straight line Transfer control forward or backward Change IP content by special instructions Compare CMP, TEST Transfer CALL, JMP, Jnnn, LOOP, RET Logical AND, NOT, OR, XOR Shift and Rotate
E N D
Program Logic and Control Mart 2006
Introduction • Instructions do not follow straight line • Transfer control forward or backward • Change IP content by special instructions • Compare • CMP, TEST • Transfer • CALL, JMP, Jnnn, LOOP, RET • Logical • AND, NOT, OR, XOR • Shift and Rotate • SAR/SHR, SAL/SHL, RCR/ROR, RCL/ROL
Short, Near, Far • Three type of address that are disguished by their distance from the current address. Distance is between • Short: -128 and +128 (one byte) • Near: -32K and +32K (one or two word) • Far: distance is greater than +-32K (means pass another segment , use CALL)
Transfer Control and Limits • JMP => short, near, far • Jnnn => short, near(+80386) • Loop => short • Call => (short), near, far Simple example JMP L10 …. L10: INC CX
JMP • Commanly used • Uniconditional • Flushes prefetched intruction queue • Format: • [label:] JMP short/near/far address • short JMP => EB (1byte) + address (1byte) adds the address to IP, means it is an offset • near JMP => E9(1byte) + 2 byte(80286) or 4 Byte (+80386) • Far JMP=> we will pass now
JMP • Forward JMP JMP L10; …. L10: … • Backward JMP L20: …. ….. JMP L20
Sample Program .MODE SMALL .CODE ORG 100H A10MAIN PROC NEAR MOV AX, 00 MOV, BX, 00 MOV CX, 01 A20: ADD AX, 01 ; 3 byte ADD BX, AX ; 2 byte SHL CX,1 ; 2 byte JMP A20 ; 2 byte A10MAIN ENDP END A10MAIN AX = 1,2,3,4….. BX = 1,3,6,10…. CX = 1,2,4,8…. No end Backward jmp with – 9 offset Hex representation of -9 is FFF7 Jmp addres = IP + jmp operand = 0112h + FFF7h = 109h
LOOP • Specified number of iteration • Initial value in CX • Automatic decrement in CX • If CX is non zero then go loop, else go next instruction • means short jmp • Format • [label:] LOOP short_address
Same Sample with Loop .MODE SMALL .CODE ORG 100H A10MAIN PROC NEAR MOV AX, 00 MOV BX, 00 MOV DX, 01 MOV CX, 08 A20: INC AX ; 3 byte ADD BX, AX SHL DX,1 LOOP A20 MOV AX, 4C00H INT 21H A10MAIN ENDP END A10MAIN Two variant of LOOP LOOPE /LOOPZ (while CX is zero) LOOPNE/ LOOPNZ (while CX is not zero)
The Flag Register C : unsigned arithmetic, shift-rotate, carry out of high order bit P : Odd parity after arithmetic operation A : skip until next chapter Z : result is zero or not S : sign of following arithmetic operation T : is single step mode or not I : Interrupt occured or not D : Direction of string processing (left to right or otherwise) O: signed arithmatic, carry out of high order sign bit 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Z C O D S A I T P
CMP • Compares two numeric data field • Format • [label:] CMP r/m, r/m/i • Cmp of string bytes (use CMPS) • Result of CMP effects A, C,O,P, Z • Example CMP DX, 00 ; Behaves like SUB JE L10 …. L10:…..
Conditional Jmp Instructions • JMP with condition • Format • [label:] Jnnn short-address • Example (equivalent of LOOP) DEC CX; JNZ A20 ; uses zero flag
Signed / Unsigned Data • Signed for numeric values, unsigned for data values • CX = 11000110 and DX = 00010110 • if they are signed then CX is smaller • İf they are unsigned CX is greater • Use suitable Jnnn’s for signed and unsigned data CMP DX, CX CMP DX,CX JB A20; for unsigned JL A20; signed data
Jump Based on UnSigned Data • JE/JZ = ZF • JNE/JNZ = ZF • JA/JNBE = CF,ZF • JAE/JNB = CF • JB/JNAE = CF • JBE/JNA = AF,CF
Jump Based on Signed Data • JE/ JZ = ZF • JNE/JNZ = ZF • JG/JNLE = OF,SF,ZF • JGE/JNL = OF, SF • JL/JNGE = OF,SF • JLE / JNG = OF,SF,ZF
Special Arithmetic Tests • JCXZ = none, cx is zero • JC = cf, carry on (same jb) • JNC = cf, carry off • JO = of, overflow on • JNO = of, overflow off • JP/JPE = pf, parity off or even parity • JNP/JPO= pf, parity off or odd parity • JS = sf, negative • JNS = sf, positive