You are on page 1of 13

Chapter 6 8088/8086 Microprocessor Programming 2

6.1 Flag Control Instructions The 8088 microprocessor has a set of lags to either monitor the status of executing instructions or control options available in its operation. The instruction set includes a group of instructions that when executed directly affect the state of the flags. The instructions are: Load AH from flags (LAHF) Store AH into flags (SAHF) Clear carry (CLC) Set carry (STC) Complement carry (CMC) Clear interrupt (CLI) Set interrupt (STI) 6.2 Compare Instruction

The compare (CMP) instruction is used to compare two 8-bit or 16-bit numbers. It enables us to determine the relationship between two numbers-whether they are equal or unequal and when they are unequal, which one is larger. The operands for the instruction can be a storage location in the memory, a register within the MPU or a part of the instruction. The result of the comparison is reflected by the changes in six of the status flags of the 8088.the new logic states of these fags can be sued to determine whether or not to alter the sequence of the program execution. The process of comparison is basically a subtraction operation. The source operand is subtracted from the destination operand. Based on the result of the subtraction operand, the appropriate flags are set or reset.

6.3 Jump Instructions The purpose of a jump instruction is to alter the execution path of instructions in the program. The code segment register and instruction pointer keep track of the next instruction to be fetched for execution. So a jump involves altering the contents of the register. In this way an execution continues at an address other than the next sequential instruction. No return linkage is saved when the jump takes place. Unconditional and Conditional Jump Instructions In an unconditional jump, no status requirements are imposed for the jump to occur. In a conditional jump instruction, the status conditions that exist at the time of the jump instruction is executed decide whether or not the jump will occur. If the condition or conditions are met, the jump takes place otherwise the sequential flow of execution is followed. A branch is a point in the program where a choice is made between two paths of execution. The branch is a frequently used program construct and is sometimes referred to as an IFTHEN-ELSE structure

. Unconditional jump instruction There are 2 types of unconditional jumps: Intrasegment jump is limited to addresses within the current code segment. Intersegment jump permits jumps from one code segment to another Jump instructions specified with a Short Label, near-Label, Memptr16 or Regptr16 operand represent intrasegment jumps. The Short-label and the Near-Label operands specify the signed displacement of the next instruction to be executed from the address of the jump instruction itself. Conditional Jump Instruction This type performs conditional; jump operations. All conditional jump instructions work in a similar way except that they test different conditions to decide whether or not the jump can take place. Examples of conditions are: the contents of CX are zero, an overflow has occurred or the result is negative.

The above figures give the general form of the conditional jump instruction and also a list of the instructions in the 8088s instruction set .To distinguish between comparisons of signed and unsigned numbers by jump instructions, two different names which seem to

imply the same, have been devised. They are above and below, for comparison of unsigned numbers and less and greater for comparison of signed numbers. Branch Program structure-IF-THEN The following are a few examples of how the conditional jump instruction can be used to implement the software branch program structure called If-Then. If then Branch condition using flag-condition test

If-Then branch program structure using Register bit test

If-then Branch program structure using an alternative register-bit test

Loop program Structures-REPEAT_UNTIL and WHILE-DO In many applications, a group of instructions may need to be executed over and over again until a condition is satisfied. A loop structure called REPEAT-UNTIL can be used for this purpose.

Here, the sequence of instructions from label AGAIN to the conditional jump instruction Jcc represents the loop. The label for the instruction that is to be jumped to is located

before the jump instruction that makes the conditional test. If the test result is true, program control returns to AGAIN and the segment of the program repeats. This continues until the condition specified by cc is false. Before entering the loop the register that is to be used for the conditional test must be loaded with the appropriate count. Another loop structure is the WHILE-DO loop. To implement this loop both a conditional and unconditional jump instruction is used.

An example program to illustrate block transfer of data.:

Subroutines and Subroutine-handling Instructions A subroutine is a special segment of program that can be called for execution from any point in the program.

The program structure is such that there is one part of the program called main program. In addition to this there is a program segment attached to the main program called the subroutine. The subroutine is written to provide a function that must be performed at various points in the main program. Instead of including this piece of code in the main program each time the function is needed, it is put into the program once as a subroutine. A subroutine is also known as a procedure. A single call instruction is used in the main

