Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Program Control Instructions Prima Dewi Purnamasari Microprocessor Department of Electrical Engineering University of Indonesia."— Presentation transcript:

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

2 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

3 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

4  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

5 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

6 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

7 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

8  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

9 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

10 Figure 6 – 3 A near jump that adds the displacement (0002H) to the contents of IP.

11  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.

12 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

13 Figure 6 – 4 A far jump instruction replaces the contents of both CS and IP with 4 bytes following the opcode.

14 Short JUMP 0000 33 DB XOR BX, BX 0002 B8 0001 START: MOV AX, 1 0005 03 C3AND AX, BX 0007 EB 17JMP SHORT NEXT 0020 8B DBNEXT:MOV BX, AX 0022 EB DEJMP START 0009H+17H = 0020H IP = 0007H + 02H = 0009H

15 Near JUMP 0000 33 DB XOR BX, BX 0002 B8 0001 START: MOV AX, 1 0005 03 C3AND AX, BX 0007 E9 0200 RJMP NEXT 0200 8B DBNEXT:MOV BX, AX 0202 E9 0002 RJMP START

16 Far JUMP EXTRN UP:FAR 0000 33 DB XOR BX, BX 0002 B8 0001 START: ADD AX, 1 0005 E9 0200 JMP NEXT 0200 8B DBNEXT:MOV BX, AX 0202 EA 0002 ---- RJMP FAR PTR START 0207 EA 0000 ---- EJMP UP ; R stands for relocatable ;E stands for external ;these addresses are established during the linking process

17 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

18 .MODEL SMALL.DATA 0000 0030 RTABLE: DW ONE 0002 0034 R DW TWO 0004 0038 R DW THREE.CODE.STARTUP 0017 TOP:MOV AH, 1 0019 INT 21 ;read into AL 001BSUB AL, 31 001DJB TOP 001FCMP AL, 2 0021JA TOP 0023MOV AH, 0 0025ADD 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

19 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 6.5 19

20 20 Example 6.5.MODEL SMALL ;select small model 0000.DATA;start of data segment 0000 0030 RTABLEDW ONE;define lookup table 0002 0034 RDW TWO 0004 0038 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,2 0021 77 F4JA TOP;if above ‘3’ typed 0023 B4 00MOV AH,0;calculate table address 0025 03 C0ADD AX,AX 0027 03 F0ADD SI,AX 0029 FF A4 0000 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

21 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 6.2 21

22  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

23  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

24

25 25

26 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

27 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

28 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 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 0000 0064 [BLOCK1 DW100 DUP (?);100 bytes for BLOCK1 0000 ] 00C8 0064 [BLOCK2 DW100 DUP (?);100 bytes for BLOCK2 0000 ] 0000.CODE;start of code segment.STARTUP;start of program 0017 8C D8MOV AX,DS;overlap DS and ES 0019 8E C0MOV ES,AX 001B FC CLD ;select increment 001C B9 0064MOV CX,100;load count 100 001F BE 0000 RMOV SI,OFFSET BLOCK1;address BLOCK1 0022 BF 0000 RMOV DI,OFFSET BLOCK2;address BLOCK2 0025 L1: 0025 ADLODSW;load AX with BLOCK1 0026 26: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

30 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 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<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:

32 32 Example 6.9 [c2]

33 33 Example 6.10 ; Program yang membaca sebuah key dan menyimpan dalam Hexadecimal ; Nilai pada memori lokasdi TEMP..Mode Small ; Pilih model SMALL 0000. Data ;Memulai data segmen 0000 00TEMP 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 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

34 34

35 35

36 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)

37 Example SUMS PROC NEAR ADDAX,BX ADDAX,CX ADDAX,DX RET SUMSENDP

38  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

39 Figure 6 – 6 The effect of a near CALL on the stack and the instruction pointer.

40 Figure 6 – 7 The effect of a far CALL instruction.

41 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

42 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

43 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

44  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

45  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

46  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


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

Similar presentations


Ads by Google