Skip to main content

Mix (C++ and Assembly)

Mix Program in Assembly and C++ to Find Factorial of Number

This C++ program calculates the factorial of a number between 0 and 8 using inline 8086 assembly instructions. The multiplication is handled within an assembly loop, showcasing a basic yet insightful use of mul, dec, and jnz instructions. #include<iostream.h> #include<conio.h> void main() { clrscr(); short a; unsigned int c; cout << "\n Enter Number between 0 to 8:"; cin >> a; asm mov ax, 0000h asm mov al, 01h // Initialize AX to 1 asm mov cx, 0000h asm mov cx, a // Set CX loop counter to input value bac: asm mul cx // Multiply AX by CX asm dec cx // Decrement CX asm jnz bac // Loop until CX reaches zero asm mov c, ax // Move result from AX to variable c cout << "\n Factorial of " << a << " is " << c; getch(); }

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

This post demonstrates how to sort an array of integers using inline assembly in C++. We use basic comparison and swap logic in assembly embedded 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 jge 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(); }