Comprehensive Study Guide for 8086 Microprocessor: Architecture, Programming, and Instruction Set

8086 Hardware Architecture and Pin Functions

  • Functional Units of the 8086 Architecture:

    • The 8086 microprocessor architecture is internally divided into two independent functional units: the Bus Interface Unit (BIU) and the Execution Unit (EU).

    • Bus Interface Unit (BIU):

    • Handles all data and address transfers on the buses for the Execution Unit.

    • Contains the 16-bit Segment Registers: Extra Segment (ESES), Code Segment (CSCS), Stack Segment (SSSS), and Data Segment (DSDS).

    • Contains the 16-bit Instruction Pointer (IPIP).

    • Houses a dedicated 20-bit Address Generation Adder (Σ\Sigma) that calculates the 20-bit physical address by shifting the segment address left by 4 bits and adding the 16-bit offset.

    • Features a 6-byte Instruction Stream Byte Queue configured as a First-In, First-Out (FIFO) buffer to prefetch and hold instructions.

    • Interfaces directly with external memory and peripheral I/O devices via the system bus.

    • Execution Unit (EU):

    • Responsible for decoding and executing fetched instructions.

    • Contains the 16-bit Arithmetic Logic Unit (ALU) for arithmetic and logical computations.

    • Contains the 16-bit Flag Register (status and control flags) and temporary operand registers.

    • Houses eight general-purpose registers: AXAX (AH/ALAH/AL), BXBX (BH/BLBH/BL), CXCX (CH/CLCH/CL), and DXDX (DH/DLDH/DL).

    • Houses pointer and index registers: Stack Pointer (SPSP), Base Pointer (BPBP), Source Index (SISI), and Destination Index (DIDI).

    • Includes an internal control system that decodes instructions received from the queue and coordinates internal operations.

8086 Internal Architecture
  • Pipelining Mechanism in 8086:

    • Pipelining is the technique of overlapping instruction fetch, decode, and execution mechanism.

    • To speed up program execution, the BIU fetches as many as 6 instruction bytes ahead of time from memory into an internal 6-byte prefetch queue.

    • The instruction prefetching queue operates as a 6-level deep FIFO buffer.

    • While the EU is decoding or executing an instruction that does not require use of the external buses, the BIU simultaneously fetches subsequent instruction bytes from memory.

    • When the EU is ready for its next instruction, it reads the instruction directly from the prefetch queue in the BIU rather than waiting for an external bus cycle.

    • Eliminates the waiting time that would otherwise occur if the execution unit had to send out an address to memory and wait for each instruction byte to be returned.

    • Greatly improves processor throughput and overall processing speed.

Pipelining Overlap of Fetch, Decode, and Execute
  • Pin Functions:

    • READY Pin:

    • Input pin used as an acknowledgement signal from slower memory or peripheral I/O devices.

    • Active-high signal.

    • When driven high, it indicates to the microprocessor that the addressed device is ready to transfer data.

    • If logic low during a bus cycle, the 8086 inserts wait states (TwT_w) until the signal goes high.

    • INTR (Interrupt Request) Pin:

    • Level-triggered interrupt request input.

    • Sampled by the processor during the last clock cycle of each instruction.

    • Determines whether an external hardware interrupt request is pending.

    • If INTRINTR is active and the Interrupt Flag (IFIF) is set, the processor enters an interrupt acknowledge cycle to process the interrupt.

Register Organization and Flag Register

  • Structure of the Flag Register:

    • The 8086 features a 16-bit register designated as the Flag Register (or Program Status Word).

    • Contains nine active 1-bit flags divided into two categories: six Status/Conditional Flags and three Control Flags.

    • The remaining seven bits (D1D_1, D3D_3, D5D_5, and D12D_{12} through D15D_{15}) are undefined (marked as XX).

