1 / 24

The 8086 Assembly Programming Program Control Instructions

The 8086 Assembly Programming Program Control Instructions. Khaled A. Al- Utaibi alutaibi@uoh.edu.sa. Agenda. Introduction Jump Instructions Unconditional Jump Instruction Conditional Jump Instructions Loop Instructions Procedures. Introduction.

nolcha
Download Presentation

The 8086 Assembly Programming Program Control Instructions

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. The 8086 Assembly ProgrammingProgram Control Instructions Khaled A. Al-Utaibi alutaibi@uoh.edu.sa

  2. Agenda • Introduction • Jump Instructions • Unconditional Jump Instruction • Conditional Jump Instructions • Loop Instructions • Procedures

  3. Introduction • The program control instructions direct the flow of a program and allow the flow to change. • A change in flow often occurs after a decision made with the instructions like CMP and TEST instruction and is followed by conditional jump instructions. • These slides explain the program control instructions, including the jumps, calls, and returns.

  4. Jump Instructions • Jump instructions are classified into two groups: • (1) Conditional Jumps • (2) Unconditional Jumps • 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.

  5. Unconditional Jump (JMP) • The general format of the JMP (Unconditional Jump) instruction is • JMP label • Where label is a program address identifier. • A label is any valid assembler identifier followed by a colon. • Any instruction statement can be prefixed by a label. • The assembler treats a reference to a label as a reference to the address of the instruction to which that label is affixed (See Figure 1). • A label can also prefix a comment statement of can appear on a line by itself (See Figures 2 and 3). • In such cases, labels are treated as references to the address of the first instruction following the label.

  6. JMPax_zero . . ax_zero: MOV AX, 0 . . (a) JMPax_zero . . ax_zero: ; a comment MOV AX, 0 . . (b) JMPax_zero . . ax_zero: MOV AX, 0 . . (c) Figure 1: Examples of (a) a label prefixing an instruction, (b) a label prefixing a comment, (c) a label prefixing an empty line.

  7. Conditional Jump Instructions • The conditional jump instructions test the following flag bits: • sign (SF), • zero (ZF), • carry (CF), • parity (PF), and • overflow (OF). • If the condition under test is true, a branch to the label associated with the jump instruction occurs. • If the condition is false, the next sequential step in the program executes.

  8. Conditional Jump Instructions • The general format of a conditional jump instruction is • Jxxxlabel • Where: • xxx describes the condition for which you are testing • label is a program address identifier. • The syntax of the 8086 assembly language supports 31 conditional jump instructions (See Table 1). • 15 of those instructions test a status flag or a combination of status flags and jump to the address indicated by label if the condition is true. • Another 15 conditional jumps are the converses of the first 15; they force a jump if the condition is false.

  9. Table 1: List of 8086 conditional jump instructions.

  10. Conditional Jump Instructions • When signed numbers are compared, use the JG, JL, JGE, JLE, JE, and JNE instructions. • The terms greater than and less than refer to signed numbers. • Whenunsigned numbers are compared, use the JA, JB, JAB, JBE, JE, and JNE instructions. • The terms above and below refer to unsigned numbers. • The conditional jump instructions all test flag bits except for JCXZ (jump if CX = 0) • Instead of testing flag bits, JCXZ directly tests the contents of the CX register without affecting the flag bits • For the JCXZ instruction, if CX = 0, a jump occurs, and if CX != 0, no jump occurs.

  11. Conditional Jump Instructions • Conditional jump instructions are used together with instructions that affect the status flags (CF, OF, PF, SF, and ZF) and those which affect the CX register. • These instructions include: • Arithmetic Instructions (e.g., ADD, SUB, MUL, DIV, CMP, INC, DEC) • Logical Instructions (e.g., AND, OR, XOR, NOT, NEG)

  12. Conditional Jump Instructions • Example 1: Write an assembly code to compare two variables V1 and V2 and store the larger into AH and the smaller into AL. • Solution: See Figure 2

  13. data segment ; start data segment v1 db 3; v1 = 3 v2 db 7; v2 = 7 ends ; end data segment code segment ; start code segment start: leaax, data ; load effective address of data segment movds, ax ; set ds = data segment movah, v1 ; load v1 into ah moval, v2 ; load v2 into al cmpah, al; compare the two numbers ja done ; if (ah > al) then goto done xchgah, al ; else exchange ah & al done: nop; do nothing ends ; end code segment end start ; end program Figure 2: Assembly code for Example 1.

  14. Loop Instruction • The general format of a loop instruction is • LOOP label • Where label is a program address identifier. • The LOOP instruction can be used to count the iterations of a loop. • The LOOP instruction is equivalent for the two instructions • DEC CX • JNZ label • The major distinction between the above two instructions and the LOOP instruction is that the later does not affect the states the status flags. • The LOOP decrements the contents of the CX register and if the result is not zero, directs program flow to the instruction at address referenced by label. • Conversely, if the result of decrementing CX is zero, program flow fall through to the next instruction.

  15. Loop Instruction • Example 2: Write an assembly code to find the largest value in a given array on n elements (assume n = 5). • Solution: See Figure 3

  16. data segment ; start data segment a db 3 ; array a of 5 values db 5 db 1 db 7 db 2 ends ; end data segment code segment ; start code segment start: leaax, data ; load effective address of data segment movds, ax; set ds = data segment movcx, 5 ; set cx = loop count movbx, 0 ; set bx = index of 1st element (0) moval, 0 ; set al = 0 (assume al is the max value) next: movah, a[bx] ; load a[bx] into ah incbx; increment bx (next element index) cmpah, al ; compare ah and al jbe skip ; if (ah <= al) then skip moval, ah ; else set al = ah skip: loop next; loop ends ; end code segment end start ; end program Figure 3: Assembly code for Example 2.

  17. Variations on Loop Instruction • The 8086 supports a couple of variations on the LOOP instruction: • LOOPZ (Loop While Zero) • LOOPNZ (Loop While Not Zero) • LOOPE (Loop While Equal) • LOOPNE (Loop While Not Equal) • These variations allow program flow to be controlled by both the CX register and the zero flag (ZF) simultaneously.

  18. Procedures • The procedure (subroutine, method, or function) 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.

  19. Procedures • The CALL instruction links to the procedure, and the RET (return) instruction returns from the procedure. • The stack stores the return address whenever a procedure is called during the execution of a program. • The CALL instruction pushes the address of the instruction following the CALL (return address) on the stack. • The RET instruction removes an address from the stack so the program returns to the instruction following the CALL.

  20. ProceduresProcedure Definition • A procedure can be defined as shown in Figure 3. • The beginning of a procedure is defined with a PROC (PROCedure) directive. • Each procedure must be assigned a name, by which it can be called from elsewhere in a program. • The ending of a procedure is defined with an ENDP (ENDProcedure) directive. • Between the PROC and ENDP directives you can write the body of your procedure. procedure_namePROC ; procedure body procedure_name END Figure 4: Procedure definition.

  21. ProceduresThe CALL Instruction • The CALL instruction is used to invoke the code in a subroutine. • The general format of a CALL instruction is • CALL procedure_name • The CPU does several things when a CALL instruction is executed: • (1) It adjusts the contents of the IP register as if it were about to execute the next instruction. • (2) It stores the contents of the IP register in the program stack. • (3) It adjusts the contents of the IP register again to point to the 1st instruction in the named procedure. • The result is that immediately after processing a CALL instruction, the CPU begins executing the 1st instruction in the named procedure. • From there on it continues executing instructions until it encounters a RET instruction.

  22. ProceduresThe RET Instruction • A RET(Return) instruction transfers program flow back from a subroutine to its caller. • The general format of a RET instruction is • RET • When the CPU encounters a RET instruction, it perform the following actions. • (1) It recovers the IP register contents stored in the program stack by the CALL instruction. • (2) It puts the restored value into the IP register and continue execution. • This gets the CPU back to where it was when the CALL instruction was encountered.

  23. Loop Instruction • Example 3:Write a procedure to add the elements of an array a. • Assume that the sum will be in AX. • Assume that the number of elements is (n = 5). • Use BX as an index of the array. • Use LOOP instruction to accomplish the task. • Solution: See Figure 4

  24. data segment ; start data segment a dw3 ; array a of 5 values dw5 dw1 dw7 dw2 ends ; end data segment code segment ; start code segment Sumproc movcx, 5 ; set cx = loop count movax, 0 ; set ax = 0 (sum) movbx, 0 ; set bx= 0 (index of 1st element) next: addax, a[bx] ; ax = ax + a[bx] incbx; bx = bx + 2 (next element 2 bytes) incbx loop next; loop ret ; return to caller endp start: leaax, data ; load effective address of data segment movds, ax; set ds = data segment callsum ; call sum procedure ends; end code segment end start ; end program Figure 3: Assembly code for Example 2.

More Related