Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (2 votes)
814 views

Chapter 5 - Program Control Instructions

The document discusses program control instructions including jumps, calls, returns, and interrupts. It describes unconditional and conditional jumps, near and far jumps, loop instructions, procedures, calls, returns, and interrupt vectors. Procedures allow code to be reused through calls and returns, while interrupts pause normal program execution to handle external events by calling interrupt service routines.

Uploaded by

Dawit Tesfaye
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
814 views

Chapter 5 - Program Control Instructions

The document discusses program control instructions including jumps, calls, returns, and interrupts. It describes unconditional and conditional jumps, near and far jumps, loop instructions, procedures, calls, returns, and interrupt vectors. Procedures allow code to be reused through calls and returns, while interrupts pause normal program execution to handle external events by calling interrupt service routines.

Uploaded by

Dawit Tesfaye
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

PROGRAM CONTROL INSTRUCTIONS

Lecture 7

Prepared by: Beimnet G


JUMP
Allows the programmer to skip sections of a program and branch
to any part of the memory for the next instruction.
A conditional jump allows the program to make decisions based on
numerical tests.
The result of these tests is reflected in the flag bits which are
tested by the conditional instructions.
UNCONDITIONAL JUMP (JMP)
3 types: short, near, and far
The short jump: a 2 byte instruction, jumps to +127 or 128 byte of
memory location from the current instruction.
The near jump: a 3 byte instruction, jumps to ± 32Kbytes of
memory location from the current instruction. Can jump to anywhere
in the current code segment.
The far jump: a 5 byte instruction, allows jumps to any location
within the real mode memory.
Note: Remember that segments are cyclic in nature.
SHORT JUMP
Relative jumps
Since an offset or displacement follows the opcode, the instruction can be moved
anyway in the code segment without any change.
The displacement is added to the current IP to generate the jump address.
When a jump references an address, a label normally identifies the address.
The JMP NEXT instruction is an example. It jumps to label NEXT for the next
instruction
It’s very rare to use an actual hexadecimal address with any jump instruction
The label NEXT must be followed by a colon (NEXT:) to allow an instruction to
reference it.
SHORT JUMP
Note: the assembler supports
addressing in relation to the
instruction pointer by using the $+a
displacement.
E.g.: JMP $+2
This jumps over the next two
memory locations (bytes) following
the JMP instruction.
NEAR JUMP
Near jump is a 3-byte instruction
with opcode followed by a signed
16-bit displacement.
It’s also a relative jump.

E.g.: JMP 0002H


FAR JUMP JMP 0127 A300

Obtains a new segment and offset


address to accomplish the jump.
A 5 byte instruction: bytes 2 and 3
contain the new offset address and
bytes 4 and 5 contain the new segment
address.
Far jumps can be achieved by using the
FAR PTR directive in instructions or
labeling the labels jumped to as FAR
labels or following the label by a
double colon :- JMP FAR PTR THERE or
THERE: FAR or THERE::
JUMPS WITH REGISTER OPERANDS
Indirect jumps
Uses a 16 bit register
Unlike displacement associated with the near jump, register
contents are transferred directly into the instruction pointer.
allows a jump to any location within the current code segment.
E.g. JMP AX - copies the contents of the AX register into the
IP.
CONDITIONAL JUMPS
Are always short jumps in microprocessors 8086~80286
Allows a conditional jump to +127 or -128 bytes of memory
location from current instruction.
Conditional jump instructions test flag bits: sign (S), zero (Z), carry
(C), parity (P), and overflow (0)
If the condition under test is true, a branch to the label associated
with the jump instruction occurs. If it’s false, the next sequential
instruction executes.
Both signed and unsigned numbers are used in these instructions,
therefore 2 sets of jump instructions exist based on the type of
number we’re using.
CONDITIONAL JUMPS
When signed numbers are
compared, use the JG, JL,
JGE, JLE, JE, and JNE
instructions.
When unsigned numbers
are compared, use the JA,
JB, JAB, JBE, JE, and JNE
instructions.
Other instructions test just
the flag bits.
E.g. JZ, JC, JO
LOOP
A combination of a decrement CX and the JNZ conditional jump.
 LOOP decrements CX; if CX != 0, it jumps to the address
