All posts by Ankur

Mix (C++ and Assembly) Program to Subtract Two 16-bit Numbers (With DAS)

While modern high-level languages like C++ abstract away many low-level operations, sometimes it’s useful to dive into assembly-level instructions for greater control and understanding. This example demonstrates subtraction using inline assembly in C++, with the addition of the DAS instruction to adjust the result for Binary Coded Decimal (BCD) operations.

#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 'a' into AX
    asm mov bx, b      // Move 'b' into BX
    asm sub ax, bx     // Subtract BX from AX
    asm das            // Adjust AX for BCD subtraction
    asm mov c, ax      // Store result in 'c'

    cout << "Result:";
    cout << c;

    getch();
}
Continue reading Mix (C++ and Assembly) Program to Subtract Two 16-bit Numbers (With DAS)

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

While modern high-level languages like C++ abstract away many low-level operations, sometimes it’s useful to get closer to the hardware to understand how things work under the hood. This example demonstrates how to subtract two 16-bit numbers using inline assembly in a simple C++ program.

#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 'a' into AX
    asm mov bx, b      // Move 'b' into BX
    asm sub ax, bx     // Subtract BX from AX
    asm mov c, ax      // Store result in 'c'

    cout << "Result:";
    cout << c;

    getch();
}
Continue reading Mix (C++ and Assembly) Program to Subtract Two 16 bit Numbers

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

This blog post will guide you through a C++ program that performs the addition of two numbers using inline assembly. While modern compilers provide high-level arithmetic operations, understanding inline assembly can help in optimizing performance and understanding low-level interactions with the CPU. Let’s explore this step-by-step!

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
    clrscr();
    short int a, b, c;
    
    cout << "Enter First Number: ";
    cin >> a;
    cout << "Enter Second Number: ";
    cin >> b;
    
    asm mov ax, a
    asm mov ah, 00h
    asm mov bx, b
    asm mov bh, 00h
    asm add al, bl
    asm mov c, ax
    
    cout << "Result: ";
    cout << c;
    
    getch();
}
Continue reading Mix (C++ and Assembly) Program to Add Two 8 bit Numbers

8086 Assembly Program to Find Reverse of an Array

In this blog post, we’ll explore how to reverse an array using an 8086 assembly language program. We will walk through the logic, the step-by-step execution, and provide a working code snippet to illustrate the process.

Overall Process:

  1. The program initializes registers and sets up pointers to the source (ARR) and destination (REV_ARR) arrays.
  2. It processes each element of the original array, copying it in reverse order to the destination array.
  3. After processing all elements, the program terminates, leaving the reversed array in memory.

8086 Assembly Code:

DATA SEGMENT
STR1 DB 01H,02H,05H,03H,04H
STR2 DB 5 DUP(?)
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
LEA SI, STR1
LEA DI, STR2+4
MOV CX, 05H

BACK: CLD
MOV AL, [SI]
MOV [DI], AL
INC SI
DEC DI
DEC CX
JNZ BACK

INT 3
CODE ENDS
END START
Continue reading 8086 Assembly Program to Find Reverse of an Array

8086 Assembly Program to Check if String is Palindrome or not

A palindrome is a word, phrase, or sequence that reads the same forward and backward. This 8086 assembly program determines whether a given string is a palindrome by reversing the string and comparing it to the original.

The program follows these steps:

  1. Initialize the Data Segment: Load the string to be checked.
  2. Reverse the String: Store the reversed string in another memory location.
  3. Compare the Original and Reversed Strings: Use CMPSB to compare byte-by-byte.
  4. Print the Result: Display whether the string is a palindrome or not.
Continue reading 8086 Assembly Program to Check if String is Palindrome or not

Performing Block Transfer using Assembly Language

In this blog post, we’ll explore how to perform a block transfer using an 8086 assembly language program. The following code snippet demonstrates this process:

DATA SEGMENT
STRING1 DB 01H,02H,03H,04H,05H
STRING2 DB 4 DUP(0)
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START: MOV AX,DATA
MOV DS,AX
MOV ES,AX
LEA SI,STRING1
LEA DI,STRING2
MOV CX,05H
CLD
REP MOVSB
INT 3
CODE ENDS
END START
Continue reading Performing Block Transfer using Assembly Language

Mix (C++ and Assembly) Program to Find Whether Number is Odd or Even

This C++ program determines whether a given number is even or odd using 8086-style inline assembly. It utilizes the div instruction to divide the number by 2 and then checks the remainder stored in the dx register.

#include<iostream.h>
#include<conio.h>

void main() {
    clrscr();
    int a, res;

    cout << "\n Enter a number";
    cin >> a;

    asm mov ax, a       // Move input number into AX
    asm mov bx, 02h     // Move divisor 2 into BX
    asm div bx          // Divide AX by BX, quotient in AX, remainder in DX
    asm mov res, dx     // Store remainder in res

    if(res == 0) {
        cout << "\n Even";
    } else {
        cout << "\n Odd";
    }

    getch();
}
Continue reading Mix (C++ and Assembly) Program to Find Whether Number is Odd or Even