Anda di halaman 1dari 65

Chapter 6 Program Control Instruction

program control instruction :

Introduction

direct the flow of a program, allow the flow to change jumps, calls, returns, interrupts, machine control instructions

change in flow :
CMP, TEST followed by conditional jump

relational assembly language statements :


.IF, .ELSE, .ELSEIF, .WHILE, .ENDW, .REPEAT, .UNTIL MASM, TASM Ver.6X ~ allow to develop control flow portions of program with C/C++ language efficiency
Ch.6 Program Control Instructions 2

6-1 The Jump Group


JMP(jump) : allow to skip sections of a program and
blanch to any part of memory for next instruction

unconditional jump, conditional jump three type unconditional jump : Fig. 6-1

Unconditional Jump(JMP)
intrasegment jump : short, near jump
Short jump(2-byte): 1 byte disp.(within +127~-128 byte) Near jump(3-byte) : 2 byte disp.(within 32K bytes or anywhere in current code segment)

segments : cyclic in nature intersegment, far jump(5-byte) :


any memory location within the real memory system

80386~ (in protected mode)


Near(5-byte) : 4 byte displacement(within 2G bytes) Far(7-byte) : 4 byte(EIP), 2 byte(CS)
Ch.6 Program Control Instructions 4

Short Jump
short jump : relative jump
distance or displacement : follow the opcode

one-byte signed number(+127~-128) :


sign-extended and added to IP/EIP to generate the jump address within current code segment

EX. 6-1 : label : symbolic name for memory address SHORT directive : force a short jump most assembler : choose best form of jump instruction JMP START : assemble as a short jump
Ch.6 Program Control Instructions 5

Short Jump
1st jump : 0020H 0009H = 0017(disp. = 17H) 2nd jump : 0002H 0024H = FFDEH(disp. = DEH)

Ch.6 Program Control Instructions

Fig. 6-2
Fig. 6-2

Ch.6 Program Control Instructions

Near, Far Jump


near jump : relocatable because relative jump signed displacement : added to IP/EIP to generate the jump address
2 byte : 32K bytes in current code segment 4-byte(386~ in protected mode) : 2G bytes

far jump : 5(7, 80386~) byte instruction


new offset address(IP/EIP) : byte 2,3(2~5) new segment address(CS) : byte 4,5(6,7)

80286~ in protected mode : CS access a descriptor that contain base address of far jump segment
Ch.6 Program Control Instructions 8

Fig. 6-3
Fig. 6-3

Ch.6 Program Control Instructions

EX. 6-2 : Near Jump


E9 0200 R JMP NEXT : only list file R : denote a relocatable jump address of 0200H
actual machine code : E9 F6 01 0200H - 000AH = 01F6H

Ch.6 Program Control Instructions

10

Fig. 6-4
Fig. 6-4

Ch.6 Program Control Instructions

11

EX. 6-3
far jump : FAR PTR directive, far label far label : external to current code segment
EXTRN UP:FAR directive a global label as a double colon(LABEL::) ----E : external. filled in by linker when links program files

12

Indirect Jump
jump with 16-, 32-bit reg. operand : indirect jump
contents of reg. : transferred directly into IP/EIP JMP AX : IP AX, JMP EAX : EIP EAX

EX. 6-4 : how JMP AX access jump table


read a key, converted ASCII to binary, doubled jump table : 16-bit offset address

Indirect Jumps using Index : double-indirect jump


[ ] form of addressing to directly access jump table near jump JMP TABLE[SI] : IP [SI+TABLE] far jump JMP FAR PTR [SI], JMP TABLE [SI] with TABLE data defined DD directive
Ch.6 Program Control Instructions 13

EX. 6-4
EX. 6-4

14

EX. 6-5
EX. 6-5

15

Conditional Jumps
conditional jump : short jump
~ 80286(short jump) : +127 ~ -128 80386 ~(short, near jump) : 1, 4 bytes

test one flag bit or some more : S, Z, C, P, O


if condition under test is true : branch to the label if condition is false : next sequential instruction

relative magnitude comparisons :


require more complicated conditional jump instructions that test more than one flag bit

Table 6-1 : conditional jump instructions


Ch.6 Program Control Instructions 16

Table 6-1
Table 6-1

17

Fig. 6-5
Fig. 6-5 : order of signed, unsigned 8-bit no.s

Ch.6 Program Control Instructions

18

Conditional Jumps
unsigned : FFH is above 00H, above, below, equal signed : FFH less than 00H, greater, less, zero alternate form :
JE = JZ JA(if above) = JNBE(if not below or equal)

JCXZ(jump if CX = 0), JECXZ(jump if ECX=0)


if CX/ECX = 0 : jump occur if CX/ECX <> 0 : no jump occur

