1 / 65

Chapter 6

Chapter 6. Program Control Instruction. Introduction. program control instruction : direct the flow of a program, allow the flow to change jumps, calls, returns, interrupts, machine control instructions change in flow : CMP, TEST followed by conditional jump

pascal
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Program Control Instruction

  2. Introduction • program control instruction : • direct the flow of a program, allow the flow to change • jumps, calls, returns, interrupts, machine control instructions • change in flow : • CMP, TEST followed by conditional jump • relational assembly language statements : • .IF, .ELSE, .ELSEIF, .WHILE, .ENDW, .REPEAT, .UNTIL • MASM, TASM Ver.6X ~ • allow to develop control flow portions of program with C/C++ language efficiency Ch.6 Program Control Instructions

  3. 6-1 The Jump Group • JMP(jump) : allow to skip sections of a program and blanch to any part of memory for next instruction • unconditional jump, conditional jump • three type unconditional jump : Fig. 6-1

  4. Unconditional Jump(JMP) • intrasegment jump : short, near jump • Short jump(2-byte): 1 byte disp.(within +127~-128 byte) • Near jump(3-byte) : 2 byte disp.(within 32K bytes or anywhere in current code segment) • segments : cyclic in nature • intersegment, far jump(5-byte) : • any memory location within the real memory system • 80386~ (in protected mode) • Near(5-byte) : 4 byte displacement(within 2G bytes) • Far(7-byte) : 4 byte(EIP), 2 byte(CS) Ch.6 Program Control Instructions

  5. Short Jump • short jump : relative jump • distance or displacement : follow the opcode • one-byte signed number(+127~-128) : • sign-extended and added to IP/EIP • to generate the jump address within current code segment • EX. 6-1 : • label : symbolic name for memory address • SHORT directive : force a short jump • most assembler : choose best form of jump instruction • JMP START : assemble as a short jump Ch.6 Program Control Instructions

  6. Short Jump • 1st jump : 0020H – 0009H = 0017(disp. = 17H) • 2nd jump : 0002H – 0024H = FFDEH(disp. = DEH) Ch.6 Program Control Instructions

  7. Fig. 6-2 • Fig. 6-2 Ch.6 Program Control Instructions

  8. Near, Far Jump • near jump : relocatable because relative jump • signed displacement : added to IP/EIP to generate the jump address • 2 byte : 32K bytes in current code segment • 4-byte(386~ in protected mode) : 2G bytes • far jump : 5(7, 80386~) byte instruction • new offset address(IP/EIP) : byte 2,3(2~5) • new segment address(CS) : byte 4,5(6,7) • 80286~ in protected mode : CS access a descriptor that contain base address of far jump segment Ch.6 Program Control Instructions

  9. Fig. 6-3 • Fig. 6-3 Ch.6 Program Control Instructions

  10. EX. 6-2 : Near Jump • E9 0200 R JMP NEXT : only list file • R : denote a relocatable jump address of 0200H • actual machine code : E9 F6 01 • 0200H - 000AH = 01F6H Ch.6 Program Control Instructions

  11. Fig. 6-4 • Fig. 6-4 Ch.6 Program Control Instructions

  12. EX. 6-3 • far jump : FAR PTR directive, far label • far label : external to current code segment • EXTRN UP:FAR directive • a global label as a double colon(LABEL::) • ----E : external. filled in by linker when links program files

  13. Indirect Jump • jump with 16-, 32-bit reg. operand : indirect jump • contents of reg. : transferred directly into IP/EIP • JMP AX : IP ← AX, JMP EAX : EIP ← EAX • EX. 6-4 : how JMP AX access jump table • read a key, converted ASCII to binary, doubled • jump table : 16-bit offset address • Indirect Jumps using Index : double-indirect jump • [ ] form of addressing to directly access jump table • near jump JMP TABLE[SI] : IP ← [SI+TABLE] • far jump JMP FAR PTR [SI], JMP TABLE [SI] with TABLE data defined DD directive Ch.6 Program Control Instructions

  14. EX. 6-4 • EX. 6-4

  15. EX. 6-5 • EX. 6-5

  16. Conditional Jumps • conditional jump : short jump • ~ 80286(short jump) : +127 ~ -128 • 80386 ~(short, near jump) : 1, 4 bytes • test one flag bit or some more : S, Z, C, P, O • if condition under test is true : branch to the label • if condition is false : next sequential instruction • relative magnitude comparisons : • require more complicated conditional jump instructions that test more than one flag bit • Table 6-1 : conditional jump instructions Ch.6 Program Control Instructions

  17. Table 6-1 • Table 6-1

  18. Fig. 6-5 • Fig. 6-5 : order of signed, unsigned 8-bit no.s Ch.6 Program Control Instructions

  19. Conditional Jumps • unsigned : FFH is above 00H, above, below, equal • signed : FFH less than 00H, greater, less, zero • alternate form : • JE = JZ • JA(if above) = JNBE(if not below or equal) • JCXZ(jump if CX = 0), JECXZ(jump if ECX=0) • if CX/ECX = 0 : jump occur • if CX/ECX <> 0 : no jump occur • EX. 6-6 : search table for 0AH using SANSB, JCXZ Ch.6 Program Control Instructions

  20. EX. 6-6 • EX. 6-6 Ch.6 Program Control Instructions

  21. Conditional Set Instructions • conditional set instructions : • 80386~ • set a byte to either a 01H or clear a byte to 00H • useful where a condition must be tested at a point much later in the program • SETNC MEM : • places a 01H into memory location MEM if carry is cleared and • a 00H into MEM if carry is set • Table 6-2 : Ch.6 Program Control Instructions

  22. Table 6-2 • Table 6-2

  23. LOOP, Conditional LOOP • LOOP : combination of decrement CX and JNZ • ~ 80286 : DEC CX ; if CX <> 0, jump to label if CX = 0, execute next sequential instruction • 80386 ~ : CX/ECX depending on instruction mode • LOOPE(loop while equal, LOOPZ) : • jump if CX <> 0 while equal condition exist • exit the loop if CX = 0 or condition is not equal • LOOPNE(loop while not equal, LOOPNZ) : • jump if CX <> 0 while not-equal condition exist • exit the loop if CX = 0 or condition is equal • LOOPEW/LOOPED,LOOPNEW/LOOPNED:override mode Ch.6 Program Control Instructions

  24. EX. 6-7 • EX. 6-7 :

  25. 6-2 Controlling the Flow of an Assembly Language Program • relational statements • .IF, .ELSE, .ELSEIF, ENDIF, .REPEAT-.UNTIL, .WHILE-.ENDW : • easier to control the flow than conditional jump • EX. 6-8 : testing system for version of DOS • DOS INT 21H, function no. 30H : read DOS ver. • (a) : source program, (b) fully expended assembled • * : assembler-generated and -inserted statements • && : logical AND • Table 6-3 : relational operator Ch.6 Program Control Instructions

  26. Table 6-3 • Table 6-3 Ch.6 Program Control Instructions

  27. EX. 6-10 • EX. 6-10 : read a key, convert to hexadecimal • `a`(61H), `A`(41H) : 61H(41H)-57H(37H)=0AH

  28. DO-WHILE Loops • .WHILE statement : used with a condition to begin the loop • EX. 6-11 : read a key, store into array called BUF until enter key(0DH) is typed • DOS 21H, fn no. 09H Ch.6 Program Control Instructions

  29. EX. 6-11 • EX. 6-11

  30. REPEAT-UNTIL Loops • .REPEAT : defined start of loop • .UNTIL : defined end of loop, contained condition • EX. 6-14 : EX. 6-11,12 Ch.6 Program Control Instructions

  31. EX. 6-14 • EX. 6-14

  32. 6-3 Procedures • Procedure : • a group of instructions that usually performs one task • a reusable section of the software that is stored in memory once, but used as often as necessary • advantage : • save memory space • make it easier to develop software • disadvantage : • take the computer a small amount of time to link to procedure and return from it • CALL/RET : link to/return from the procedure Ch.6 Program Control Instructions

  33. Procedure • CALL : push the address of instruction following CALL(return address) on stack • RET : remove an address from stack so the program return to instruction following CALL • specific rules for storing procedure • begin with PROC, end with ENDP directive • each directive : appear with name of procedure • PROC : followed by type of procedure : NEAR,FAR • type :can be followed by the USES statement • USES statement : allow any no. of reg. to be automatically pushed and popped within procedure Ch.6 Program Control Instructions

  34. EX. 6-16 • EX. 6-16 Ch.6 Program Control Instructions

  35. CALL • near return(C3H) : remove 16-bit no. from stack, place it into IP to return from procedure in current segment • far return(CBH) : remove 32-bit no. from stack, place it into both IP, CS to return from procedure to any memory location • far procedure : global, used by all software • near procedure : local, used by a given task • CALL : differ from jump instruction • because a CALL save a return address on stack Ch.6 Program Control Instructions

  36. Near CALL • near CALL : 3(5, 80386~ in protected mode)-byte instruction • 1st byte : opcode • 2nd, 3rd byte : displacement(distance) of 32K • 2nd~5th byte : 32-bit displacement of 2G bytes • near CALL execute : • push offset address of next instruction(IP/EIP) on stack • add displacement from byte 2,3(2~5) to IP/EIP to transfer control to the procedure • CALLN(near CALL) • Fig. 6-6 : Ch.6 Program Control Instructions

  37. Fig. 6-6 • Fig. 6-6 Ch.6 Program Control Instructions

  38. Far CALL • far CALL : 5(7, 80386~ in protected mode)-byte instruction • 1st byte : opcode • 2nd 3rd byte : new IP, 4th 5th byte : new CS • 2nd~5th byte : new EIP, 6th 7th byte : new CS • far CALL execute : • push IP/EIP, CS on stack • place byte 2,3(2~5) to IP/EIP and byte 4,5(6,7) to CS to call a procedure located anywhere in memory system • CALLF(far CALL) • Fig. 6-7 Ch.6 Program Control Instructions

  39. Fig. 6-7 • Fig. 6-7 Ch.6 Program Control Instructions

  40. CALLs with Register, indirect address • CALL with register operand : • like jump, also contain a register operand • CALL BX : push IP, jump to offset address located in BX(IP ← BX) in current code segment • CALL with indirect memory address : • useful whenever different subroutines need to be chosen • CALL : also reference far pointers • CALL FAR PTR [SI] or CALL TABLE[SI] • data in table : defined as doubleword data with DD • retrieve a 32-bit address from data segment addressed by SI, use it as address of a far procedure Ch.6 Program Control Instructions

  41. EX. 6-17 • EX. 6-17 : display ‘OK’

  42. RET • RET : real mode(80386~ in protected mode) • near RET: remove 16-bit(32-bit), place it into IP/EIT • far : remove 32-bit(6 bytes), place it into IP/EIP, CS • near, far return : defined in procedure’s PROC • other form : RET n • n(bytes) : add n to contents of SP after return address is removed from stack • push passing parameters on stack before calling procedure • if these parameters are to be discarded upon return, RET contains a no. that represents the no. of bytes pushed to stack as parameters Ch.6 Program Control Instructions

  43. Fig. 6-8 : near return • Fig 6-8 Ch.6 Program Control Instructions

  44. EX. 6-19 • EX. 6-19 Ch.6 Program Control Instructions

  45. RET • RETN : CALLN • RETF : CALLF • passing parameters to a procedure : • 1. to use one of the CPU register : MOV CX, TI • 2. to use a memory location : MOV TEMP, TI • 3. to pass the address of memory location : MOV SI, OFFSET TI • 4. to pass the parameters on the stack : (EX. 6-19) MOV DX, TI, PUSH DX • 5. to use stack frame : ENTER, LEAVE(p.211) Ch.6 Program Control Instructions

  46. 6-4 Introduction to Interrupt • 1. hardware-generated CALL : external interrupt • externally derived from a hardware signal • 2. software-generated CALL : internal, exception • internally derived from the execution of an instruction or by some other internal event() • interrupt : interrupts the program by calling an interrupt service procedure or interrupt handler • interrupt vector : • in real : 4-byte no. stored in 1st 1024 bytes(~0003FFH) 256(00H~FFH)  4byte = 1024byte • protected : replaced by interrupt descriptor table Ch.6 Program Control Instructions

  47. Interrupt Vectors • Table 6-4 : 256 different interrupt vectors in real • each contain address of an interrupt service procedure for IP, CS • Intel reserve the 1st 32 interrupt vector(~1FH) : for present , future µ • remaining : available for user • some of reserved : for error that occur during execution of software, such as divide error interrupt • some of reserved : for coprocessor • others : occur for normal events in the system • vectors 1-6,7,9,17 : function in real, protected mode • remaining : only in protected mode Ch.6 Program Control Instructions

  48. Table 6-4 • Table 6-4 Ch.6 Program Control Instructions

  49. Interrupt Instructions • software interrupt instruction : special type of CALL • INT, INTO, INT3 • each of these instruction : • 1. in real, fetches vector from interrupt vector table • 1. in protected, fetches an interrupt descriptor from interrupt descriptor table • 2. calls the interrupt service procedure • interrupt call : similar to far CALL instruction • because placed return address(IP/EIP, CS) on stack • different : pushed flags, then pushed return address • fetched new value IP/EIP, CS from vector Ch.6 Program Control Instructions

  50. INTs • INT n : 256 different software interrupt instruction • type no. n : 0 ~ 255(00H ~ FFH) • INT 100 : uses interrupt vector no. 100(64H) • memory address in IVT : 190H~193H • 0110 0100(64H) → shift left 2 → 01 1001 0000(190H) • address of interrupt vector in real : • multiplying type no. times 4(each vector : 4 bytes) • address of interrupt descriptor in protected : • multiplying type no. times 8(each descriptor : 8 byte) • INT : 2-byte long(1st:opcode, 2nd:vector type no.) • INT 3: 1-byte special software interrupt for breakpoints Ch.6 Program Control Instructions

More Related