Microprocessors Chapter 7 Program Control Instructions prepared by Dr. Mohamed A. Shohla Chapter Overview • • • • • • • جام عة الم نوف ية The Jump Group Unconditional Jump (JMP) Conditional Jumps and Conditional Sets Loop Procedures CALL RET Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6-2 The Jump Group • The main program control instruction, 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 instruction allows the programmer to make decisions based upon numerical tests. • The results of numerical tests are held in the flag bits, which are then tested by conditional jump instructions. جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6-3 Unconditional Jump (JMP) • Three types of unconditional jump instructions are available to the microprocessor: short jump, near jump, and far jump. • The short jump is a two-byte instruction that allows jumps or branches to memory locations within +127 and -128 bytes from the address following the jump. • The three-byte near jump allows a branch or jump within ±32K bytes from the instruction in the current code segment. • Finally, the five-byte far jump allows a jump to any memory location within the real memory system. The short and near jumps are often called intrasegment jumps, and the far jumps are often called intersegment jumps. جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6-4 Short Jump: • Short jumps are called relative jumps because they can be moved, along with their related software, to any location in the current code segment without a change. • جام عة الم نوف ية A short jump to four memory locations beyond the address of the next instruction. Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6-5 Near Jump: • The near jump is similar to the short jump, except that the distance is farther. • A near jump passes control to an instruction in the current code segment located within ±32K bytes from the near jump instruction. The distance is ±2G in the 80386 and above when operated in protected mode. A near jump that adds the displacement (0002H) to contents of IP. جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6-6 Far Jump: • A far jump instruction obtains a new segment and offset address to accomplish the jump. •A far jump instruction replaces the contents of both CS and IP with four bytes following the opcode. جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6-7 Conditional Jumps and Conditional Sets • • Conditional jumps are always short jumps in the 8087-80286 microprocessors. This limits the range of the jump to within +127 bytes and -128 bytes from the location following the conditional jump. In the 80386/80486 conditional jumps are either short or near jumps. Instruction JA JAE JB JBE JC JE or JZ JG JGE JL JLE JNC JNE or JNZ JNO JNS JNP/JPO JO JP/JPE JS JCXZ JECXZ جام عة الم نوف ية Condition Tested C = 0 and Z = 0 C=0 C=1 C = 1 or Z = 1 C=1 Z=1 Z = 0 and S = 0 S=0 S≠0 Z = 1 or S ≠ 0 C=0 Z=0 0=0 S=0 P=0 0=1 P= 1 S=1 CX = 0 ECX = 0 Comment Jump above Jump above or equal to Jump below Jump below or equal to Jump carry set Jump equal to or jump zero Jump greater than Jump greater than or equal to Jump less than Jump less than or equal to Jump carry cleared Jump not equal to or jump not zero Jump no overflow Jump no sign Jump no parity/jump parity odd Jump on overflow Jump on parity/jump parity even Jump on sign Jump if CX = 0 Jump if ECX = 0 (80386/80486 only) Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6-8 •A procedure that searches a table of 100 bytes for a 0AH SCAN PROC NEAR MOV SI, OFFSET TABLE MOV CX,100 MOV AL,OAH CLD REPNE SCASB JCXZ NOT_FOUND RET SCAN ENDP جام عة الم نوف ية ;address TABLE ;load count ;load search data Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6-9 Loop • The LOOP instruction is a combination of a decrement CX and a conditional jump. • LOOP decrements CX and if CX <> 0, it jumps to the address indicated by the label. • If CX becomes a 0, the next sequential instruction executes. جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 10 •A procedure that adds word in BLOCK1 and BLOCK2 ADDS PROC NEAR MOV CX,100 MOV SI,OFFSET BLOCK1 MOV DI,OFFSET BLOCK2 AGAIN: LODSW ADD AX,ES:[DI] STOSW LOOP AGAIN ADDS ENDP جام عة الم نوف ية ;load count ;address BLOCK1 ;address BLOCK2 ;get BLOCK1 data ;add BLOCK2 data ;store in BLOCK2 ;repeat 100 times RET Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 11 Procedures • • • • • The procedure or subroutine is an important part of any computer system’s architecture. A procedure is a group of instructions that usually performs one task. A procedure is a reusable section of the software that is stored in memory once, but used as often as necessary. This saves memory space and makes it easier to develop software. The only disadvantage of a procedure is that it takes the computer a small amount of time to link to the procedure and return from it. Example SUMS SUMS جام عة الم نوف ية PROC ADD ADD ADD RET ENDP NEAR AX, BX AX, CX AX, DX Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 12 • • • CALL 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 follows the CALL in a program when a RET instruction executes. The effect of a near call instruction on the stack and the SP, SS, and IP registers. Notice how the old IP is stored on the stack. جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 13 • • RET The return instruction (RET) removes either a 17-bit number (near return) from the stack and places it into IP or a 32-bit number (far return) and places it into IP and CS. The near and far return instructions are both defined in the procedure's PROC directive. This automatically selects the proper return instruction. The effect of a near RET instruction on the stack and the SP, SS, and IP registers. جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 14 Chapter 7 Questions and Answers جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 15 Which type of JMP instruction (short, near, or far) assembles for the following: (a) if the distance is 0210H bytes (b) if the distance is 0020H bytes (c) if the distance is 10000H bytes (a) near (b) short (c) far جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 16 Explain what the JMP AX instruction accomplishes. Also identify it as a near or a far jump instruction. • جام عة الم نوف ية The JMP AX instruction is a near jump to the offset address loaded in AX. Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 17 Contrast the operation of a JMP [DI] with a JMP FAR PTR [DI]. The JMP [DI] instruction is a near jump that obtain the jump address from the data segment memory location addressed by DI. The other jump is a far jump. جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 18 (Develop a short sequence of instructions that stores a 00H into 150H bytes of memory, beginning at extra segment memory location DATA. You must use the LOOP instruction to help perform this task. MOV CLD MOV MOV DI, OFFEST DATA CX, 150H AL, 0 AGAIN: STOSB LOOP AGAIN جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 19 Develop a sequence of instructions that searches through a block of 100H bytes of memory. This program must count all the unsigned numbers that are above 42H and all that are below 42H. Byte-sized data segment memory location UP must contain the count of numbers above 42H, and data segment location DOWN must contain the count of numbers below 42H. UP DB DOWN DB MOV MOV MOV AGAIN:CMP JA INC JMP L1: INC L2: DEC LOOP جام عة الم نوف ية 0 0 DI, OFFSET BLOCK CX, 100 AL, 42H AL, [DI] L1 DOWN L2 UP DI AGAIN ;address data ;load counter ;put 66 IN AL ;search Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 20 Develop a sequence of instructions that shows how data in one block of memory (BLOCK1) adds to data in a second block (BLOCK2) using LOOP to control how many numbers add. Store the result over top of data in BLOCK2 ;procedure that adds word in BLOCK1 and BLOCK2 ADDS PROC NEAR MOV CX,100 ;load count MOV SI, OFFSET BLOCK1 MOV DI, OFFSET BLOCK2 AGAIN: LODSW ;get BLOCK1 data ADD AX,ES:[DI] ;add BLOCK2 data STOSW ;store in BLOCK2 LOOP AGAIN ;repeat 100 times RET ADDS ENDP جام عة الم نوف ية Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 21 Write a program to fill 1000H-byte block of memory in the extra segment beginning at address BLOCK with the data byte 20H (ASCII space). MOV MOV MOV CLD REP جام عة الم نوف ية CX,1000H DI, OFFSET BLOCK AL, 20H ;load count ;address BLOCK STOSB Faculty of Electronic Engineering – Dept. of Computer Science & Eng. Microprocessors Course 6 - 22