EX. 6-6 : search table for 0AH using SANSB, JCXZ


Ch.6 Program Control Instructions 19

EX. 6-6
EX. 6-6

Ch.6 Program Control Instructions

20

Conditional Set Instructions


conditional set instructions :
80386~ set a byte to either a 01H or clear a byte to 00H useful where a condition must be tested at a point much later in the program

SETNC MEM :
places a 01H into memory location MEM if carry is cleared and a 00H into MEM if carry is set

Table 6-2 :
Ch.6 Program Control Instructions 21

Table 6-2
Table 6-2

22

LOOP, Conditional LOOP


LOOP : combination of decrement CX and JNZ
~ 80286 : DEC CX ; if CX <> 0, jump to label CX = 0, execute next sequential instruction 80386 ~ : CX/ECX depending on instruction mode if

LOOPE(loop while equal, LOOPZ) :


jump if CX <> 0 while equal condition exist exit the loop if CX = 0 or condition is not equal

LOOPNE(loop while not equal, LOOPNZ) :


jump if CX <> 0 while not-equal condition exist exit the loop if CX = 0 or condition is equal LOOPEW/LOOPED,LOOPNEW/LOOPNED:override mode
Ch.6 Program Control Instructions 23

EX. 6-7
EX. 6-7 :

24

6-2 Controlling the Flow of an Assembly Language Program


relational statements
.IF, .ELSE, .ELSEIF, ENDIF, .REPEAT-.UNTIL, .WHILE-.ENDW : easier to control the flow than conditional jump EX. 6-8 : testing system for version of DOS

DOS INT 21H, function no. 30H : read DOS ver. (a) : source program, (b) fully expended assembled * : assembler-generated and -inserted statements && : logical AND Table 6-3 : relational operator
Ch.6 Program Control Instructions 25

Table 6-3
Table 6-3

Ch.6 Program Control Instructions

26

EX. 6-10
EX. 6-10 : read a key, convert to hexadecimal `a`(61H), `A`(41H) : 61H(41H)-57H(37H)=0AH

27

DO-WHILE Loops
.WHILE statement : used with a condition to begin the loop EX. 6-11 : read a key, store into array called BUF until enter key(0DH) is typed DOS 21H, fn no. 09H

Ch.6 Program Control Instructions

28

EX. 6-11
EX. 6-11

29

REPEAT-UNTIL Loops
.REPEAT : defined start of loop .UNTIL : defined end of loop, contained condition EX. 6-14 : EX. 6-11,12

Ch.6 Program Control Instructions

30

EX. 6-14
EX. 6-14

31

6-3 Procedures
Procedure :
a group of instructions that usually performs one task a reusable section of the software that is stored in memory once, but used as often as necessary

advantage :
save memory space make it easier to develop software

disadvantage :
take the computer a small amount of time to link to procedure and return from it

CALL/RET : link to/return from the procedure


Ch.6 Program Control Instructions 32

Procedure
CALL : push the address of instruction following CALL(return address) on stack RET : remove an address from stack so the program return to instruction following CALL specific rules for storing procedure
begin with PROC, end with ENDP directive each directive : appear with name of procedure PROC : followed by type of procedure : NEAR,FAR type :can be followed by the USES statement USES statement : allow any no. of reg. to be automatically pushed and popped within procedure
Ch.6 Program Control Instructions 33

EX. 6-16
EX. 6-16

Ch.6 Program Control Instructions

34

CALL
near return(C3H) : remove 16-bit no. from stack, place it into IP to return from procedure in current segment far return(CBH) : remove 32-bit no. from stack, place it into both IP, CS to return from procedure to any memory location far procedure : global, used by all software near procedure : local, used by a given task CALL : differ from jump instruction
because a CALL save a return address on stack
Ch.6 Program Control Instructions 35

Near CALL
near CALL : 3(5, 80386~ in protected mode)-byte instruction
1st byte : opcode 2nd, 3rd byte : displacement(distance) of 32K 2nd~5th byte : 32-bit displacement of 2G bytes

near CALL execute :


push offset address of next instruction(IP/EIP) on stack add displacement from byte 2,3(2~5) to IP/EIP to transfer control to the procedure

CALLN(near CALL) Fig. 6-6 :


Ch.6 Program Control Instructions 36

Fig. 6-6
Fig. 6-6

Ch.6 Program Control Instructions

37

Far CALL
far CALL : 5(7, 80386~ in protected mode)-byte instruction
1st byte : opcode 2nd 3rd byte : new IP, 4th 5th byte : new CS 2nd~5th byte : new EIP, 6th 7th byte : new CS

far CALL execute :


push IP/EIP, CS on stack place byte 2,3(2~5) to IP/EIP and byte 4,5(6,7) to CS to call a procedure located anywhere in memory system

CALLF(far CALL) Fig. 6-7


Ch.6 Program Control Instructions 38

Fig. 6-7
Fig. 6-7

Ch.6 Program Control Instructions

39

CALLs with Register, indirect address


CALL with register operand :
like jump, also contain a register operand

CALL BX : push IP, jump to offset address located in BX(IP BX) in current code segment CALL with indirect memory address :
useful whenever different subroutines need to be chosen

CALL : also reference far pointers CALL FAR PTR [SI] or CALL TABLE[SI]
data in table : defined as doubleword data with DD retrieve a 32-bit address from data segment addressed by SI, use it as address of a far procedure
Ch.6 Program Control Instructions 40

EX. 6-17 : display OK

EX. 6-17

41

RET
RET : real mode(80386~ in protected mode)
near RET: remove 16-bit(32-bit), place it into IP/EIT far : remove 32-bit(6 bytes), place it into IP/EIP, CS

near, far return : defined in procedures PROC other form : RET n


n(bytes) : add n to contents of SP after return address is removed from stack push passing parameters on stack before calling procedure if these parameters are to be discarded upon return, RET contains a no. that represents the no. of bytes pushed to stack as parameters
Ch.6 Program Control Instructions 42

Fig. 6-8 : near return


Fig 6-8

Ch.6 Program Control Instructions

43

EX. 6-19
EX. 6-19

Ch.6 Program Control Instructions

44

RET
RETN : CALLN RETF : CALLF passing parameters to a procedure :
1. to use one of the CPU register : MOV CX, TI 2. to use a memory location : MOV TEMP, TI 3. to pass the address of memory location : MOV SI, OFFSET TI 4. to pass the parameters on the stack : (EX. 6-19) MOV DX, TI, PUSH DX 5. to use stack frame : ENTER, LEAVE(p.211)
Ch.6 Program Control Instructions 45

6-4 Introduction to Interrupt


1. hardware-generated CALL : external interrupt
externally derived from a hardware signal

2. software-generated CALL : internal, exception


internally derived from the execution of an instruction or by some other internal event()

interrupt : interrupts the program by calling an interrupt


service procedure or interrupt handler

interrupt vector :
in real : 4-byte no. stored in 1st 1024 bytes(~0003FFH) 256(00H~FFH) 4byte = 1024byte protected : replaced by interrupt descriptor table
Ch.6 Program Control Instructions 46

Interrupt Vectors
Table 6-4 : 256 different interrupt vectors in real
each contain address of an interrupt service procedure for IP, CS Intel reserve the 1st 32 interrupt vector(~1FH) : for present , future remaining : available for user some of reserved : for error that occur during execution of software, such as divide error interrupt some of reserved : for coprocessor others : occur for normal events in the system vectors 1-6,7,9,17 : function in real, protected mode remaining : only in protected mode
Ch.6 Program Control Instructions 47

Table 6-4
Table 6-4

Ch.6 Program Control Instructions

48

Interrupt Instructions
software interrupt instruction : special type of CALL
INT, INTO, INT3

each of these instruction :


1. in real, fetches vector from interrupt vector table 1. in protected, fetches an interrupt descriptor from interrupt descriptor table 2. calls the interrupt service procedure

interrupt call : similar to far CALL instruction


because placed return address(IP/EIP, CS) on stack different : pushed flags, then pushed return address fetched new value IP/EIP, CS from vector
Ch.6 Program Control Instructions 49

INTs
INT n : 256 different software interrupt instruction
type no. n : 0 ~ 255(00H ~ FFH)

INT 100 : uses interrupt vector no. 100(64H)


memory address in IVT : 190H~193H 0110 0100(64H) shift left 2 01 1001 0000(190H)

address of interrupt vector in real :


multiplying type no. times 4(each vector : 4 bytes)

address of interrupt descriptor in protected :


multiplying type no. times 8(each descriptor : 8 byte)

INT : 2-byte long(1st:opcode, 2nd:vector type no.)


INT 3: 1-byte special software interrupt for breakpoints
Ch.6 Program Control Instructions 50

INTs
software interrupt instruction execute :
(1) push flags (2) clear T, I flag bits (3) push CS (4) fetch new value for CS from interrupt vector (5) push IP/EIP (6) fetch new value for IP/EIP from vector (7) jump new location addressed by CS, IP/EIP

INT : perform PUSHF, followed by far CALL I flag : control external hardware interrupt input pin,
INTR(maskable interrupt request) I = 0 : disable the INTR pin

T flag : trap, single step interrupt


T = 0 : disable the single step interrupt
Ch.6 Program Control Instructions 51

