Program Control Instructions
Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Reference
 The Intel   Microprocessors
        by Barry B. Brey
   Microprocessors and Interfacing
        by Douglas V. Hall
   Microprocessor and Microcomputer based system design
        by Md. Rafiquzzaman
2
                      Prepared by Mehdi Hasan Chowdhury, EEE, CUET
This presentation slide only contains the overview of the related
topics. Students are advised to take decent class-notes and read
          thoroughly from the prescribed text books.
                                                                     3
                      Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                   Introduction
 This chapter explains the program control instructions,
  including the jumps, calls, returns, interrupts and
  machine control instructions.
 This chapter also presents the relational assembly
  language statements (.IF, .ELSE, .ELSEIF, .ENDIF,
  .WHILE, .ENDW, .REPEAT, and .UNTIL).
                                                                  4
                   Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                  THE JUMP GROUP
• Allows programmer to skip program sections and
  branch to any part of memory for the
  next instruction.
• A conditional jump instruction allows decisions based
  upon numerical tests.
  – results are held in the flag bits, then tested by conditional
    jump instructions
• LOOP and conditional LOOP are also forms
  of the jump instruction.
                                                                     5
                      Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Unconditional Jump (JMP)
 Unconditional Jump                    Short Jump
      (JMP)
                                        Near Jump
                                          Far Jump
                                                                     6
                      Prepared by Mehdi Hasan Chowdhury, EEE, CUET
            Unconditional Jump (JMP)…
• Short jump is a 2-byte instruction that allows jumps to
  memory locations within +127 and –128 bytes from the
  address following the jump
• 3-byte near jump allows a jump within ±32K bytes from the
  instruction in the current code segment. (i.e. anywhere in the
  current code segment)
• 5-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. Far jumps are called intersegment jumps.
                                                                      7
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
               Unconditional Jump (JMP)…
Figure 6–1 The three main forms of the JMP instruction. Note that Disp is either an 8-
                    or 16-bit signed displacement or distance.
                                                                                8
                              Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                             Short Jump
 • Called relative jumps because they can be moved, with
   related software, to any location in the current code segment
   without a change. Jump address is not stored with the opcode
     – a distance, or displacement, follows the opcode. The short jump
       displacement is a distance represented by a 1-byte signed number
       whose value ranges between +127 and –128.
                   0009
 The address of the next instruction (0009H) is added to the sign-extended
  displacement (0017H) of the first jump, the address of NEXT is at location
  (0017H + 0009H) or 0020H.
                                                                          9
                           Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Figure 6–2 A short jump to four memory locations beyond the address of the next
                                  instruction.
                       – when the microprocessor executes
                         a short jump, the displacement is sign-
                         extended and added to the instruction
                         pointer (IP) to generate the jump address
                         within the current code segment
                                                       – The         instruction
                                                         branches      to   this
                                                         new     address      for
                                                         the next instruction
                                                         in the program
                                                                                    10
                           Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                      Short Jump…
• When a jump references an address, a label normally
  identifies the address.
• The JMP NEXT instruction is an example.
   – it jumps to label NEXT for the next instruction
   – very rare to use an actual hexadecimal address with any
     jump instruction
• The label NEXT must be followed by a colon (NEXT:) to allow
  an instruction to reference it
   – if a colon does not follow, you cannot jump to it
• The only time a colon is used is when the label is used with a
  jump or call instruction.
                                                                     11
                      Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                  Near Jump
• A near jump passes control to an instruction in the current
  code segment located within ±32K bytes from the near jump
  instruction.
     – Near jump is a 3-byte instruction with opcode followed by a signed 16-
       bit displacement. because signed displacement is ±32K, a near jump
       can jump to any memory location within the current real mode code
       segment. The near jump is also relocatable because it is also a relative
       jump.
 The address of the next instruction (000AH) is added to the displacement (01F6H) of
  the    first    jump,     the      address     of    NEXT       is   at     location
  (000AH + 01F6H) or 0200H.                                                     12
                               Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                              Problem!
• Find the value of X. Is it short or near or far jump?
                                                                       13
                        Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                              Near Jump
Figure 6–3 A near jump that adds the displacement (0002H) to the contents of IP.
                                                                            14
                           Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                         Far Jump
• Obtains     a   new     segment     and   offset  address
  to accomplish the jump:
   – bytes 2 and 3 of this 5-byte instruction contain
     the new offset address
   – bytes 4 and 5 contain the new segment address.
                                                                   15
                    Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                             Far Jump…
