Program Control Instructions Prima Dewi Purnamasari Microprocessor Department of Electrical Engineering University of Indonesia.

Slides:



Advertisements
Similar presentations
Chapter 6: Program Control Instructions
Advertisements

There are two types of addressing schemes:
Instruction Set Architecture
Micro-Computer Applications: Jumping and Loop Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
ACOE2511 Assembly Language Arithmetic and Logic Instructions.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Interrupt Processing Haibo Wang ECE Department
Flow Control Instructions
Introduction to Interrupts
Microcomputer & Interfacing Lecture 3
8.4 Instruction Execution Times TOBIN PROC FAR SUB AX,AX MOV DX,AX MOV CX,4 NEXTD: PUSH CX SUB BP,BP MOV CX,4 GETNUM: RCL BX,1 RCL BP,1 LOOP GETNUM.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
An Introduction to 8086 Microprocessor.
Microprocessor Programming II
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Faculty of Engineering, Electrical Department,
CE302 MICROPROCESSORS Levent EREN Izmir University of Economics.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Strings, Procedures and Macros
Executing and Linking an assembly program. Lesson plan Review Program logic and control Practice exercise Assembling, Linking and Executing Programs Practice.
1 ICS 51 Introductory Computer Organization Fall 2009.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 5 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Program Control Instructions
Microprocessor Programming II To discuss more complicated programming techniques Flag control instructions Compare and jump Subroutines Loop and string.
EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of.
LEA instruction The LEA instruction can be used to get the offset address of a variable Example ORG 100h MOV AL, VAR1 ; check value of VAR1 by moving it.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Introduction to Microprocessors Chapter 3. Programming Model (8086)  Shows the various internal registers that are accessible to the programmer.
2/20/2016CAP 2211 Flow Control Instructions. 2/20/2016CAP 2212 Transfer of Control Flow control instructions are used to control the flow of a program.
Jumps, Loops and Branching. Unconditional Jumps Transfer the control flow of the program to a specified instruction, other than the next instruction in.
Internal Programming Architecture or Model
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Assembly language programming
Instruction set Architecture
Format of Assembly language
Microprocessor and Assembly Language
Homework Reading Labs PAL, pp
Instruksi Set Prosesor 8088
Microprocessor and Assembly Language
EE3541 Introduction to Microprocessors
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Machine control instruction
INSTRUCTION SET.
Assembly Language Programming Part 2
ADDRESSING MODES.
Intel 8088 (8086) Microprocessor Structure
Microprocessor and Assembly Language
Processor Processor characterized by register set (state variables)
(The Stack and Procedures)
Chapter 3 Addressing Modes
Subject Name: MICROPROCESSORS Subject Code:10CS45
Computer Organization and Assembly Language
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Flags Register & Jump Instruction
Intel 8088 (8086) Microprocessor Structure
(The Stack and Procedures)
Homework Reading Machine Projects Labs PAL, pp
Flow Control Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
EECE.3170 Microprocessor Systems Design I
(The Stack and Procedures)
The JUMP GROUP Unconditional Jump (JMP).
Chapter 8: Instruction Set 8086 CPU Architecture
Computer Operation 6/22/2019.
Procedures and Macros.
Presentation transcript:

Program Control Instructions Prima Dewi Purnamasari Microprocessor Department of Electrical Engineering University of 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 and above when operated in protected mode  Near jump is a 3-byte instruction with opcode followed by a signed 16-bit displacement.  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 DB XOR BX, BX 0002 B START: MOV AX, C3AND AX, BX 0007 EB 17JMP SHORT NEXT B DBNEXT:MOV BX, AX 0022 EB DEJMP START 0009H+17H = 0020H IP = 0007H + 02H = 0009H

Near JUMP DB XOR BX, BX 0002 B START: MOV AX, C3AND AX, BX 0007 E RJMP NEXT B DBNEXT:MOV BX, AX 0202 E RJMP START

