Q1Microprocessor and Interfaces
Question
(a) Explain the 8086 instruction set categories. (b) Write assembly language programs for: (i) Sorting an array (ii) Finding the largest element in an array.
Answer
The 8086 instruction set is organized into six functional categories - data transfer, arithmetic, logical, string manipulation, control transfer, and processor control - and this classification directly informs how bubble-sort and largest-element assembly routines are constructed using indexed addressing and conditional jumps.
The Intel 8086 supports roughly 20,000 forms of about 117 base instruction types (once every addressing-mode and operand-size variant is counted), all of which fall into six broad functional categories based on the type of operation they perform on the CPU's registers, memory, and flags.
1. Data Transfer Instructions
These move data between registers, memory, and I/O ports without performing any computation, and generally do not affect the flags. MOV copies a value between two operands; PUSH/POP move a word to or from the stack; XCHG swaps the contents of two operands; IN/OUT transfer a byte or word between the accumulator and an I/O port; LEA loads the effective address of a memory operand rather than its contents.
2. Arithmetic Instructions
These perform binary or BCD arithmetic and update the flag register (Carry, Zero, Sign, Overflow, Parity, Auxiliary Carry) according to the result. ADD/SUB perform 8-bit or 16-bit addition/subtraction; MUL/IMUL and DIV/IDIV perform unsigned and signed multiplication/division, a hardware capability the 8085 completely lacked; INC/DEC increment or decrement an operand by 1 without affecting the Carry flag, making them ideal for loop counters.
3. Logical Instructions
These perform bitwise Boolean operations and shifts/rotates on the operand, always updating the flags. AND, OR, XOR, and NOT implement standard Boolean algebra; SHL/SHR perform logical left/right shifts; ROL/ROR and RCL/RCR rotate bits, optionally through the Carry flag, useful for multi-word arithmetic and bit-testing.
4. String Manipulation Instructions
These operate on blocks of memory addressed via SI (source) and DI (destination) within DS and ES, and automatically increment or decrement the index registers based on the Direction Flag. MOVS copies a byte/word from source to destination; CMPS compares two strings; SCAS scans a string for a value; LODS/STOS load/store the accumulator from/to a string. Prefixed with REP/REPE/REPNE, they process an entire array in one instruction using CX as an automatic counter.
5. Control Transfer Instructions
These alter the normal sequential flow of the Instruction Pointer. JMP performs an unconditional jump; conditional jumps like JZ, JC, JNZ, JAE branch based on flag states set by a preceding comparison or arithmetic instruction; CALL/RET transfer control to and from subroutines, automatically managing the return address on the stack; LOOP decrements CX and jumps if it is nonzero, ideal for fixed-count iteration.
6. Processor Control Instructions
These directly manipulate CPU flags or overall processor state rather than data. STC/CLC set/clear the Carry flag; STI/CLI enable/disable maskable hardware interrupts; HLT halts the processor until an interrupt or reset occurs; NOP performs no operation, often used for timing delays or code patching.
(i) Sorting an Array (Bubble Sort, Ascending Order)
The algorithm repeatedly scans the array, swapping any adjacent out-of-order pair, so that after each full pass the next-largest unsorted element "bubbles" into its correct position at the end.
``assembly
MOV CX, N-1 ; Number of outer passes = N-1
OUTER: MOV BX, CX ; Inner loop count = remaining unsorted elements
LEA SI, ARRAY ; SI -> start of array
INNER: MOV AL, [SI] ; AL = current element
CMP AL, [SI+1] ; Compare with next element
JBE SKIP ; Already in order, skip swap
XCHG AL, [SI+1] ; Swap: put larger value at SI+1
MOV [SI], AL ; Store smaller value back at SI
SKIP: INC SI ; Move to next pair
DEC BX
JNZ INNER ; Repeat inner pass
LOOP OUTER ; Repeat outer pass, CX decremented automatically
HLT
``
(ii) Finding the Largest Element in an Array
This routine assumes the first element is the maximum, then linearly scans the remaining N-1 elements, updating the running maximum whenever a larger value is found.
``assembly
LEA SI, ARRAY ; SI -> start of array
MOV CX, N ; CX = total element count
MOV AL, [SI] ; Assume first element is the largest
DEC CX ; One element already examined
INC SI
NEXT: CMP AL, [SI] ; Jump if AL >= [SI]
JAE SKIP
MOV AL, [SI] ; Update AL with new largest value
SKIP: INC SI
LOOP NEXT ; Repeat for remaining elements
MOV LARGEST, AL ; Store final result in memory
HLT
``
In both routines, LOOP combines the decrement of CX and the conditional jump into a single instruction, and comparisons rely on the flags set by CMP (which internally performs a non-destructive subtraction) to drive the conditional jumps JBE (jump if below or equal) and JAE (jump if above or equal) - both unsigned comparisons appropriate for byte arrays holding non-negative data.