Figure 6–4 A far jump instruction replaces the contents of both CS and IP with 4
                          bytes following the opcode.
                                        Hence, Current IP = (CS*10) + IP
                                                          = (A300*10) + 0127
                                                          = A3127
                                                            EA 2701 00A3
                                                                               16
                           Prepared by Mehdi Hasan Chowdhury, EEE, CUET
          Jumps with Register Operands
• Jump can also use a 16 bit register as an operand.
   – address of the jump is in the register specified
     by the jump instruction
• Unlike displacement associated with the near jump, register
  contents are transferred directly into the instruction pointer.
• Example:
              MOV AX, 1009H
              JMP AX
   – copies the contents of the AX register into the IP.
   – allows a jump to any location within the current code
     segment
                                                                      17
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
   Conditional Jumps and Conditional Sets
• Always short jumps in 8086.
    – limits range to within +127 and –128 bytes from the
      location following the conditional jump
• Allows a conditional jump to any location within the current
  code segment.
• Conditional jump instructions test flag bits:
        sign (S), zero (Z), carry (C), parity (P), overflow (0)
• 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
• Example: inatruction JC will jump if the carry bit is set (CF=1)
                                                                      18
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
Conditional Jumps and Conditional Sets…
                                                            19
             Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                LOOP
• A combination of a decrement CX and the JNZ conditional
  jump.
• In 8086 LOOP decrements CX.
   – if CX != 0, it jumps to the address indicated
     by the label
   – If CX becomes 0, the next sequential instruction executes
                                                                    20
                     Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                            Problem!
• Using jump instruction, write an assembly language program
    for the following series.
i. 1+2+3+4+5+ ………… + ∞
ii. 1+2+3+4+5+ ………… + 100
iii. 5+6+8+11+15+ ………… + 110
iv. 1+3+5+7+9+ ………… + ∞
v. 2+4+6+8+10+ ………… + 200
vi. 10+20+30+40+ ………… + 500
vii. 1+5+9+13+17+ ………… + 107
viii. 1+5+6+11+17+28+ …… + ∞
      100
ix.   𝑋=0
          (X   + 1)
x. 12+22+32+42+52+ ………… + ∞
                                                                     21
                      Prepared by Mehdi Hasan Chowdhury, EEE, CUET
  CONTROLLING THE FLOW OF THE PROGRAM
• Easier to use assembly language statements .IF, .ELSE,
  .ELSEIF, and .ENDIF to control the flow of the program than
  to use the correct conditional jump statement.
• Other statements developed include .REPEAT–.UNTIL and
  .WHILE–.ENDW.
                                                                    22
                     Prepared by Mehdi Hasan Chowdhury, EEE, CUET
CONTROLLING THE FLOW OF THE PROGRAM…
                                                           23
            Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                 .WHILE - .ENDW
• .WHILE
     logic
     operation
  .ENDW
                       MOV AX, 5500H
                       MOV BX, 0001H
                       .WHILE
                          AL != 05 H
                          ADD AX, BX
                       .ENDW
                                                                24
                 Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                 .REPEAT- .UNTIL
• .REPEAT
     operation
  .UNTIL
      logic
                         MOV AX, 5500H
                         MOV BX, 0001H
                         .REPEAT
                            ADD AX, BX
                         .UNTIL
                            AL=06H
                                                                 25
                  Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                    .IF - .ENDIF
• .IF
     logic
     operation
  .ENDIF
                 MOV AX, 30FF H
                 .IF
                     AL >= FF && AH< 33
                     MOV BX, 0DEF H
                     ADD AX, BX
                 .END IF
                                                                26
                 Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                  IMPORTANT
MOV BL, 44; Copies 44 D into BL (2C H)
MOV CX, 100; Copies 100 D into CX (0064 H)
MOV AX, 44H; Copies 44 H into AX
                                                              27
               Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                       PROCEDURES
• A procedure (also known as subroutine or function) is a
  group of instructions that usually performs one task. A
  procedure is a reusable section of the software stored in
  memory once, used as often as necessary.
   – It has many advantages. Like it saves memory space and
     makes it easier to develop software.
   – Only disadvantage of procedure is time it takes the computer
     to link to, and return from it.
• A procedure begins with the PROC directive and ends with the
  ENDP directive.
   – each directive appears with the procedure name
                                                                      28
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                     PROCEDURES…
• 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.
                                                                      29
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
               PROCEDURES…
Main Program                                                  Procedure
MOV AX, CX                                      XYZ           PROC NEAR
….                1                                           ADD CX,DX
….                                                            ….
….                                                            ….
CALL XYZ                2                                     ….
ADD AX,BX                                                     RET
….                                              XYZ           ENDP
….
….
END
                                                                          30
               Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                  CALL
• CALL instruction transfers the flow of the program to the
  procedure.