program to call the function and a return instruction is included at the end of the subroutine to initiate a return sequence to the main program. CALL and RET instruction CALL allows implementation of the 2 types of operations: the intersegment and intrasegment call depending on the operand. In all cases, the execution of the instruction causes the contents of IP to be saved on the stack. The saved value of IP is the offset address of the instruction that follows the CALL instruction. After saving the return address, a new 16-bit value, which is specified by the instructions operand and corresponds to the storage location of the first instruction of the subroutine, is loaded. into IP. The types of operands represent different ways of specifying a new value of IP. Every subroutine must end by executing an instruction that returns control to the main program. This is the return (RET) instruction. Its execution causes the value of IP or both the values of IP and CS that were saved on the stack to be returned to their corresponding registers and the stack pointer to be adjusted accordingly. PUSH and POP Instructions: Upon entering a subroutine, we find that it is usually necessary to save the contents of certain registers or some other main program parameters. The values are saved by pushing them onto the stack. The instruction that is used to save parameters on the stack is the PUSH instruction and that which is used to retrieve them is called POP.

To save contents of the flag register, the push flags (PUSHF) and pop flag (POPF) instructions are used. Loop and the Loop Handling Instructions The 8088 microprocessor have instructions specifically designed for implementing Loop operations.

The first instruction Loop works with respect to the contents of the CX register. CX is preloaded with the contents of the count of the number of times the loop is executed. Whenever Loop is executed, the contents of CX are decremented and checked to determine if it is equal to 0. If equal then the loop is complete and the instruction following the loop is executed. In this way, the Loop functions as a decrement CX instruction followed by a JNZ instruction. The other 2 loop instructions operate in a similar way except that they check for 2 conditions. The loop while equal checks the contents of both CX and Zero Flag. It checks ZF and CX looking for ZF equal to zero and the CX flag not equal to zero.

Strings and String Handling Instructions

The 8088 microprocessor is equipped with special instructions to handle string instructions. By string we mean a series of data words that reside in consecutive memory locations. The basic 5 string instructions in the instruction set of 8088 are move byte or word string (MOVSB/MOVSW), compare string (CMPSB/CMPSW), scan string (SCASB/SCASW), load string (LODSB/LODSW) and store strings (STOSB/STOSW). They define an operation for one element of a string. Thus these operations must be repeated to handle a string of more than one element. Move String - MOVSB, MOVSW An element of the string specified by the source index (SI) register with respect to the current data segment register is moved to the location specified by the destination register with respect to the current extra segment register. This move can be performed on a byte or a word of data. After the move is complete, the contents of both SI and DI are automatically incremented or decremented by 1 for a byte move and by 2 for a word move. Compare String and Scan String- CMPSB/CMPSW This instruction is used to compare 2 elements in the same or different strings; It subtracts the destination operand from the source operand and adjusts the flags accordingly. The source element is pointed by SI with respect to the current value in DS and the destination element is specified by the contents of DI relative to the contents of ES. When executed, the operands are compared, the flags adjusted and both SI and DI are updated so that they point to the next elements in their respective strings. Load and Store String-LODSB/LODSW and STOSB/STOSW These instructions are specifically used to move string elements between the accumulator and the memory. LODSB loads a byte form the string in the memory into AL. The LODSW indicates that the word-string element at the physical address derived form DS and SI are to be loaded into AX. STOSB on the other hand stores a byte from AL into a string location in memory. This time , the contents of ES and DI are used to form the address of the storage location in memory. Repeat String-REP

In most applications, the basic string operations must be repeated in order to process arrays of data. This is done by inserting a repeat prefix before the instruction that is to be repeated. The repeat prefixes are REP, REPE/REPZ and REPNE/REPNZ. Auto indexing for String instructions The 8088 provides 2 instructions, clear direction flag(CLD) and set direction flag(STD) to permit selection between auto increment mode and auto decrement mode of operation. When CLD is executed, DF is set to 0. This selects the auto increment mode and each time a string operation is performed SI and/or DI are incremented by 1 byte if byte data are processed and by 2 if word data are processed.

You might also like