# assemnly language for 8086 part 2 ## FLAG CONTROL The 8086 microprocessor has a set of flags that either monitors the status of executing instructions or controls options available in its operation. The instruction set includes a group of instructions that when executed directly affects the state of the flags. these instructions shown below: - LAHF (Load AH from flags) - SAHF (Store AH into flags) - CLC (Clear carry) - STC (Set carry) - CMC (Complement carry) - CLI (Clear interrupt) - STI (Set interrupt) ### LAHF Load the AH register with the value of flag register, the AH flag will equal to `AH= [7=StatusFlag][6=ZeroFlag][5= 0 always][4=auxiliary carryflag][3=0 always][2=Parity Flag][1=1 always][0=Carry flag]` ### SAHF store the value of AH into flag register with same bit order, to set all flags to logic 1 we load AH with **FFH** then execute ***SAHF*** ### CLC clear carry flag ### STC set carry flag to one ### CMC reverse the logic of **CF** , for example if `CF=1` we use CMC to make `CF=0` ### CLI stop hardware interrupt by making `*IF=0 ;*interrupt flag` ### STI This instruction set the interrupt flag to one --- ## COMPARE The comparison instruction (CMP) compares a **byte(8bit)** / **word(16bit)** from the specified source with a byte/word from the specified destination. * The source and destination both can't be memory locations. * The comparison is done by subtracting the source byte or word from the destination byte or word. But the result is not stored in the destination * Source and destination remain unchanged. only flags are updated.`Flags : The AF, OF, SF, ZF, PF and CF` are updated by the ***CMP*** instruction. --- ## TRANSFER This group of instructions will always cause the 8086 to fetch its next instruction from the **location specified** or indicated by instruction rather than from the next location after the JMP instruction. ### unconditional jump (JMP) there is two types of jump state divided by the change of IF * The first, called an intrasegment jump, is limited to addresses within the current code segment. This type of jump is achieved by just modifying the value in **IP**. * The other kind of jump. the intersegment jump, permits jumps from one code segment to another. Implementation of this type of jump requires modification of the contents of both **CS** and **IP** #### short , near and far label operands The Short-label and Near-label operands specify the jump relative to the address of the jump instruction itself ##### The Short-label in a Short-label jump instruction an 8-bit number is coded as an immediate operand to specify the signed displacement of the next instruction to be executed from the location of the jump instruction. When the jump instruction is executed, IP is reloaded with a new value equal to the updated value in IP. which is (IP) + 2, plus the signed displacement. The new value of IP and current value in CS give the address of the next instruction to be fetched and executed ##### The near-lable Near-label operands specify a new value for IP with a 16-bit immediate operand. This size of offset corresponds to the complete rangeof the current code segment. The value of the offset is automatically added to IP upon execution of the instruction. In this way, program control is passed to the location identified by the new IP * Example: JMP BX Uses the contents of register BX for the displacement. ##### The far label Far – label uses a 32-bit immediate operand to specify the jump to address. The first 16 bits of 32- bit pointer are loaded into IP and are an offset address relative to the contents of the code-segment register. * Example: JMP WORD [DI] ### Conditional Jump Conditional jumps are always **short** jumps in the 8086,The destination must be in the range of –128 bytes to +127 bytes from the address of the instruction after the conditional transfer instruction * Above and Below used for comparison of unsigned numbers. * Less and Greater used for comparison of signed numbers. ### Jump above (JA) if (CF = 0) and (ZF = 0) then **jump** Used after a *CMP* or *SUB* instruction, JA transfers control to short-label if the first operand (which should be unsigned) was greater thanthe second operand (also unsigned).The target of the jump must bewithin -128 to +127 bytes of the next instruction ### Jump above or equal (JAE ≡ JNB ≡ JNC) if `CF = 0 `then jump. ### Jump below (JB ≡ JC ≡ JNAE) Short Jump if first operand is Below second operand (as set by CMP instruction). for Unsigned. if `CF = 1` then jump. ### Jump below or equal (JBE = JNA) Short Jump if first operand is Below or Equal to second operand (as set by CMP instruction) for Unsigned. if `CF = 1 ;or ZF = 1 `then jump ### Jump equal (JE) Short Jump if first operand is Equal to second operand (as set by CMP instruction). **Signed**/**Unsigned**. if `ZF = 1 `then jump ### Jump greater (JG) Short Jump if first operand is Greater than second operand (as set by CMP instruction). for **Signed**. if `ZF = 0` and (SF=OF) then jump ### Jump greater or equal (JGE) Short Jump if first operand is Greater or Equal to second operand (as set by CMP instruction). for **Signed**. ### Jump less (JL) Short Jump if first operand is Less then second operand (as set by CMP instruction).for **Signed**. ### Jump less or equal (JLE) Short Jump if first operand is Less or Equal to second operand (as set by CMP instruction). for **Signed**. ### Jump carry (JC) Short Jump if **Carry flag** is set to 1. if `CF = 1` then jump ### Jump no carry (JNC) Short Jump if Carry flag is set to 0. ### Jump if CX is zero (JCXZ) Short Jump if CX register is 0. if `CX = 0 {(CF or ZF)=0}` then jump ### Jump not overflow instruction (JNO) if `OF = 0` then jump ### Jump not parity (JNP) Short Jump if No Parity (**odd**). Only 8 low bits of result are checked. Set by CMP, SUB, ADD, TEST, AND, OR, XOR instructions. ### Jump parity (JP) Short Jump if Parity (**even**). Only 8 low bits of result are checked. ### Jump not sign (JNS) Short Jump if Not Signed (if **positive**). Set by CMP ### Jump sign (JS) Short Jump if Signed (if **negative**) ### Jump not zero (JNZ) Short Jump if Not Zero (not equal zero). ### Jump zero (JZ) Short Jump if Zero (equal to zero) --- ## Counter OR Loop The 8086 microprocessor has three instructions specifically designed for implementing loop operations. These instructions can be use in place of certain conditional jump instruction and give the programmer a simpler wayof writing loop sequences. The loop instructions are listed below ### LOOP CX must be preloaded with a count representing the number of times the loop is to be repeated **Decrease CX**, jump to label if **CX not zero**. ### LOOPE Decrease CX, jump to label if CX not zero and Equal **(ZF = 1)**. ### LOOPNE Decrease CX, jump to label if CX not zero and Not Equal **(ZF = 0)**. ### LOOPZ Decrease CX, jump to label if CX not zero and ZF = 1 --- ## Stack (push and pop) the main use of push and pop to store values in register then use the *push* instruction to store the value in spearated place then change the register and do any operations and after that *pop* back register value to the old value. ## Subroutines A subroutine is a special segment of program that can be called for execution from any point in a program. ## CALL & RET the call (CALL) and return (RET) provide the mechanism for calling a subroutine into operation and returning control back to the main program at its completion * examples CALL **BX** RET When this instruction is executed, the contents of BX are loaded into IP and execution continues with the subroutine starting at a physical address derived from CS and the new value of IP CALL **[BX]** RET the instruction has its subroutine offset address at the memory location whose physical address is derived from the contents of DS and BX. The value stored at this memory location is loaded into IP every subroutine must end by executing an instruction that returns control to the main program. This is the return (RET) instruction --- ## ARRAY PROCESSING just store the offset in any reggister then put it in DS then define the size of array --- ## Strings String: A series of data words (or bytes) that reside in consecutive memory locations. ### MOVSB Copy byte at DS:[SI] to ES:[DI]. Update SI and DI. ### MOVSW Copy word at DS:[SI] to ES:[DI]. Update SI and DI. ### CMPSB Compare bytes: ES:[DI] from DS:[SI].