Far JUMP EXTRN UP:FAR DB XOR BX, BX 0002 B START: ADD AX, E JMP NEXT B DBNEXT:MOV BX, AX 0202 EA RJMP FAR PTR START 0207 EA EJMP 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.DATA RTABLE: DW ONE R DW TWO R DW THREE.CODE.STARTUP 0017 TOP:MOV AH, INT 21 ;read into AL 001BSUB AL, DJB TOP 001FCMP AL, JA TOP 0023MOV AH, ADD AX, AX 0027MOV SI, OFFSET TABLE 002AADD SI, AX 002CMOV AX, [SI] 002EJMP AX 0030 ONE:MOV DL, ‘1’ 0032 JMP BOT 0034TWO: MOV DL, ‘2’ 0036JMP BOT 0038THREE:MOV DL, ‘3’ 003AMOV AH, 2 003CINT 21 H.EXIT.END

Indirect Jump Using an Index  It uses 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 SIexample

20 Example 6.5.MODEL SMALL ;select small model 0000.DATA;start of data segment RTABLEDW ONE;define lookup table RDW TWO R DW THREE 0000.CODE;start code segment.STARTUP;start of program 0017TOP: 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 F8JB TOP;if below ‘1’ typed 001F 3C 02CMP AL, F4JA TOP;if above ‘3’ typed 0023 B4 00MOV AH,0;calculate table address C0ADD AX,AX F0ADD SI,AX 0029 FF A RJMP TABLE [SI];jump to ONE, TWO or THREE 002DONE: 002D B2 31MOV DL,’1’;load DL with ‘1’ 002F EB 06 JMP BOT 0031TWO: 0031 B2 32 MOV DL,’2’;load DL with ‘2’ 0033 EB 02 JMP BOT 0035THREE: 0035 B2 33MOV DL,’3’;load DL with‘3’ 0037BOT: 0037 B4 02 MOV AH,2;display ONE, Two or THREE 0039 CD 21 INT 21H.EXIT;exit to DOS END;end of file

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.1Table 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) example 6.6  See also Table 6.2 for the conditional set instruction Table

 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 Example 6.7  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

29 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 [BLOCK1 DW100 DUP (?);100 bytes for BLOCK ] 00C [BLOCK2 DW100 DUP (?);100 bytes for BLOCK ] 0000.CODE;start of code segment.STARTUP;start of program C D8MOV AX,DS;overlap DS and ES E C0MOV ES,AX 001B FC CLD ;select increment 001C B9 0064MOV CX,100;load count F BE 0000 RMOV SI,OFFSET BLOCK1;address BLOCK BF 0000 RMOV DI,OFFSET BLOCK2;address BLOCK L1: 0025 ADLODSW;load AX with BLOCK :03 05ADD AX,ES:[DI];add BLOCK2 data to AX 0029 ABSTOSW;store sum in BLOCK2 002A E2 F9LOOP L1;repeat 100 times.EXIT;exit to DOS END;end file

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

31 ; Inti Program Sequence MOVAH,30H INT 21 H.IF AL<3 && AH<30 MOVAH,4CH INT 21H.ENDIF Example 6.8(a) 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< C 03 * CMP AL,003H * FC 1E* CMP AH,01EH 000B * 000D B4 4C MOV AH,4CH 000F CD 21 INT 21H.ENDIF 0011

32 Example 6.9 [c2]

33 Example 6.10 ; Program yang membaca sebuah key dan menyimpan dalam Hexadecimal ; Nilai pada memori lokasdi TEMP..Mode Small ; Pilih model SMALL Data ;Memulai data segmen TEMP DB? ; define TEMP 0000.CODE ;Awal Code segment.STARTUP ; Start program 0017 B4 01 MOV AH,1 ; Pembacaan key 0019 CD 21INT 21H. IF AL>=’a’ && AL<=’f’ ; Bila huruf kecil C 57 SUB AL,57H.ELSEIF AL>=’A’ && AL<=’F’ ; bila huruf besar 002F 2C 37.ELSE : bila angka C 30. SUB AL,30H.ENDIF A R MOV TEMP, AL.EXIT ; Keluar DOS END ;Akhir file

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 ADDAX,BX ADDAX,CX ADDAX,DX RET SUMSENDP

 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.MODEL SMALL.CODE MAINPROC FAR... CALL SUBPROG1... MOV AH, 4C INT 21H MAIN ENDP END MAIN PUBLIC SUBPROG1.MODEL SMALL.CODE SUBPROG1 PROC FAR... RET SUBPROG1 ENDP END

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