Status and Control Flags of Intel 8086
  • Bit Mapping of the Flag Register:

    • Bit 0 (CFCF): Carry Flag

    • Bit 1 (XX): Undefined

    • Bit 2 (PFPF): Parity Flag

    • Bit 3 (XX): Undefined

    • Bit 4 (AFAF): Auxiliary Carry Flag

    • Bit 5 (XX): Undefined

    • Bit 6 (ZFZF): Zero Flag

    • Bit 7 (SFSF): Sign Flag

    • Bit 8 (TFTF): Trap Flag

    • Bit 9 (IFIF): Interrupt Enable Flag

    • Bit 10 (DFDF): Direction Flag

    • Bit 11 (OFOF): Overflow Flag

    • Bits 12–15 (XX): Undefined

  • Status / Conditional Flags:

    • Carry Flag (CFCF): Set to 1 when a carry or borrow is generated out of the Most Significant Bit (MSB) of the result (D7D_7 for an 8-bit operation, D15D_{15} for a 16-bit operation); otherwise, it is cleared to 0.

    • Parity Flag (PFPF): Set to 1 if the lower 8 bits (least significant byte) of the operation result contain an even number of 1s (even parity); otherwise, it is reset to 0 (odd parity).

    • Auxiliary Carry Flag (AFAF): Set to 1 if a carry or borrow is generated from the lower nibble to the higher nibble (from bit D3D_3 to bit D4D_4); otherwise, it is reset.

    • Zero Flag (ZFZF): Set to 1 if the result of an arithmetic or logical operation is zero; otherwise, it is reset to 0.

    • Sign Flag (SFSF): Set equal to the MSB of the result (D7D_7 for 8-bit, D15D_{15} for 16-bit). If the MSB is 1, indicating a negative signed number, SF=1SF = 1; if 0, indicating a positive result, SF=0SF = 0.

    • Overflow Flag (OFOF): Set to 1 if an arithmetic overflow occurs in signed operations, meaning the signed result is too large in magnitude to fit into the destination register; otherwise, cleared to 0.

  • Control Flags:

    • Trap Flag (TFTF): When set to 1, puts the processor into single-step execution mode for debugging, generating an internal interrupt after each instruction.

    • Interrupt Enable Flag (IFIF): Used to mask (disable) or unmask (enable) the maskable hardware interrupt INTRINTR. When set to 1, the 8086 recognizes external interrupts on INTRINTR; when cleared to 0, INTRINTR requests are ignored.

    • Direction Flag (DFDF): Determines the direction of address modification for string operations involving index registers SISI and DIDI. Setting DF=1DF = 1 decrements the pointers (auto-decrement mode), while clearing DF=0DF = 0 increments the pointers (auto-increment mode).

Memory Addressing, Segmentation, and Physical Address Calculation

  • Definitions of Address Types:

    • Logical Address: The address at which an instruction or data item appears to reside from the perspective of an executing application program. Specified as a segment:offset pair (Segment:Offset\text{Segment}:\text{Offset}) and translated into a physical address by the BIU.

    • Effective Address (EAEA) / Offset Address: An unsigned 16-bit displacement value representing the distance in bytes from the base address of a segment to the targeted operand or memory location. Handled by pointer and index registers (BXBX, BPBP, SISI, DIDI, or immediate values).

  • Physical Address Generation Process:

    • The 8086 features a 20-bit external address bus enabling access to 220=1,048,576 bytes2^{20} = 1{,}048{,}576\text{ bytes} (1 MB1\text{ MB}) of physical memory.

    • Physical address formation proceeds through the following sequential steps:

    1. Segment registers hold a 16-bit base address.

    2. The BIU internally shifts the 16-bit base address 4 bit positions to the left (equivalent to multiplying the value by 10H10\text{H} or appending four zero bits: 000020000_2 to the LSB). This expands the value into a 20-bit base address.

    3. The offset address is stored as a 16-bit value in a pointer, base, or index register.

    4. The 16-bit offset address is added to the 20-bit shifted base address inside the internal address adder (Σ\Sigma), producing the final 20-bit physical address.

    • Physical Address Formula:     Physical Address=(Segment Register×10H)+Offset Address\text{Physical Address} = (\text{Segment Register} \times 10\text{H}) + \text{Offset Address}