indicated by the label. If CX becomes 0, the next sequential
instruction executes.
A block of code is executed multiple times until CX is 0
CONDITIONAL LOOP
LOOP instruction also has conditional forms:
LOOPE (LOOPZ) and LOOPNE (LOOPNZ)
LOOPE (loop while equal) instruction jumps if CX != 0 while an
equal condition exists; will exit loop if the condition is not equal or
the CX register decrements to 0
LOOPNE (loop while not equal) jumps if CX != 0 while a not-equal
condition exists; will exit loop if the condition is equal or the CX
register decrements to 0
CONTROLLING THE FLOW OF THE PROGRAM
It is much easier to use the assembly language statements .IF, .ELSE,
.ELSEIF, and .ENDIF to control the flow of the program than it is to
use the correct conditional jump statement.
Other statements: .REPEAT - .UNTIL and .WHILE - .ENDW, .BREAK,
.CONTINUE
These statements can replace the conditional jump and loop
statements.
CONTROLLING THE FLOW OF THE PROGRAM
PROCEDURES
Subroutine, method, functions
A group of instructions that perform a task and is written once and
used multiple times.
Disadvantage of procedure is time it takes the computer to link to,
and return from it. CALL links to the procedure; the RET (return)
instruction returns from the procedure
The CALL instruction pushes an address to the stack and the RET
instruction pops from the stack to identify the return address.
PROCEDURES
A procedure begins with the PROC directive and ends with the
ENDP directive.
Each directive appears with the procedure name.
The PROC directives is followed by NEAR or FAR describing if the
procedure is a global or local procedure.
The NEAR or FAR type can be followed by the USES statement. The
USES statement allows any number of registers to be automatically
pushed to the stack and popped from the stack within the procedure.
MYPROC PROC [NEAR] [USES AX,BX]
CALL
The CALL instruction transfers the flow of the program to the
procedure.
The CALL instruction differs from the jump instruction because a
CALL saves a return address on the stack.
The return address returns control to the instruction that
immediately follows the CALL in a program when a RET instruction
executes.
NEAR CALL
A 3 byte instruction: the first byte is the opcode and the second 2
bytes contain the displacement.
When the near CALL executes, it first pushes the offset address of
the next instruction (in IP) onto the stack.
After saving this return address, it then adds the displacement
from bytes 2 and 3 to the IP to transfer control to the procedure.
There is no short CALL instruction.
NEAR CALL
CALL (proc name)
FAR CALL
A 5 byte instruction: the opcode is followed by the new offset
contained in bytes 2 and 3 and the new CS values contained in
bytes 4 and 5.
The far CALL pushes both the current CS and IP values on to the
stack before jumping to the new location.
The type of call, near or far, can be defined using the CALLN or
CALLF instructions but this should be avoided and the PROC should
be labeled NEAR or FAR to identify the type of call instruction
instead.
CALL WITH REGISTER OPERANDS
CALL can use register addressing.
Example: CALL BX pushes the contents of IP onto the stack then
jumps to the offset address, located in register BX, in the current
code segment.
CALL WITH INDIRECT MEMORY ADDRESSES
useful when different subroutines need to be chosen in a program.
selection process is often keyed with a number that addresses a
CALL address in a lookup table
the memory address itself contains the address for the next
instruction.
E.g. CALL TABEL[SI]
RET
Removes a 16-bit number (near return) from the stack placing it in
IP, or removes a 32-bit number (far return) and places it in IP & CS.
The assembles automatically selects the proper return instruction
based on the NEAR and FAR directives found in the PROC’s header.
The program returns to the next instruction following the most
recent CALL instruction.
RET
A near RET
RET
RET N ; where is the a number added to the SP
A form of return adds a number to the contents of the stack pointer
(SP) after the return address is removed from the stack. This causes
a number of byte to be discarded.
Some conventions push parameters on the stack before calling a
procedure, the return statement is followed by the number of bytes
pushed to the stack as parameters so they can be discarded once
the procedure is done.
As with the CALL statement, variants RETN and RETF should be
avoided.
INTERRUPTS
Lecture 8

Prepared by: Beimnet G


