Skip to main content

Assembly

The Complete 8086 Register Reference: AX, BX, CX, DX, Segment, Index & Pointer Registers Explained

The definitive 8086 register reference: all 14 registers explained with size, purpose, aliases, mandatory roles, and typical instructions. Includes a bit-by-bit FLAGS table, addressing mode summary, and a "which register for which task" decision guide that maps every common programming task to the correct register — with links to worked assembly examples throughout.

8086 Assembly Program to Compute the Power of a Number

This blog post details an 8086-assembly program that computes the power of a number, specifically baseexponent. This example demonstrates fundamental assembly concepts like loops, multiplication, and register manipulation to perform iterative arithmetic. Let’s get started! data segment base dw 0003h exponent dw 0004h result dd ? data ends code segment assume cs:code, ds:data start: ; Initialize data segment mov ax, data mov ds, ax ; Initialize result to 1 (R = B^0) mov word ptr result, 0001h mov word ptr result+2, 0000h ; Load base and exponent mov cx, exponent ; CX = exponent (loop counter) mov bx, base ; BX = base cmp cx, 0000h je exit ; If exponent is 0, result is already 1. Jump to exit. power_loop: ; Multiply result by base ; The result is a 32-bit number in result[0] (lower word) and result[2] (upper word) ; Multiplication requires careful handling of the 32-bit result with a 16-bit multiplier. ; 1. Multiply the lower word of result by base mov ax, word ptr result ; AX = result[0] mul bx ; AX * BX -> DX:AX (32-bit product) push dx ; Save the higher word (DX) of the product mov word ptr result, ax ; Store the lower word (AX) of the product as the new result[0] ; 2. Multiply the upper word of result by base mov ax, word ptr result+2 ; AX = result[2] mul bx ; AX * BX -> DX:AX (32-bit product) ; 3. Add the two 16-bit 'carry' terms pop cx ; Retrieve the saved higher word (DX) from step 1 into CX add ax, cx ; AX = AX + CX (Sum of two high words) adc dx, 0000h ; DX = DX + 0 + Carry from the previous ADD (final carry from the 32-bit multiplication) ; 4. Store the final upper word mov word ptr result+2, ax ; Store AX as the new result[2] ; Handle the 32-bit overflow (DX is the final carry from the 32-bit multiplication) ; For a 32-bit result storage, this program assumes the result fits in 32-bits. ; If the power is large, overflow might occur, which is a limitation of this 32-bit storage approach. loop power_loop ; Decrement CX and jump back to power_loop if CX != 0 exit: int 3 ; Program termination code ends end start

8086 MASM Assembly Program for Addition of Two 8-bit Numbers

This blog post will guide you through a MASM (Microsoft Macro Assembler) program that performs the addition of two 8-bit numbers. While this is a fundamental operation, it demonstrates crucial assembly programming concepts such as data handling, register usage, and memory storage. Let’s dive in! Assembly Code

Implementing Code Generator in C++

Code generation is the final back-end phase of a compiler. It takes the intermediate representation (IR) of the program — typically a sequence of three-address instructions — and translates each one into the equivalent target machine instructions. For a register-based architecture like the 8086, this means emitting MOV, ADD, SUB, MUL, DIV, and assignment instructions that use the AX and BX registers. This C++ program reads three-address IR instructions from an input file (AIP.TXT), generates the corresponding 8086-style assembly code, and writes it to an output file (ANOP.TXT). Each IR instruction has the form: operator operand1 operand2 result

Implementing Macro Processor in C

A macro processor is a system program that expands macros in a source file before the code is assembled or compiled. A macro is a named block of assembly statements; every time its name appears in the program the processor substitutes the full block in place of the call. This eliminates repetition and makes large assembly programs easier to maintain. This C implementation reads an assembly program from MACIN.TXT, detects MACRO / MEND delimiters to populate a Macro Definition Table (MDT), and then expands every macro call by inline-substituting the stored body. The expanded output is written to MACOUT.TXT and the raw MDT is saved to MDT.TXT.

Implementing Multi-pass Assembler in C

An assembler is a system software tool that translates an assembly language program (ALP) into machine code. A multi-pass assembler does this translation in two distinct passes over the source program. In Pass 1, it scans the ALP to build a Symbol Table (ST) that maps all labels and symbols to their memory addresses. In Pass 2, it uses the symbol table along with a Machine Opcode Table (MOT) and a Pseudo Opcode Table (POT) to generate the final object code. This C implementation reads the assembly program from alp.txt, the machine opcode table from mot.txt, and the pseudo opcode table from pot.txt. The generated object code is written to OUTPUTNEW.txt and the symbol table is saved to SymT.txt.

Mix (C++ and Assembly) Program to Sort Numbers in Ascending Order

This post demonstrates how to sort an array of integers using inline assembly in C++. Here, we perform sorting in ascending order by comparing and swapping adjacent elements using embedded assembly within a C++ program. #include<iostream.h> #include<conio.h> void main() { int a[5], x, y; int i, j; cout << "\n Enter 5 Numbers:"; for(i = 0; i < 5; i++) { cin >> a[i]; } //Sorting for(i = 0; i < 4; i++) { for(j = 0; j < 4; j++) { x = a[j]; y = a[j + 1]; _asm { mov ax, x mov bx, y cmp ax, bx jl nxt mov cx, ax mov ax, bx mov bx, cx mov x, ax mov y, bx } nxt: a[j] = x; a[j + 1] = y; } } cout << "\n Sorted Array:"; for(i = 0; i < 5; i++) cout << a[i] << " "; getch(); }

Mix (C++ and Assembly) Program to Find Smallest Number from Given Numbers

This C++ program demonstrates how to find the smallest number from an array using inline 8086 assembly language instructions. The logic involves comparing each array element and storing the smallest found so far using cmp and conditional jump instructions. #include<iostream.h> #include<conio.h> void main() { short a[5], x, y, res; short i, j; y = 999; // Initialize with a large number cout << "\n Enter 5 Numbers:"; for (i = 0; i < 5; i++) { cin >> a[i]; } asm { mov bx, y } // Finding smallest for (i = 0; i < 5; i++) { x = a[i]; asm { mov ax, x mov bx, y cmp ax, bx jnb nxt // Jump if not below (i.e., current is not smaller) mov bx, ax mov y, bx } nxt: } asm { mov res, bx; } cout << "\n Smallest Element:" << res; getch(); }