0% found this document useful (0 votes)
31 views

07 - Program Control Instruction

The document discusses different types of jump instructions in microprocessors including short jumps, near jumps, and far jumps. It explains how each type of jump allows branching to different memory locations and segments. Conditional jumps and indirect jumps using registers or tables are also covered.

Uploaded by

Lich King
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

07 - Program Control Instruction

The document discusses different types of jump instructions in microprocessors including short jumps, near jumps, and far jumps. It explains how each type of jump allows branching to different memory locations and segments. Conditional jumps and indirect jumps using registers or tables are also covered.

Uploaded by

Lich King
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Microprocessor

Computer Engineering Study Program


Department of Electrical Engineering
Universitas Indonesia
The Jump Group
 Jump (JMP) 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 programmer
to make decisions based upon numerical
test
 LOOP and conditional LOOP are also
forms of the jump instruction.

2
Unconditional Jump
 Three types: short jump, near jump, far jump.

 Short jump (2-byte) allows jumps or branches


to memory locations within +127 and –128 bytes
from the address following the jump

 Near jump (3-byte) allows a branch or jump


within ±32K bytes from the instruction in the
current code segment.
3
 Far jump (5 byte) allows a jump to any memory
location within the real memory system.

 The short and near jumps are often called


intrasegment jumps.
 Far jumps are called intersegment jumps.

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.

– when the microprocessor executes


a short jump, the displacement is
sign-extended and added to the
instruction pointer (IP/EIP) to
generate the jump address
within the current code segment

– 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

0009H+17H <skipped memory locations>


= 0020H

0020 8B DB NEXT: MOV BX, AX


0022 EB DE JMP START
Near JUMP
0000 33 DB XOR BX, BX
0002 B8 0001 START: MOV AX, 1
0005 03 C3 AND AX, BX
0007 E9 0200 R JMP NEXT

<skipped memory locations>

0200 8B DB NEXT: MOV BX, AX


0202 E9 0002 R JMP START
Far JUMP
EXTRN UP:FAR
0000 33 DB XOR BX, BX
0002 B8 0001 START: ADD AX, 1
0005 E9 0200 JMP NEXT

<skipped memory locations>

0200 8B DB NEXT: MOV BX, AX


0202 EA 0002 ---- R JMP FAR PTR START
0207 EA 0000 ---- E JMP UP
; R stands for relocatable
;E stands for external
;these addresses are established during the linking process
Jumps with Register Operands
 Jump can also use a 16- or 32-bit register as an operand.
 automatically sets up as an indirect jump
 address of the jump is in the register specified
by the jump instruction
 Unlike displacement associated with the near jump, register
contents are transferred directly into the instruction pointer.
 An indirect jump does not add to the instruction pointer.
 JMP AX, for example, copies the contents of the AX register
into the IP.
 allows a jump to any location within the current code segment

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

001B 2C 31 SUB AL,31H ;convert to biner


001D 72 F8 JB TOP ;if below ‘1’ typed
001F 3C 02 CMP AL,2
0021 77 F4 JA TOP ;if above ‘3’ typed
0023 B4 00 MOV AH,0 ;calculate table address
0025 03 C0 ADD AX,AX
0027 03 F0 ADD SI,AX
0029 FF A4 0000 R JMP TABLE [SI] ;jump to ONE, TWO or THREE
002D ONE:
002D B2 31 MOV DL,’1’ ;load DL with ‘1’
002F EB 06 JMP BOT
0031 TWO:
0031 B2 32 MOV DL,’2’ ;load DL with ‘2’
0033 EB 02 JMP BOT
0035 THREE:
0035 B2 33 MOV DL,’3’ ;load DL with‘3’
0037 BOT:
0037 B4 02 MOV AH,2 ;display ONE, Two or THREE
003920CD 21 INT 21H
.EXIT ;exit to DOS
Conditional Jump and Conditional Sets

 The conditional jump instructions test the following flag


bits: sign (S), zero (Z), carry (C), parity (P), and overflow
(O) --- see Table 6.1
 if the condition under test is true, a branch to the label
associated with the jump instruction occurs
 Otherwise, the next sequential step in the program executes
 The conditional jump instructions all test flag bits, except
for the JCXZ (jump if CX=0) and JECXZ (study the
example 6.6)
 See also Table 6.2 for the conditional set instruction

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, 200


MOV SI, 1680H
BACK: CMP [SI], 55H
İNC Sİ
LOOPE BACK
Conditional LOOPs
 Example
 Find the first day that had a 90 degree Fahrenheit in 30 days with
the values stored at offset 1200h

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.

.Mode Small ; Pilih model SMALL


0000 . Data ;Memulai data segmen
0000 00 TEMP DB? ; define TEMP

0000 .CODE ;Awal Code segment


.STARTUP ; Start program
0017 B4 01 MOV AH,1 ; Pembacaan key
0019 CD 21 INT 21H

. IF AL>=’a’ && AL<=’f’ ; Bila huruf kecil


0023 2C 57 SUB AL,57H

.ELSEIF AL>=’A’ && AL<=’F’ ; bila huruf besar


002F 2C 37 .ELSE : bila angka
0033 2C 30 . SUB AL,30H
.ENDIF
A2 0000 R MOV TEMP, AL
.EXIT ; Keluar DOS
END ;Akhir file

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

SUMS PROC NEAR


ADD AX,BX
ADD AX,CX
ADD AX,DX
RET
SUMS ENDP
 To use a procedure: use CALL directive followed by
procedure name

CALL SUMS

 Disadvantage  need to link to (CALL) and return from it


(RET).
 CALL pushes the return address on the stack.
 RET removes an address from the stack so the program
returns to where it was before CALL

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

You might also like