• It 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.
                                                                     31
                      Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                             Near CALL
• 3 bytes long.
   – the first byte contains the opcode; the second and third
      bytes contain the displacement (within ±32K bytes)
• When the near CALL executes, it first pushes the offset
  address of the next instruction onto the stack.
   – offset address of the next instruction appears in the
      instruction pointer (IP)
• It then adds displacement from bytes 2 & 3 to the IP to transfer
  control to the procedure.
                                                                       32
                        Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                       Near CALL…
The effect of a near CALL on the stack and the instruction pointer
                                                                    After CALL,
                                                                    IP= (0003 H+0FFF H)
                                                                      = 1002 H
                                                                    SP after CALL = FFFD
                                                                    SS after CALL = A000
                                                                    IP after CALL = 1002
                                                                                           33
                     Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                             Far CALL
• 5-byte instruction contains an opcode followed by the next
  value for the IP and CS registers.
   – bytes 2 and 3 contain new contents of the IP
   – bytes 4 and 5 contain the new contents for CS
• Far CALL places the contents of both IP and CS on the stack
  before jumping to the address indicated by bytes 2 through 5.
• This allows far CALL to call a procedure located anywhere in
  the memory and return from that procedure.
                                                                      34
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
         Far CALL…
The effect of a far CALL instruction
                                                     Address of the procedure,
                                                     IP= (CS * 10) + IP
                                                       = (1100 H * 10) + 0002 H
                                                       = 11002 H
                                                     SP after CALL = FFFB
                                                     SS after CALL = A000
                                                     IP after CALL = 0002
                                                     CS after CALL = 1100
                                                                            35
      Prepared by Mehdi Hasan Chowdhury, EEE, CUET
             CALLs with Register Operands
• Like jump instructions, call instructions also may contain a
  register operand.
• An example CALL BX, which pushes the contents of IP onto the
  stack.
   – then jumps to the offset address, located in register BX, in
     the current code segment
• Always uses a 16-bit offset address, stored in any 16-bit
  register except segment registers.
                                                                      36
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                            RET
• 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.
      The effect of a near return instruction on the stack and instruction pointer
                                                             SP before RET = FFFD
                                                             SS before RET = A000
                                                             IP before RET = 1004
                                                             SP after RET = FFFF
                                                             SS after RET = A000
                                                             IP after RET = 0003
                                                                                     37
                               Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                          INTERRUPTS
• An interrupt is a hardware-generated CALL or a software-
  generated CALL
   – Hardware interrupts occur when a peripheral device asserts
     an interrupt input pin of the microprocessor.
   – Internal interrupts are initiated by the state of the CPU (e.g.
     divide by zero error) or by an instruction. At times an internal
     interrupt is called an exception.
• Either    type      interrupts     the   program       by   calling
  an interrupt service procedure (ISP) or interrupt handler.
                                                                        38
                         Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                       INTERRUPTS…
• This section explains software interrupts, which are special
  types of CALL instructions.
• This section describes the three types of software interrupt
  instructions.
                                         INT
                                      INTO
                                      INT 3
• In the real mode, each of these instructions fetches a vector
  from the vector table, and then calls the procedure stored at the
  location addressed by the vector.
                                                                       39
                        Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                      Interrupt Vectors
• An interrupt vector is a 4-byte number stored in the first 1024
   bytes of memory in real mode.
• 256 different interrupt vectors.
    – each vector contains the address of an interrupt service
      procedure.
 • Each vector contains a value for IP and CS that forms the
    address of the interrupt service procedure.
    – the first 2 bytes contain IP; the last 2 bytes CS
 • Intel reserves the first 32 interrupt vectors (0-31) for the
    present and future products.
 • Remaining interrupt vectors (32–255) are available to users.
                                                                      40
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                      INTs
• 256 different software interrupt instructions (INTs) available to
  the programmer.
   – each INT instruction has a numeric operand whose range is
     0 to 255 (00H–FFH)
• Address of the interrupt vector is determined by multiplying the
  interrupt type number by 4.
   – For example, INT 10H instruction calls the interrupt service
     procedure whose address is stored beginning at memory
     location 40H (10H  4) in the mode.
                                                                       41
                        Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                        INTs…
                       Consequences of software interrupts
• When a software interrupt executes, it:
  – pushes the flags onto the stack
  – clears the T and I flag bits
  – pushes CS onto the stack
  – fetches the new value for CS from the interrupt vector
  – pushes IP/EIP onto the stack
  – fetches the new value for IP/EIP from the vector
  – jumps to the new location (service procedure) addressed by
    the new CS and IP.
