07 - Program Control Instruction
07 - Program Control Instruction
2
Unconditional Jump
Three types: short jump, near jump, far jump.
4
Figure 6–1 The three main forms of the JMP instruction. Note that Disp is
either an 8- or 16-bit signed displacement or distance.
5
Short Jump
Called relative jumps because they can be moved, with
related software, to any location in the current code segment
without a change.
jump address is not stored with the opcode
a distance, or displacement, follows the opcode
The short jump displacement is a distance represented by a
1-byte signed number whose value ranges between +127 and
–128.
6
Figure 6–2 A short jump to four memory locations beyond the address of the
next instruction.
– The instruction
branches to this
new address for
the next instruction
in the program
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
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
if a colon does not follow, you cannot jump to it
The only time a colon is used is when the label is used with a
jump or call instruction.
8
Near Jump
A near jump passes control to an instruction in the current
code segment located within ±32K bytes from the near jump
instruction.
distance is ±2G in 80386 and above when operated in protected
mode
Near jump is a 3-byte instruction with opcode followed by a
signed 16-bit displacement.
80386 - Pentium 4 displacement is 32 bits and the near jump is 5
bytes long
Figure 6–3 A near jump that adds the displacement (0002H) to the
contents of IP.
The near jump is also relocatable because it is also a relative
jump.
This feature, along with the relocatable data segments, Intel
microprocessors ideal for use in a general-purpose computer
system.
Software can be written and loaded anywhere in the memory
and function without modification because of the relative
jumps and relocatable data segments.
Far Jump
Obtains a new segment and offset address
to accomplish the jump:
bytes 2 and 3 of this 5-byte instruction contain
the new offset address
bytes 4 and 5 contain the new segment address
Figure 6–4 A far jump instruction replaces the contents of both CS and IP
with 4 bytes following the opcode.
Short JUMP
0000 33 DB XOR BX, BX
0002 B8 0001 START: MOV AX, 1
0005 03 C3 AND AX, BX
IP = 0007H + 02H
= 0009H
0007 EB 17 JMP SHORT NEXT
17
.MODEL SMALL 0030 ONE: MOV DL, ‘1’
.DATA 0032 JMP TOP
0000 0030 R TABLE: DW ONE 0034 TWO: MOV DL, ‘2’
0002 0034 R DW TWO 0036 JMP TOP
0004 0038 R DW THREE 0038 THREE: MOV DL, ‘3’
.CODE 003A MOV AH, 02H
.STARTUP 003C INT 21 H
.EXIT
0017 TOP: MOV AH, 01H
.END
0019 INT 21H ;read into AL
001B SUB AL, 31H
001D JB TOP
001F CMP AL, 2
0021 JA TOP
0023 MOV AH, 0H
0025 ADD AX, AX
0027 MOV SI, OFFSET TABLE
002A ADD SI, AX
002C MOV AX, [SI]
002E JMP AX
Indirect Jump Using an Index
Ituses the [ ] form of addressing to directly
access the jump table
The JMP Table [SI] instruction (example 6.5)
points to a jump address stored at the code
segment offset location addressed by SI
19
.MODEL SMALL ;select small model
0000 .DATA
Example 6.5 ;start of data segment
0000 0030 R TABLE DW ONE ;define lookup table
0002 0034 R DW TWO
0004 0038 R DW THREE
0000 .CODE ;start code segment
.STARTUP ;start of program
0017 TOP:
0017 B4 01 MOV AH,1 ;read key into AL
0019 CD 21 INT 21H
21
Conditional jump instructions test flag bits:
sign (S), zero (Z), carry (C)
parity (P), overflow (0)
If the condition under test is true, a branch to the label
associated with the jump instruction occurs.
if false, next sequential step in program executes
for example, a JC will jump if the carry bit is set
Most conditional jump instructions are straightforward as
they often test one flag bit.
although some test more than one
When signed numbers are compared, use the JG, JL, JGE, JLE,
JE, and JNE instructions.
terms greater than and less than refer to signed numbers
When unsigned numbers are compared, use the JA, JB, JAB,
JBE, JE, and JNE instructions.
terms above and below refer to unsigned numbers
Remaining conditional jumps test individual flag bits, such as
overflow and parity.
All instructions have alternates, but many aren’t used in
programming because they don’t usually fit the condition
under test.
notice that JE has an alternative op-code JZ
25
LOOP
Loop
It is a combination of a decrement CX and JNZ conditional
jump
Example 6.7 shows how to add data in a block of memory with
data in another block of memory
Conditional Loops
LOOPE (loop while equal) jumps if CX != 0 while an equal condition
exists (the same as LOOPZ)
LOOPNE (loop while not equal) jumps if CX != 0 while a not-equal
condition exists (LOOPNZ)
26
Conditional LOOPs
Example
Assume that you want to test if all of 200 memory locations
starting at the offset of 1680H contain 55H
MOV CX, 30
MOV Sİ, 1200H
BACK: COMPARE [Sİ], 90
İNC Sİ
LOOPNE BACK
Example 6.7
;A program that sums the contens of BLOCK1 and BLOCK2
;and stores the result over top of data in BLOCK2
;through the SI register
;
.MODEL SMALL ;select small model
0000 .DATA ;start of data segment
0000 0064 [ BLOCK1 DW 100 DUP (?) ;100 bytes for BLOCK1
0000
]
00C8 0064 [ BLOCK2 DW 100 DUP (?) ;100 bytes for BLOCK2
0000
]
0000 .CODE ;start of code segment
.STARTUP ;start of program
0017 8C D8 MOV AX,DS ;overlap DS and ES
0019 8E C0 MOV ES,AX
001B FC CLD ;select increment
001C B9 0064 MOV CX,100 ;load count 100
001F BE 0000 R MOV SI,OFFSET BLOCK1 ;address BLOCK1
0022 BF 0000 R MOV DI,OFFSET BLOCK2 ;address BLOCK2
0025 L1:
0025 AD LODSW ;load AX with BLOCK1
0026 26:03 05 ADD AX,ES:[DI] ;add BLOCK2 data to AX
0029 AB STOSW ;store sum in BLOCK2
002A E2 F9 LOOP L1 ;repeat 100 times
.EXIT ;exit to DOS
END ;end file
29
Controlling the Flow of an
Assembly Language Program
It is much easier to use the assembly language
statements .IF., .ELSE., .ELSEIF., and .ENDIF.
DO-WHILE Loops
Pair: .WHILE and .ENDW
REPEAT-UNTIL Loops
Pair: .REPEAT and .UNTIL
30
Example 6.8(a)
; Inti Program Sequence
MOV AH,30H
INT 21 H
.IF AL<3 && AH<30
MOV AH,4CH
INT 21H
.ENDIF
Example 6.8(b)
; Diagram file bahasa Mesin pada contoh 6.8 (a)
;
0000 B4 30 MOV AH,30H
0002 CD 21 INT 21H
.IF AL<3 && AH<30
0004 3C 03 * CMP AL,003H
0006 73 09 * JAE @c0001
0008 80 FC 1E* CMP AH,01EH
000B 73 04 * JAE @c0001
000D B4 4C MOV AH,4CH
000F CD 21 INT 21H
.ENDIF
0011 * @C0001:
31
Example 6.9
[c2]
32
; Program yang membaca sebuah key dan menyimpan dalam Hexadecimal
; Nilai Example
pada memori 6.10
lokasdi TEMP.
33
34
35
PROCEDURES
Is a group of instructions that usually performs one task.
subroutine, method, or function is an important part of any
system’s architecture
Reusable—stored in memory once, used as often as
necessary.
Begins with the PROC directive and ends with the ENDP
directive.
each directive appears with the procedure name
PROC is followed by the type of procedure:
NEAR (global) or FAR (local)
Example
CALL SUMS
38
Figure 6–6 The effect of a near CALL on the stack and the instruction
pointer.
Figure 6–7 The effect of a far CALL instruction.
FAR Call
EXTRN SUBPROG1:FAR PUBLIC SUBPROG1
.MODEL SMALL .MODEL SMALL
.CODE .CODE
MAIN PROC FAR SUBPROG1 PROC FAR
... ...
CALL SUBPROG1 RET
... SUBPROG1 ENDP
MOV AH, 4C END
INT 21H
MAIN ENDP
END MAIN
Introduction to Interrupt
An Interrupt is either a hardware-generated CALL (externally derived from a
hardware signal) or a software-generated CALL(internally derived of the
execution of an instruction or by some other internal event)
Interrupt Vectors
An interrupt vector is a 4-byte number stored in the first 1,024 bytes of
memory (in the real mode)
The vector table is replaced by an interrupt descriptor table that uses 8-byte
descriptors to describe each of the interrupts
There are 256 different interrupt vectors; each vector contains an address of
an interrupt service procedure
42
Interrupt Instructions
INT, INTO, and INT 3
INTs
256 software interrupt (INT) available
Whenever a software interrupt executes, it:
pushes the flags onto the stack
clears the T and I flag bits
pushes CS onto the stack
fetches the new value for IP/EIP from the vector
jump to the new leocation (CS:IP/EIP)
43
IRET/IRETD
Used only with software or hardware interrupt service
procedure
The IRET instruction will:
pop stack data back into the IP
pop stack data back into CS
pop stack data back into the flag register
INT 3
A special software interrupt designed to be used as a
breakpoint
It is common to insert an INT 3 instruction in software to
interrupt or break the flow of the software
44
INTO
Interrupt on overflow is a conditional software interrupt that
tests the overflow flag (O)
if O = 0 the INTO instruction performs no operation
if O = 1 an INTO instruction executes
It appears in software that adds or subtracts signed binary
numbers --> INTO detects the overflow condition
An Interrupt Service Procedure (Ex. 6.20)
The main difference between this procedure and a normal far
procedure is that it ends with the IRET instruction instead of
the RET instruction, and the contents of the flag register are
saved on the stack
45
Interrupt Control
The set interrupt flag instruction (STI) enables the INTR pin
The clear interrupt flag instruction (CLI) disables the INTR pin
Interrupts in the Personal Computer
See Table 6.5
46