Microprocessor and Assembly Language Program Control Instructions Program
Microprocessor and Assembly Language Program Control Instructions
Program Control Instructions • The program control instructions direct the flow of a program and allow the flow to change • Allows programmer to skip program sections and branch to any part of memory for the next instruction • Type of program control instructions – Unconditional transfers – Conditional transfers – Subroutine call and return
Unconditional Jump (JMP) • JMP instruction unconditionally transfers control to another point in the program • Types of unconditional jump instructions: – short jump: allows jumps or branches to memory locations within +127 and – 128 bytes from the address following the jump – near jump: allows a branch or jump within ± 32 K bytes (or anywhere in the current code segment) – far jump: 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
JMP Format • Shot, near and far jump instructions generally use the same syntax, it is: jmp target • The assembler differentiates them by their operands: – – – jmp jmp jmp disp 8 ; direct intrasegment(short), 8 bit displacement disp 16 ; direct intrasegment(near), 16 bit displacement adrs 32 ; direct intersegment(far), 32 bit segmented address mem 16 ; indirect intrasegment (near), 16 bit memory operand. reg 16 ; register indirect intrasegment(near). mem 32 ; indirect intersegment(far), 32 bit memory operand. • The first form consists of an opcode and a single byte displacement. The CPU sign extends this displacement to 16 bits and adds it to the IP register • The second form: The CPU simply adds the two byte displacement to the IP register. • In third form: the address following the opcode is the absolute memory address of the target instruction. This instruction loads cs: ip with a 32 bit immediate value. • The direct jumps are normally specify the target address using a statement label. A statement label is usually an identifier followed by a colon • The assembler automatically computes the distance from the jump instruction to the statement label
Short JMP Example 1: XOR BX, BX START: MOV AX, 1 ADD AX, BX JMP NEXT; or JMP short NEXT <skipped memory locations> NEXT: MOV BX, AX JMP START Example 2:
Near JMP • A near jump that adds the displacement (0002 H) to the contents of IP
Far JMP • A far jump instruction replaces the contents of both CS and IP with 4 bytes following the opcode • FAR PTR directive indicates a far jump instruction ; JMP FAR PTR UP • Define far (global) label: – label UP is defined as a far label by the EXTRN UP: FAR directive – Or using UP: : ……. . – Ex. 1 : EXTRN UP: FAR NEXT: MOV BX, AX JMP UP – Ex. 2: NEXT: MOV BX, AX JMP UP ; <skipped memory locations> UP: : MOV BX, AX
Indirect Jump • The address of the jump is in the register specified by the jump instruction , or memory location • Unlike displacement associated with the near jump, register contents or memory location contents are transferred directly into the instruction pointer • Indirect JMP using Register: – 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 • Ex. : TABLE: DW ONE ; jump table DW TWO MOV SI, OFFSET TABLE MOV AX, [SI] JMP AX ONE: MOV DL, ’ 1’ JMP Start TWO: MOV DL, ’ 2’ JMP Start ; address TABLE ; get ONE, TWO ; jump to ONE, TWO ; get ASCII 1 ; get ASCII 2
Indirect Jumps Using an Index • Jump instruction may also use the [ ] form of addressing to directly access the jump table. • The jump table can contain offset addresses for near indirect jumps, or segment and offset addresses for far indirect jumps. • The assembler assumes that the jump is near unless the FAR PTR directive indicates a far jump instruction • Ex. : TABLE: DW ONE ; jump table DW TWO MOV SI, 1 JMP TABLE[SI] ONE: MOV DL, ’ 1’ JMP Start TWO: MOV DL, ’ 2’ JMP Start ; jump to ONE ; get ASCII 1 ; get ASCII 2
Conditional Jumps • Conditional jump instructions test flag bits: – sign (S), zero (Z), carry (C) – parity (P), overflow (0) • To compare numbers there two sets of compare instruction: – compare signed numbers – compare unsigned numbers • you will probably execute a conditional jump after a cmp instruction • 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 • The conditional jump instructions only test flags, they do not affect any of the 80 x 86 flags. • Most conditional jump instructions are straightforward as they often test one flag bit. – although some test more than one • Allows a conditional jump to any location within the current code segment
Conditional Jumps • The conditional jumps that test one flag:
Instructions for Unsigned Comparisons • Terms above and below refer to unsigned numbers
Instructions for signed Comparisons • Terms greater than and less than refer to signed numbers
The JCXZ/JECXZ Instructions • The conditional jump instructions all test flag bits except for JCXZ (jump if CX = 0) JECXZ (jump if ECX = 0) • Instead of testing flag bits, JCXZ directly tests the contents of the CX register without affecting the flag bits, and JECXZ tests the contents of the ECX register. • For the JCXZ instruction, if CX = 0, a jump occurs, and if CX != 0, no jump occurs. • Likewise for the JECXZ instruction, if ECX = 0, a jump occurs; if ECX != 0, no jump occurs.
Loop • The LOOP instruction is a combination of a decrement CX and the JNZ conditional jump • LOOP instruction decrements either CX or ECX, depending upon the instruction mode, if CX != 0 (or ECX!=0), it jumps to the address indicated by the label. If CX (ECX) becomes 0, the next sequential instruction executes • Ex. : . Data LIST DB 100 DUP(0). CODE. STARTUP MOV CX, 100 MOV BL, 1 L 1: MOV BH, 6 ADD BH, LIST [BL] MOV LIST[BL], BH INC BL LOOP L 1. EXIT END
Procedures • A procedure is a group of instructions that usually performs one task. – subroutine, method, or function is an important part of any system’s architecture • A procedure is a reusable section of the software stored in memory once, used as often as necessary. – saves memory space and makes it easier to develop software
Procedures • Only disadvantage of procedure is time it takes the computer to link to, and return from it. – CALL links to the procedure; the RET (return) instruction returns from the procedure • CALL pushes the address of the instruction following the CALL (return address) on the stack. – the stack stores the return address when a procedure is called during a program • RET instruction removes an address from the stack so the program returns to the instruction following the CALL • With the assembler, there are specific rules for storing procedures: – A procedure 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 (Local) or FAR (Global)
Procedure Example SUMS PROC NEAR ADD AX, BX ADD AX, CX ADD AX, DX RET SUMS ENDP SUMS 1 PROC FAR ADD AX, BX ADD AX, CX ADD AX, DX RET SUMS 1 ENDP MOV BX, 1000 H MOV CX, 1000 H MOV DX, 1000 H CALL SUMS ADD AX, 1. . .
CALL Instruction • The CALL instruction transfers the flow of the program to the procedure • The CALL instruction differs from the jump instruction because a CALL saves a return address on the stack • The return address returns control to the instruction that immediately follows the CALL in a program when a RET instruction executes • Near Call: – identical to the form of the near jump instruction – it first pushes the offset address of the next instruction (IP or EIP register)onto the stack. WHY? – It then adds the displacement to the IP to transfer control to the procedure
Near CALL • The effect of a near CALL on the stack and the instruction pointer
Far CALL • Far CALL places the contents of both IP and CS on the stack before jumping to the address indicated by target. • The far CALL instruction is like a far jump because it can call a procedure stored in any memory location in the system.
Far CALL • The effect of a far CALL instruction
CALLs with Register Operands • • Like jump instructions, call instructions also may contain a register operand An example is the CALL BX instruction, which pushes the contents of IP onto the stack – It then jumps to the offset address, located in register BX, in the current code segment This type of CALL always uses a 16 -bit offset address, stored in any 16 -bit register except the segment registers Ex. : . MODEL TINY ; select tiny model. CODE ; start code segment. STARTUP MOV BX, OFFSET DISP ; load BX with offset DISP MOV DL, ’O’ ; display O CALL BX MOV DL, ’K’ ; display K CALL BX. EXIT ; Procedure that displays the ASCII character in DL DISP PROC NEAR MOV AH, 2 ; select function 2 INT 21 H ; execute DOS function 2 RET DISP END
RET Instruction • Removes a 16 -bit number (near return) from the stack placing it in IP, or removes a 32 -bit number (far return) and places it in IP & CS
- Slides: 24