Skip to main content

Mix (C++ and Assembly)

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(); }

Mix (C++ and Assembly) Program to Find Reverse of an Array

This post explores how to reverse an array of integers using inline assembly within a C++ program. The program swaps elements from the start and end of the array using low-level assembly instructions. #include<iostream.h> #include<conio.h> void main() { int nos[5], x, y; cout << "\n Enter 5 numbers:"; int i, j; for(i = 0; i < 5; i++) { cin >> nos[i]; } for(i = 0, j = 4; i < 3; i++, j--) { x = nos[i]; y = nos[j]; _asm{ mov ax, x mov bx, y mov cx, ax mov ax, bx mov bx, cx mov x, ax mov y, bx } nos[i] = x; nos[j] = y; } cout << "\n Reverse:"; for(i = 0; i < 5; i++) { cout << nos[i] << " "; } getch(); }

Mix (C++ and Assembly) Program to Check if String is Palindrome or not

This C++ program checks whether a given array of five integers is a palindrome (i.e., reads the same forwards and backwards) using inline 8086 assembly instructions. It utilizes basic comparison instructions and control flow to determine equality between symmetric elements. #include<iostream.h> #include<conio.h> void main() { int nos[5], x, y; cout << "\n Enter 5 numbers:"; int i, j; for (i = 0; i < 5; i++) { cin >> nos[i]; } for (i = 0, j = 4; i < 3; i++, j--) { x = nos[i]; y = nos[j]; _asm { mov ax, x mov bx, y cmp ax, bx jnz no } } cout << "\n Array is Palindrome."; goto end; no: cout << "\n Array is not palindrome."; end: getch(); }

Mix (C++ and Assembly) Program to Search an Element in an Array

This C++ program demonstrates how to search for a specific element within an array of 10 integers using 8086 assembly instructions embedded inline. The program uses the cmp and jz instructions to perform the comparison and detect a match. #include<iostream.h> #include<conio.h> void main() { int nos[10], j, t; int i, p; cout << "\n Enter 10 numbers:"; for (i = 0; i < 10; i++) { cin >> nos[i]; } cout << "\n Enter number to be searched:"; cin >> j; for (i = 0; i < 10; i++) { p = i; t = nos[i]; _asm { mov ax, t // Load current array element mov bx, j // Load number to be searched cmp ax, bx // Compare current element with target jz out // If match found, jump to output } } cout << "\n No is not found."; goto end; out: cout << "\n Number is found at " << p + 1 << " th position"; end: getch(); }

Mix (C++ and Assembly) Program to Add Two 16 bit Numbers

While high-level languages like C++ are well known for abstracting away low-level operations, sometimes it’s beneficial to peek under the hood to see how things really work at the machine level. In this post, we’ll explore a simple C++ program that incorporates inline assembly to add two 16-bit numbers. #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); int a,b,c; cout<<"Enter First Number:"; cin>>a; cout<<"Enter Second Number:"; cin>>b; asm mov ax, a // Move first number into AX asm mov bx, b // Move second number into BX asm add ax, bx // Add BX to AX (binary addition) asm mov c, ax // Move result into variable 'c' cout<<"Result:"; cout<<c; getch(); }