Tag Archives: C

Animating a Truck in C

Computer graphics animation is the technique of creating the illusion of movement by rapidly drawing and erasing images on screen. In the Borland Turbo C environment, the graphics.h library provides low-level drawing functions that make it straightforward to build simple frame-by-frame animations. In this post, we walk through a C program that animates a truck moving across the screen using rectangles and circles to represent the truck body and wheels.

What the Program Does

The program draws a simple truck shape — a large rectangle for the cabin/body, a smaller rectangle for the cab, and two circles for the wheels — then shifts this shape incrementally across the screen to create an animation effect. Between each frame the viewport is cleared, giving the impression of smooth movement.

Continue reading Animating a Truck in C

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.

Continue reading Implementing Macro Processor in C

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.

Continue reading Implementing Multi-pass Assembler in C

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

This C++ program demonstrates how to find the largest element from a set of five integers using 8086 assembly instructions embedded directly into the code. The inline assembly helps perform comparisons and updates the result with the highest value found.

Continue reading Mix (C++ and Assembly) Program to Find Largest Number from Given Numbers

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();
}
Continue reading Mix (C++ and Assembly) Program to Sort Numbers in Ascending Order

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();
}
Continue reading Mix (C++ and Assembly) Program to Find Smallest Number from Given Numbers