20-Bit Physical Address Generation Process
  • Worked Example:

    • Given Segment Register (DSDS) = 345AH345A\text{H}

    • Given Index Register (SISI) = 13DCH13DC\text{H}

    • Step 1: Shift DSDS left by 4 bits (×10H\times 10\text{H}):     345AH×10H=345A0H345A\text{H} \times 10\text{H} = 345A0\text{H}

    • Step 2: Add the effective address (SISI) to the base address:     Physical Address=345A0H+13DCH\text{Physical Address} = 345A0\text{H} + 13DC\text{H}

    • Step 3: Perform hexadecimal addition:     +345A0H+013DCH+3597CH\begin{aligned} \phantom{+}345A0\text{H} \\ +\phantom{0}13DC\text{H} \\ \hline \phantom{+}3597C\text{H} \end{aligned}

    • The resulting physical address is 3597CH3597C\text{H}.

Instruction Formats and Addressing Modes

  • Machine Language Instruction Format for Register-to-Register Transfer:

    • Instructions transferring data between registers consist of a two-byte encoding format.

    • Byte 1:

    • Bits D7D_7–D2D_2 (6 bits6\text{ bits}): Operational Code (OP CODE\text{OP CODE}) specifying the operation.

    • Bit D1D_1 (d bitd\text{ bit}): Direction bit. Specifies destination or source:

      • d=1d = 1: The register specified in the REG\text{REG} field is the destination operand.

      • d=0d = 0: The register specified in the REG\text{REG} field is the source operand.

    • Bit D0D_0 (w bitw\text{ bit}): Word/Byte operation indicator:

      • w=0w = 0: Byte operation (8 bits).

      • w=1w = 1: Word operation (16 bits).

    • Byte 2:

    • Bits D7D_7–D6D_6 (MOD bits\text{MOD bits}): Mode field. Set to 11 to specify a pure register-to-register operation (no memory operand/displacement).

    • Bits D5D_5–D3D_3 (REG bits\text{REG bits}): 3-bit code selecting the general register operand.

    • Bits D2D_2–D0D_0 (R/M bits\text{R/M bits}): 3-bit register/memory specifier. With MOD=11\text{MOD} = 11, this field selects the second register operand.

Machine Language Instruction Format for Register to Register Transfer
  • Addressing Modes of the 8086:

    • Immediate Addressing Mode:

    • The 8-bit or 16-bit operand (constant data) is directly included within the instruction itself.

    • Example: MOV AX, 67D3H (loads the 16-bit immediate value 67D3H67D3\text{H} into register AXAX).

    • Register Addressing Mode:

    • The operand is contained within one of the internal general-purpose or segment registers.

    • Example: MOV AX, CX (copies the 16-bit content of register CXCX into register AXAX).

    • Direct Addressing Mode:

    • The 16-bit effective address (offset) of the memory operand is explicitly supplied within the instruction.

    • Example: MOV CL, [2000H] (loads the byte located at offset address 2000H2000\text{H} in the data segment into register CLCL).

    • Register Indirect Addressing Mode:

    • The effective address of the memory operand is held in a base register (BXBX) or pointer/index register (BPBP, SISI, DIDI).

    • Example: MOV AX, [BX] (loads AXAX with the 16-bit word from the memory address specified by BXBX in the default data segment).

    • Indexed Addressing Mode:

    • The offset address of the operand is stored in an index register (SISI or DIDI).

    • DSDS is the default segment for SISI and DIDI; for string operations, DSDS and ESES serve as default segments for SISI and DIDI respectively.

    • Considered a specialized form of register indirect addressing.

    • Example: MOV AX, [SI] (retrieves the 16-bit word from the offset pointed to by SISI).

    • Based Indexed Addressing Mode:

    • The effective address is computed by summing the contents of a base register (BXBX or BPBP) and an index register (SISI or DIDI).

    • The default segment is DSDS when using BXBX, or SSSS when using BPBP.

    • Example: MOV AX, [BX][SI] (effective address equals (BX)+(SI)(BX) + (SI)).

    • Register Relative Addressing Mode:

    • The operand's offset is formed by adding an 8-bit or 16-bit displacement value to the contents of a base or index register (BXBX, BPBP, SISI, or DIDI).

    • Example: MOV AX, 50H[BX] (effective address equals (BX)+50H(BX) + 50\text{H}).

    • Relative Based Indexed Addressing Mode:

    • The effective address is calculated by summing an 8-bit or 16-bit displacement with the contents of both a base register and an index register.

    • Example: MOV AX, 50H[BX][SI] (effective address equals (BX)+(SI)+50H(BX) + (SI) + 50\text{H}).

