Saturday, September 26, 2026
HomeC ProgrammingAgain When Unions Have been Helpful

Again When Unions Have been Helpful


Although it’s shunned immediately, utilizing a union in your C code was widespread within the Eighties. Again then, compilers and IDEs comparable to Borland’s TurboC had been custom-made for the MS-DOS working system. Daring C programmers used the C language’s union key phrase to entry the system’s {hardware} instantly. Scary.

{Hardware} meddling, particularly accessing the CPU’s registers instantly from inside your C code, was potential as a result of your program was the one factor working on the pc on the time; calling or responding to an 8086 interrupt was okee-doke. So loading up just a few registers through a union wasn’t one thing that made different applications go nuts.

To make the magic occur, the C supply code included the dos.h header file. This header file prototyped just a few helpful however DOS-specific features in addition to numerous outlined constants and different stuff helpful to that working system. Keep in mind that this period was when the Ok&R model of C was the one customary. It was years earlier than ANSI C happened. In truth, the chaos of C variations and varieties is what prompted the event of the POSIX customary in 1988.

Within the dos.h header file was the definition for a union you might load up with values for low-level system entry:

union REGS {
    struct  WORDREGS x;
    struct  BYTEREGS h;
};

The union comprises two constructions, WORDREGS and BYTEREGS. Sure, unions can comprise constructions as they’re a sound C language knowledge sort.

Every construction within the REGS union represents a unique sized CPU register, 16-bit and 8-bit respectively. This format displays the structure of the 8086 CPU discovered within the authentic IBM PC and its numerous copies or “clones.” The 8086 CPU registers are illustrated in Determine 1.

Determine 1. Primary 8086 CPU structure. The information registers are proven, together with SI and DI. What’s not proven are the IP (instruction pointer), SP (stack pointer), flags registers, and others.

For instance, the AX register holds 16 bits. The decrease half, solely 8-bits, is register AL. The higher half is register AH. Collectively, they make register AX. From the REGS union, AX is the “phrase” register, WORDREGS; AL or AH is a “byte” register, BYTEREGS. The objective of the union is to characterize each of this stuff, AL and AH, which match into register AX that holds the identical knowledge. Therefore, the union is a perfect construction to reference knowledge saved in whichever register sort is required.

Listed here are the constructions related to the REGS union:

struct WORDREGS {
    unsigned int ax,bx,cx,dx,si,di,cflag,flags;
};

struct BYTEREGS {
    unsigned char al,ah,bl,bh,cl,ch,dl,dh;
};

The REGS union was used with the int86() operate, which initiated an 8086 interrupt, or a system name on to the CPU. Right here’s pattern code:

2026_09_26-Lesson.c



#embody <dos.h>

int foremost()
{
    union REGS in,out;


    in.h.ah = 0x0f;
    int86(0x10,&in,&out);    




    
    in.h.ah = 0x00;

    
    in.h.al = out.h.al;

    
    int86(0x10,&in,&out);



    return 0;
}

This code calls the system BIOS through 8086 interrupt, 0x10. The BIOS operate is loaded into the ah register, which is represented within the C code through REGS union member in.h.al. The int86() operate — distinctive to MS-DOS and prototyped within the dos.h header file — requires three arguments: the interrupt quantity, the tackle of the CPU registers to set earlier than the decision, and the tackle for CPU registers set after the decision.

The primary name to int86() obtains the video mode, which is saved within the al register after the decision returns.

The second name to int86() clears the display for the mode obtained from the primary name.

This system has no output and it’ll not construct on a contemporary pc. My level is to reminisce (in fact), but in addition to indicate a sensible instance of how a union can work to characterize a number of values referenced in numerous methods. However unions may also be abused, which I cowl in subsequent week’s Lesson.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments