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
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)
Short Jump
short jump : relative jump
distance or displacement : follow the opcode
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)
Fig. 6-2
Fig. 6-2
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
10
Fig. 6-4
Fig. 6-4
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
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
Table 6-1
Table 6-1
17
Fig. 6-5
Fig. 6-5 : order of signed, unsigned 8-bit no.s
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)
EX. 6-6
EX. 6-6
20
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
EX. 6-7
EX. 6-7 :
24
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
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
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
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
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
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
Fig. 6-6
Fig. 6-6
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
Fig. 6-7
Fig. 6-7
39
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
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
43
EX. 6-19
EX. 6-19
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
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
48
Interrupt Instructions
software interrupt instruction : special type of CALL
INT, INTO, INT3
INTs
INT n : 256 different software interrupt instruction
type no. n : 0 ~ 255(00H ~ FFH)
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
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
IRET/IRETD
IRET : used only with software or hardware interrupt service
procedures
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 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
55
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
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
Fig. 6-9
Fig. 6-9
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 :
65