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

Understanding the Code

Variable Declarations

  • nos[5] → Array to hold five integers.
  • x, y → Temporary variables to hold elements for comparison.
  • i, j → Loop counters to traverse the array from both ends.

User Input
The user is prompted to enter 5 numbers that are stored in the array nos.

Logic Using Assembly

  • The loop compares nos[i] and nos[j] using inline assembly.
  • mov ax, x and mov bx, y move values into registers.
  • cmp ax, bx checks if the elements are equal.
  • jnz no jumps to the label no if the elements do not match (i.e., not a palindrome).

Output Decision

  • If all mirrored elements match, the message “Array is Palindrome” is printed.
  • Otherwise, it jumps to the no label and prints “Array is not palindrome.”

Output

Enter 5 numbers:10
12
15
12
10
Array is Palindrome.

Enter 5 numbers:10
12
15
10
12
Array is not palindrome.

Explanation

In the first run, the array [10, 12, 15, 12, 10] reads the same forwards and backwards—hence it’s a palindrome. In the second run, the mismatch indicates it’s not. The use of assembly provides a low-level understanding of conditional comparisons.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.