Instruction Set and Operation Details

  • Data Transfer Instructions:

    • XCHG (Exchange):

    • Exchanges the contents of a destination register with the contents of a source register or memory location.

    • Cannot directly exchange contents of two memory locations.

    • Syntax: XCHG destination, source

    • Example: XCHG AX, BX (exchanges the 16-bit contents of register AXAX with BXBX).

  • Flag Manipulation Instructions:

    • STC (Set Carry Flag):

    • Sets the Carry Flag to logic 1.

    • Operation: CF \thickleftarrow 1

    • CMC (Complement Carry Flag):

    • Inverts the current state of the Carry Flag.

    • Operation: CF \thickleftarrow \thicksim CF

  • Logical Instructions:

    • AND (Logical AND):

    • Performs a bitwise logical AND operation between each bit of the source and destination operands and stores the result in the destination.

    • Syntax: AND destination, source

    • Operation: \text{Destination} \thickleftarrow \text{destination AND source}

    • Flags Affected: CF=0CF = 0, OF=0OF = 0, and updates PFPF, SFSF, ZFZF.

    • Bitwise Example: AND AL, BL

      • Initial values:

      • AL=1111 11002AL = 1111\text{ }1100_2

      • BL=0000 00112BL = 0000\text{ }0011_2

      • Computation:         AND 1111 11002AND 0000 00112AND 0000 00002\begin{aligned} \phantom{\text{AND }} 1111\ 1100_2 \\ \text{AND } 0000\ 0011_2 \\ \hline \phantom{\text{AND }} 0000\ 0000_2 \end{aligned}

      • Final result: AL \thickleftarrow 0000\text{ }0000_2

    • OR (Logical Inclusive OR):

    • Performs a bitwise logical OR operation between corresponding bits of the source and destination, placing the result in the destination.

    • Syntax: OR destination, source

    • Operation: \text{Destination} \thickleftarrow \text{destination OR source}

    • Flags Affected: CF=0CF = 0, OF=0OF = 0, and updates PFPF, SFSF, ZFZF.

    • Bitwise Example: OR AL, BL

      • Initial values:

      • AL=1111 11002AL = 1111\text{ }1100_2

      • BL=0000 00112BL = 0000\text{ }0011_2

      • Computation:         OR 1111 11002OR 0000 00112OR 1111 11112\begin{aligned} \phantom{\text{OR }} 1111\ 1100_2 \\ \text{OR } 0000\ 0011_2 \\ \hline \phantom{\text{OR }} 1111\ 1111_2 \end{aligned}

      • Final result: AL \thickleftarrow 1111\text{ }1111_2

    • NOT (Logical Invert):

    • Inverts every individual bit (one's complement) of the specified destination operand.

    • Syntax: NOT destination

    • Operation: \text{Destination} \thickleftarrow \text{NOT destination}

    • Flags Affected: None (status flags remain unchanged).

    • Bitwise Example: NOT BL

      • Initial value: BL=0000 00112BL = 0000\text{ }0011_2

      • Final result: BL=1111 11002BL = 1111\text{ }1100_2

    • XOR (Logical Exclusive-OR):

    • Performs a bitwise exclusive-OR between matching bits of source and destination, placing the outcome in the destination.

    • Syntax: XOR destination, source

    • Operation: \text{Destination} \thickleftarrow \text{destination XOR source}

    • Flags Affected: CF=0CF = 0, OF=0OF = 0, and updates PFPF, SFSF, ZFZF.

    • Bitwise Example: XOR AL, BL

      • Initial values:

      • AL=1111 11002AL = 1111\text{ }1100_2

      • BL=0000 00112BL = 0000\text{ }0011_2

      • Computation:         XOR 1111 11002XOR 0000 00112XOR 1111 11112\begin{aligned} \phantom{\text{XOR }} 1111\ 1100_2 \\ \text{XOR } 0000\ 0011_2 \\ \hline \phantom{\text{XOR }} 1111\ 1111_2 \end{aligned}

      • Final result: AL \thickleftarrow 1111\text{ }1111_2

    • TEST (Logical Compare):

    • Performs a bitwise AND between the source and destination operands and updates flags accordingly, without modifying either operand.

    • Syntax: TEST Destination, Source

    • Operation: \text{Flags} \thickleftarrow \text{evaluated from (destination AND source)}

    • Flags Affected: Updates PFPF, SFSF, ZFZF; clears CF=0CF = 0 and OF=0OF = 0.

    • Examples:

      • TEST AL, BL: Performs bitwise AND of BLBL with ALAL; discards numerical result; updates PFPF, SFSF, ZFZF.

      • Given MOV AL, 00000101b:

      • TEST AL, 1 testing bit 0 (000001012 AND 000000012=000000012≠000000101_2 \text{ AND } 00000001_2 = 00000001_2 \neq 0) yields ZF=0ZF = 0.

      • TEST AL, 10b testing bit 1 (000001012 AND 000000102=000000002=000000101_2 \text{ AND } 00000010_2 = 00000000_2 = 0) yields ZF=1ZF = 1.

  • String Manipulation Instructions and Repeat Prefixes:

    • Repeat Prefixes:

    • REP: Prefix written before a string instruction. Causes the instruction to repeat while decrementing count register CXCX until CX=0CX = 0.

    • REPE / REPZ (Repeat while Equal / Zero): Repeats the string instruction as long as compared bytes or words match (ZF=1ZF = 1) and CX≠0CX \neq 0.

  • REPNE / REPNZ (Repeat while Not Equal / Not Zero): Repeats the instruction as long as compared elements are not equal (ZF=0ZF = 0) and CX≠0CX \neq 0.

    • Syntax Example: REP MOVSB

  • MOVS / MOVSB / MOVSW (Move String Byte or Word):

    • Transmits a byte or word from a location in the Data Segment pointed to by DS:SIDS:SI to a location in the Extra Segment pointed to by ES:DIES:DI.

    • Syntax: MOVS destination, source or MOVSB or MOVSW

    • Operation: ES:[DI] \thickleftarrow DS:[SI]

    • Modifies SISI and DIDI by incrementing (if DF=0DF = 0) or decrementing (if DF=1DF = 1) by 1 (for byte) or 2 (for word).

    • Example Usage:assembly LEA SI, Source LEA DI, destination CLD MOV CX, 04H REP MOVSB       

  • CMPS / CMPSB / CMPSW (Compare String Byte or Word):

    • Compares a byte or word addressed by DS:SIDS:SI with an element addressed by ES:DIES:DI by subtraction, modifying flags without altering either memory operand.

    • Syntax: CMPS destination, source or CMPSB or CMPSW

    • Operation: \text{Flags affected} \thickleftarrow DS:[SI] - ES:[DI]

    • Auto-increments or auto-decrements SISI and DIDI according to the status of DFDF.

    • Example Usage:assembly LEA SI, Source LEA DI, destination CLD MOV CX, 100 REPE CMPSB       

  • SCAS / SCASB / SCASW (Scan String Byte or Word):

    • Compares the content of accumulator register ALAL (byte) or AXAX (word) with a memory byte/word addressed by ES:DIES:DI.

    • Syntax: SCAS destination or SCASB or SCASW

    • Operation: \text{Flags affected} \thickleftarrow AL/AX - ES:[DI]

    • Operates on strings residing in the Extra Segment (ESES). Sets ZF=1ZF = 1 when a match is detected.

    • Example Usage:assembly LEA DI, destination MOV AL, 0DH MOV CX, 80H CLD REPNE SCASB       

  • LODS / LODSB / LODSW (Load String Byte or Word):

    • Copies a byte or word from the data segment pointed to by DS:SIDS:SI into accumulator ALAL or AXAX.

    • Syntax: LODS source or LODSB or LODSW

    • Operation: AL/AX \thickleftarrow DS:[SI]

    • Updates pointer SISI based on DFDF.

    • Example Usage:assembly LEA SI, Source CLD LODSB       

    • STOS / STOSB / STOSW (Store String Byte or Word):

    • Copies a byte from ALAL or word from AXAX into a memory location pointed to by ES:DIES:DI in the Extra Segment.

    • Syntax: STOS destination or STOSB or STOSW

    • Operation: ES:[DI] \thickleftarrow AL/AX

    • Updates pointer DIDI based on DFDF.

  • Assembly Instructions for Specific Tasks:

    • Rotate register BL right 4 times:assembly MOV CL, 04H ROR BL, CL          (Alternative rotate forms include using ROL, RCR, or RCL with count register CL).

  • Multiply AL by 04H:assembly MOV BL, 04H MUL BL     

  • Signed division of AX by BL:assembly IDIV BL     

  • Move 2000H into BX register:assembly MOV BX, 2000H     

  • Increment the counter of AX by 1:assembly INC AX     

  • Compare AX with BX:assembly CMP AX, BX     

Assembly Language Programming Fundamentals and Tools

  • Assembly Language Programming Tools:

    • Editors: Programs used to create, write, and modify the source code text file of an assembly language program (typically saved with an .ASM extension).

    • Assembler: Translates symbolic assembly language mnemonic statements into binary machine code object files (.OBJ). Examples include MASM and TASM.

    • Linker: Combines independently assembled object modules, resolves external references, assigns absolute or relative addresses, and produces an executable binary program (.EXE).

    • Debugger: Software tool allowing developers to execute a program step-by-step, inspect and edit register contents, monitor memory locations, and insert breakpoints to identify and resolve logic errors.

  • Program Development Steps:

    1. Defining the problem: Carefully identify the operations the software must execute, understand the input requirements, expected outputs, constraints, and operational environment.

    2. Algorithm: Formulate a step-by-step sequential specification of computational and logical actions expressed in plain, clear language.

    3. Flowchart: Create a graphical diagram using standardized flow symbols representing the sequence, decision branches, loops, and overall control structure.

    4. Initialization checklist: Compile a list of all data items, constants, memory variables, hardware ports, registers, and operational flags requiring initialization prior to execution.

    5. Choosing instructions: Select optimal processor instructions that produce compact object code and maximize execution speed.

    6. Converting algorithms to assembly language program: Systematically transcribe each algorithmic step into corresponding assembly language statements and directives.

  • Assembler Directives:

    • DW (Define Word):

    • Informs the assembler to allocate 16-bit word storage locations in memory and optionally assign initial data values.

    • Example: MULTIPLIER DW 437AH reserves one word named MULTIPLIER and initializes it to 437AH437A\text{H}.

    • EQU (Equate):

    • Assigns a symbolic label or constant name to a specific numerical expression or string. During assembly, the assembler replaces all occurrences of the symbol with its defined value.

    • Example:assembly DATA SEGMENT Num1 EQU 50H Num2 EQU 66H DATA ENDS              Assigns the numeric values 50H50\text{H} and 66H66\text{H} to constants Num1 and Num2.

    • ASSUME:

    • Instructs the assembler on which segment register (CSCS, DSDS, SSSS, ESES) is associated with which logical segment label defined in the program.

    • Informs the assembler that segment register CSCS corresponds to code block label CODE, and DSDS corresponds to data block label DATA.

    • OFFSET:

    • An operator that calculates the 16-bit displacement (offset address) of a variable, data item, or procedure from the beginning of its enclosing segment.

    • Example: MOV BX, OFFSET PRICES loads the offset distance of the memory item PRICES into register BXBX.

    • SEGMENT and ENDS:

    • Directives used to bracket and delineate the boundaries of a logical program segment containing data, code, or stack definitions.

    • Example: CODE SEGMENT starts a logical section named CODE, which is closed by CODE ENDS.

    • EVEN (Align on Even Memory Address):

    • Instructs the assembler to update its internal Location Counter (LCLC) to the next even memory boundary address if it is currently pointing to an odd address.

    • Inserts a dummy NOP (90H90\text{H}) byte to maintain alignment, allowing faster 16-bit bus transfers from word-aligned addresses.

  • Macros:

    • Definition: A named block of instructions containing frequently repeated sequences of code that perform the same computational task across varying data items. The sequence is defined once and expanded in-line at each location where the macro is called.

    • General Syntax:assembly Macro_name MACRO [arg1, arg2, ..... argN] ; Sequence of instructions ENDM     

Procedure Calls and Flow Control Mechanisms

  • Intersegment vs. Intrasegment Procedures:

Parameter / Feature

Intersegment Call (Far Call)

Intrasegment Call (Near Call)

Alternative Terminology

Far procedure call

Near procedure call

Segment Location

Target procedure resides in a different code segment than the calling instruction.

Target procedure resides in the same code segment as the calling instruction.

Register Modifications

Replaces both Code Segment and Instruction Pointer (CS:IPCS:IP) pair.

Replaces only the Instruction Pointer (IPIP). Code segment (CSCS) is unchanged.

Stack Operations

Pushes both old CSCS and old IPIP onto the stack:
1. SP=SP−2SP = SP - 2 ; Save CSCS
2. SP=SP−2SP = SP - 2 ; Save IPIP

Pushes only the old IPIP onto the stack:
1. SP=SP−2SP = SP - 2 ; Save IPIP

Stack Overhead

Requires more stack memory (4 bytes4\text{ bytes} total).

Requires less stack memory (2 bytes2\text{ bytes} total).

Assembly Syntax Example

CALL FAR PTR Delay

CALL Delay

  • Re-entrant Procedures:

    • A software design scenario where Procedure 1 is invoked from the main program, Procedure 1 subsequently calls Procedure 2, and Procedure 2 invokes Procedure 1 again before the first invocation completes.

    • Under this condition, execution flow re-enters Procedure 1.

    • Execution returns properly via the stack:

    1. The RET instruction at the end of the second invocation of Procedure 1 returns execution back to Procedure 2.

    2. The RET statement terminating Procedure 2 returns control back to the first instance of Procedure 1 at the instruction following its call to Procedure 2.

    3. The final RET in Procedure 1 returns control back to the main program instruction immediately following the original procedure call.

Re-entrant Procedure Execution Flow
  • Recursive Procedures:

    • A specialized procedure that directly or indirectly calls itself.

    • Commonly employed when processing complex, recursive data structures such as hierarchical trees.

    • In a procedure invoked with a recursion depth parameter N=3N = 3, each iterative invocation decrements NN by one until the base condition N=0N = 0 is satisfied, after which nested RET instructions execute in reverse sequence to unwind back to the mainline caller.

Recursive Procedure Flow Diagram and Pseudo-code

Assembly Language Programs and Flowcharts

  • Multiplication of Two 16-bit Numbers:

    • Multiplication of two 16-bit unsigned numbers results in a product up to 32 bits wide, partitioned across two 16-bit registers: the most significant word in DXDX and the least significant word in AXAX.

Flowchart for Multiplication of Two 16-Bit Numbers
  • 16-bit Number Addition Program:

    • Program Listing: ```assembly DATA SEGMENT NUMBER1 DW 6753H NUMBER2 DW 5856H SUM DW 0 DATA ENDS

    CODE SEGMENT ASSUME CS: CODE, DS: DATA START: MOV AX, DATA MOV DS, AX MOV AX, NUMBER1 MOV BX, NUMBER2 ADD AX, BX MOV SUM, AX MOV AH, 4CH INT 21H CODE ENDS END START     ```

  • String Length Calculation Program:

    • Calculates the number of characters in a string terminated by a special character (</code>):</p></li><li><p><strong>ProgramListing:</strong>‘‘‘assemblyDATASEGMENTSTRGDB′GOODMORNING</code>):</p></li><li><p><strong>Program Listing:</strong> ```assembly DATA SEGMENT STRG DB 'GOOD MORNING' LEN DB ? DATA ENDS

    CODE SEGMENT ASSUME CS: CODE, DS: DATA START: MOV DX, DATA MOV DS, DX LEA SI, STRG MOV CL, 00H MOV AL, '$' NEXT: CMP AL, [SI] JZ EXIT ADD CL, 01H INC SI JMP NEXT EXIT: MOV LEN, CL MOV AH, 4CH INT 21H CODE ENDS END START     ```

  • Evaluation of p=x2+y2p = x^2 + y^2 Using a Macro:

    • Computes the sum of squares of two 8-bit unsigned integers xx and yy using an inline parametric macro expansion.

    • Program Listing: ```assembly .MODEL SMALL

    PROG MACRO a, b MOV AL, a MUL AL MOV BL, AL MOV AL, b MUL AL ADD AL, BL ENDM

    .DATA x DB 02H y DB 03H p DB DUP()

    .CODE START: MOV AX, DATA MOV DS, AX PROG x, y MOV p, AL MOV AH, 4CH INT 21H END START     ```

  • Count the Number of Zeros in a 16-Bit Word:

    • Inspects each bit of a 16-bit variable by rotating it into the carry flag and incrementing a counter whenever a zero is found.

    • Program Listing: ```assembly DATA SEGMENT N DB 1237H Z DB 0 DATA ENDS

    CODE SEGMENT ASSUME DS: DATA, CS: CODE START: MOV DX, DATA MOV DS, DX MOV AX, N MOV CL, 08 NEXT: ROL AX, 01 JC ONE INC Z ONE: LOOP NEXT HLT CODE ENDS END START     ```

  • Find the Largest Number in an Array:

    • Finds the largest value among five array elements: 10H10\text{H}, 24H24\text{H}, 02H02\text{H}, 05H05\text{H}, and 17H17\text{H}.

    • Program Listing: ```assembly DATA SEGMENT ARRAY DB 10H, 24H, 02H, 05H, 17H LARGEST DB 00H DATA ENDS

    CODE SEGMENT ASSUME CS: CODE, DS: DATA START: MOV DX, DATA MOV DS, DX MOV CX, 04H MOV SI, OFFSET ARRAY MOV AL, [SI] UP: INC SI CMP AL, [SI] JNC NEXT MOV AL, [SI] NEXT: DEC CX JNZ UP MOV LARGEST, AL MOV AX, 4C00H INT 21H CODE ENDS END START     ```

  • Addition of an 8-bit Series Using a Procedure:

    • Adds a series of five 8-bit values (10H10\text{H}, 20H20\text{H}, 30H30\text{H}, 40H40\text{H}, 50H50\text{H}) using a separate subroutine to perform the accumulation and track carry.

    • Program Listing: ```assembly DATA SEGMENT NUM1 DB 10H, 20H, 30H, 40H, 50H RESULT DB 0H CARRY DB 0H DATA ENDS

    CODE SEGMENT ASSUME CS: CODE, DS: DATA START: MOV DX, DATA MOV DS, DX MOV CL, 05H MOV SI, OFFSET NUM1 UP: CALL SUM INC SI LOOP UP MOV AH, 4CH INT 21H

    SUM PROC ; Procedure to add an 8-bit number MOV AL, [SI] ADD RESULT, AL JNC NEXT INC CARRY NEXT: RET SUM ENDP

    CODE ENDS END START     ```

  • String Reversal Program and Flowchart:

    • Program Listing: ```assembly DATA SEGMENT STRB DB 'GOOD MORNING$' REV DB 0FH DUP(?) DATA ENDS

    CODE SEGMENT ASSUME CS: CODE, DS: DATA START: MOV DX, DATA MOV DS, DX LEA SI, STRB MOV CL, 0FH LEA DI, REV ADD DI, 0FH UP: MOV AL, [SI] MOV [DI], AL INC SI DEC DI LOOP UP MOV AH, 4CH INT 21H CODE ENDS END START     ```

Flowchart to Reverse a String