INTRODUCTION
A hardware or software generated CALL that interrupts the
program by calling an Interrupt Service Procedure (ISP) or an
interrupt handler, which is a short program that instructs the
microprocessor on how to handle the interrupt.
Is a method of creating a temporary halt during program
execution and allows devices to access the microprocessor.
A hardware generated CALL is externally derived from a
hardware signal.
A software generated CALL is internally derived from the
execution of an instruction.
INTERRUPTS VECTORS
A 4-byte number stored in the first 1024 bytes of memory (00000H–
003FFH) in real mode.
256 different interrupt vectors, each vector contains the address of an
interrupt service procedure
Each vector contains a value for IP and CS that forms the address of
the interrupt service procedure, the first 2 bytes contain IP; the last 2
bytes CS.
Intel reserves the first 32 interrupt vectors for the present and future
products, interrupt vectors (32–255) are available to users
Some reserved vectors are for errors that occur during the execution
of software such as the divide error interrupt.
INTERRUPT INSTRUCTIONS
The microprocessor has 3 interrupt instructions: INT, INTO, and INT 3
each instruction fetches a vector from the vector table, and then calls
the procedure stored at the location addressed by the vector.
The interrupt call is similar to a far CALL instruction because it places
the return address on the stack.
INTS (INTERRUPT INSTRUCTIONS)
Each INT instruction has a numeric operand that range from 0 to
255 (00H to FFH).
The address of the interrupt vector is determined by multiplying
the interrupt type number by 4
Each INT instruction is 2 bytes long. The first byte contains the
opcode and the second byte contains the vector type number.
Exception: INT 3 is a 1 byte interrupt instruction.
INTS
When a software interrupt executes, it:
opushes the flags onto the stack
oclears the T and I flag bits
opushes CS onto the stack
ofetches the new value for CS from the interrupt vector
opushes IP onto the stack
ofetches the new value for IP from the vector
ojumps to the new location addressed by CS and IP
INTS
Software interrupts are most commonly used to call system
procedures because the address of the system function doesn’t
have to be known to the program.
Relieves the program from having to remember the address of the
system call.
Replaces a CALL that would’ve been used to call the system
functions.
INT is a 2 byte instruction compared to a far CALL which is 5 bytes
long.
IRET
A return instruction used only with interrupt service procedures.
IRET instruction will
opop stack data back into the IP
opop stack data back into CS
opop stack data back into the flag register
Similar to a POPF followed by a far Return
When IRET executes, it restores the contents of I and T from the
stack.
INT 3
a special 1 byte software interrupt instruction designed to function
as a breakpoint.
Common to insert an INT 3 in software to interrupt or break the
flow of the software.
Breakpoints are used to debug faulty software.
A breakpoint occurs for any software interrupt, but because INT 3
is 1 byte long, it is easier to use for this function.
INTO
A conditional software interrupt.
tests the overflow flag (O), if O = 0, the INTO instruction performs
no operation; if O= 1 and an INTO instruction executes, an
interrupt occurs via vector type number 4.
appears in software that adds or subtracts signed binary numbers.
AN INTERRUPT SERVICE PROCEDURE
Example: An interrupt procedure that adds the contents of DI, SI,
BP, and BX and then save the sum in AX.

Note: Interrupts are usually used for system events, this is just an
example.
INTERRUPT CONTROL
Hardware interrupt is caused by any peripheral device by sending
a signal through a specified pin to the microprocessor.
One interrupt pin in the 8086 microprocessor is the INTR pin.
There are 2 instructions that control the INTR pin: set interrupt flag
(STI) that enables the INTR pin and the clear interrupt flag (CLI)
that disables the INTR pin.
 In a software interrupt service procedure, hardware interrupts are
enabled as one of the first steps. Because just about all of the I/O
devices in the personal computer are interrupt-processed.
INTERRUPTS IN A PERSONAL COMPUTER
Interrupts found in the personal computer only contain Intel-
specified interrupts 0–4 that were present in the early 8086
microprocessors.
Access to the 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.
MACHINE CONTROL AND MISCELLANEOUS
INSTRUCTIONS
Used in hardware control
Provide control of the carry bit, sample the BUSY/TEST pin, and
perform various other functions.
STC, CLC, CMC
WAIT
HLT Reading Assignment
NOP
LOCK
ESC
BOUND
ENTER and LEAVE

You might also like