Notice that when the INT instruction executes, it clears the interrupt flag (I),
which controls the external hardware interrupt input pin INTR (interrupt
request). When I = 0, the microprocessor disables the INTR pin; when I = 1, the
microprocessor enables the INTR pin.                                     42
                             Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                       INT vs. Far CALL
• INT performs as a far CALL
    – There are some major differences.
• INT not only pushes CS & IP onto the stack, but also pushes the
  flags onto the stack. The INT instruction performs the operation of
  a PUSHF, followed by a far CALL instruction.
• Software interrupts are most commonly used to call system
  procedures because the address of the function need not be
  known. The interrupts often control printers, video displays, and
  disk drives.
• INT replaces a far CALL that would otherwise be used to call a
  system function.
• INT instruction is 2 bytes long, whereas the far CALL is 5 bytes
  long. Each time that the INT instruction replaces a far CALL, it
  saves 3 bytes of memory. This can amount to a sizable saving if
  INT often appears in a program, as it does for system calls.
                                                                        43
                         Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                     IRET
• Used only with software or hardware interrupt service
  procedures.
• IRET instruction will
   – pop stack data back into the IP
   – pop stack data back into CS
   – pop stack data back into the flag register
• Accomplishes the same tasks as the POPF followed by a far
  RET instruction.
• When IRET executes, it restores the contents of I and T from
  the stack. Thus it preserves the state of these flag bits. Hence,
  If interrupts were enabled before an interrupt service procedure,
  they are automatically re-enabled by the IRET instruction.
                                                                       44
                        Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                   INT 3
• A special software interrupt designed to function as a
  breakpoint.
   – a 1-byte instruction, while others are 2-byte
• Common to insert an INT 3 in software to interrupt or break the
  flow of the software.
   – function is called a breakpoint
   – breakpoints help to debug faulty software
• A breakpoint occurs for any software interrupt, but because INT
  3 is 1 byte long, it is easier to use for this function.
                                                                      45
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                    INTO
• Interrupt on overflow (INTO) is a conditional software interrupt
  that tests overflow flag (O).
   – If O = 0, INTO performs no operation
   – if O = 1 and an INTO executes, an interrupt occurs via vector
     type number 4
• The INTO instruction appears in software that adds or subtracts
  signed binary numbers.
   – With these operations, it is possible to have an overflow
• JO or INTO instructions detect the overflow.
                                                                       46
                        Prepared by Mehdi Hasan Chowdhury, EEE, CUET
             An Interrupt Service Procedure
• Interrupts are usually reserved for system events. This is simply
  an example showing how an interrupt service procedure
  appears.
• Suppose a procedure is required to add the contents of DI, SI,
  BP, and BX and save the sum in AX.
   – as a common task, it may be worthwhile to develop the task
     as a software interrupt.
                                                                       47
                        Prepared by Mehdi Hasan Chowdhury, EEE, CUET
MACHINE CONTROL AND MISCELLANEOUS INSTRUCTIONS
• The last category of real mode instructions found in the
  microprocessor is the machine control and miscellaneous
  group.
• These instructions provide control of the carry bit, sample the
  BUSY/TEST pin, and perform various other functions.
• Because many of these instructions are used in hardware
  control, they need only be explained briefly at this point.
                                                                      48
                       Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                 WAIT
• Monitors the hardware TEST pin on 8086/8088.
• If the WAIT instruction executes while the TEST pin = 1,
  nothing happens and the next instruction executes.
• If TEST pin = 0 when the WAIT instruction executes, the
  microprocessor waits for the pin to return to a logic 1.
                                                                    49
                     Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                    HLT
• The halt instruction (HLT) stops the execution of software.
• There are three ways to exit a halt:
   – by interrupt
   – a hardware reset
   – or DMA (Direct memory access) operation
• Often synchronizes external hardware interrupts with the
  software system.
                                                                     50
                      Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                     NOP
• When the microprocessor encounters a no operation instruction
  (NOP), it takes a short time to execute.
• In early years, before software development tools were available,
  a NOP, which performs absolutely no operation, was often used to
  pad software with space for future machine language instructions.
• When the microprocessor encounters a NOP, it takes a short time
  to execute.
• If you are developing machine language programs, which are
  extremely rare, it is recommended that you place 10 or so NOPS
  in your program at 50-byte intervals., in case you need to add
  instructions at some future point.
• A NOP may also find application in time delays to waste time.
• A NOP used for timing is not very accurate because of the cache
  and pipelines in modern microprocessors.
                                                                       51
                        Prepared by Mehdi Hasan Chowdhury, EEE, CUET
                                               52
Prepared by Mehdi Hasan Chowdhury, EEE, CUET