INTs
software interrupt :
most commonly used to call system procedure because address of system function need not be known

system procedure(function) :
common to all system, application software

software interrupt :
often control printers, video displays, disk drives relieving the program from remembering address of system call

INT(2-byte long) : used to replace a far CALL(5-byte)


Ch.6 Program Control Instructions 52

IRET/IRETD
IRET : used only with software or hardware interrupt service
procedures

IRET : perform POPF, followed by far RET


(1) pop stack data back into IP/EIP (2) pop into CS (3) pop into flag register

IRETD : 80386~ in protected mode


differ from IRET, pop 32-bit EIP from stack

RET 3 : 1-byte instruction


special software interrupt designed to function breakpoint interrupt or break the flow of software breakpoint : help to debug faulty software
Ch.6 Program Control Instructions 53

Interrupt
INTO(interrupt on overflow) :
conditional software interrupt that test overflow flag(O) if O = 1 : interrupt vector no. 4 occur appear in software that add, subtract signed binary no.

interrupt service procedure :


same as far procedure different : ends with IRET, saved and restored flag register

interrupt control
STI(set interrupt flag) : I 1, enable INTR pin CLI(clear interrupt flag) : I 0, disable INTR pin
Ch.6 Program Control Instructions 54

EX. 6-20
EX. 6-20 : add DI, SI, BP, BX, save sum in AX

Ch.6 Program Control Instructions

55

Interrupts in the Personal Computer


Interrupts found in the personal computer only contained Intel-specified interrupts 04. Access to protected mode interrupt structure in use by Windows is accomplished through kernel functions Microsoft provides.
and cannot be directly addressed

Protected mode interrupts use an interrupt descriptor table.


Ch.6 Program Control Instructions 56

Figure 69 Interrupts in a typical personal computer.


Figure 69 Interrupts in a typical personal computer.

Ch.6 Program Control Instructions

57

6-5 Machine Control and Miscellaneous Instructions carry flag(C) : carry(addition), borrow(subtraction)
STI(set carry) CLC(clear carry) CMC(complement carry) indicate error in procedure(ex. reads data from a disk memory file) : successful or file-not-found error

WAIT :
monitor BUSY(286,386)/TEST(8086/88) input pin BUSY pin : connected to BUSY pin of coprocessor BUSY = 1 : nothing happen, next instruction execute BUSY = 0 : wait for BUSY pin return to logic 1, wait until coprocessor finishes a task
Ch.6 Program Control Instructions 58

HLT, NOP
HLT(halt) :
stop the execution of soft ware three ways to exit a halt : by an interrupt, by hardware reset, during a DMA operation normally appears in a program to wait for an interrupt

NOP(no operation)
take a short time to execute no operation also used in time delays to waste time : not very accurate(cache, pipeline in modern ) often used to pad software with space for future machine language
Ch.6 Program Control Instructions 59

LOCK prefix, ESC


LOCK prefix :
cause LOCK output pin to activate(logic 0) for duration of a locked instruction LOCK pin : often disables external bus masters or other system components ex. : LOCK:MOV AL,[SI]

ESC(escape) :
pass information to numeric coprocessor 6 bits of the ESC instruction : provide the opcode to coprocessor and begin executing an instruction ESC opcode : never appear code prefixed in coprocessor instruction(FLD,FST..)
Ch.6 Program Control Instructions 60

BOUND
BOUND : 80186~
compare contents of any 16- or 32-bit reg. against contents of two word or doubleword of memory: a lower and an upper boundary if value of reg. is not within the boundary : type 5 interrupt if within the boundary : next instruction execute

ex. BOUND SI, DATA


word-sized location DATA : lower boundary word-sized location DATA+2 : upper boundary if SI WORD PTR [DATA] or WORD PTR [DATA+2] SI : type 5 interrupt

return address : point to BOUND instruction


Ch.6 Program Control Instructions 61

ENTER and LEAVE


ENTER(two operand) : create stack frame
by pushing BP onto stack and loading BP with uppermost address of stack frame stack frame variables : accessed through BP 1st operand : no of byte to reserve for variables on stack frame, 2nd : level of procedure

stack frame : mechanism used to pass parameters to a


procedure through the stack memory

LEAVE : reverse ENTER


by reloading SP, BP with their prior values

ENTER 8,0 : reserve 8 bytes for stack frame(Fig6-9)


Ch.6 Program Control Instructions 62

Fig. 6-9
Fig. 6-9

Ch.6 Program Control Instructions

63

EX. 6-21 : create a stack frame so that two 16-bit parameters are passed to a system level procedure

EX. 6-21

64

EX. 6-21
EX. 6-21 :

Ch.6 Program Control Instructions

65

Anda mungkin juga menyukai