TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Frank Adam
from: John Gardeniers
date: 1996-07-10 15:12:28
subject: Re: Regs

-=> On 09 Jul 96  10:21:02 <=-
-=> Frank Adam was heard to tell John Gardeniers <=-

    Hi Frank,

 FA> Just curious why is REGS a union and not a struct ? It feels like
 FA> one..

 JG> Because the registers come in a variety of sizes REGS has to be
 JG> a union.  Al & AH are byte wide (CHAR) while AX is the same two but

 FA> No-no, i've been using regs and unions for some time, but i could
 FA> never  find any in debt info on *how* they work.
 FA> Eg, i access regs.h.ah, and then regs.x.ax. Now according to the
 FA> manuals  and articles i've seen, i would've thought that the first
 FA> struct fills up  the union, and the second can't be accessed, but it
 FA> can.

    I'm sorry, you lost me a bit there but I'll have a go at
explaining it in my own (non-ISO) way :)

    One thing to always keep in mind about the regs is that they are
the *only* variables which consist entirely of hardware.  i.e.
Software can give them a different appearance but it can't do
anything *to* them other than alter the value stored there.  The
location and the properties of these very special variables are
literally cast in silicon.

    In your case, with regs.h and regs.x, (and there must then also
be regs.l) they may look like two different structures but they are
physically the same.  regs.h is simply using a part of regs.x, thus
making it a union which is made to look like two structures.  I'll
bet *that* cleared it up :)

    Here's the way it works: The registers are often treated by the
compiler as one or more structures.  Within one of these structures,
the one that hold the sixteen bit (or larger) registers each element
of the structure is actually a union.  That's not the official
description, it's just the way it's done.

    =============================================================
    Register AX holds two bytes, in AL and AH.  If you use regs.h
    to read or write AH it doesn't change the fact that the data
    is being read from, or written to, AX in regs.x.
    =============================================================

    Don't worry too much about any confusion, it's a lot to do with
the rather poor way it's handled by compiler writers.  The compiler
I use, PCC, confused me to the point where I didn't knof if it was
dealing directly with the registers or if it was doing it indirectly
via "virtual" registers.  Believe me, it's a hell of a lot easier to
do it in assembler.

 JG> Whenever I need to do anything at the register level I simply use
 JG> in-line assembler code.  This is even more important if you are

 FA> Because although i can read some assembly , i'm not at all comfortable
 FA> writing them.

    So, you were born with a complete knowledge of C? If you can
learn one language the rest get easier.  I think you will be
surprised at just how simple it is when you really start.  The
benefits are so enormous that it really is worth doing.  Besides,
you just might find yourself enjoying it once you get a bit of a
start.  I have what amounts to a very good book on disk if you ever
decide to give it a go and need any help.

 FA> So my motto is, if in doubt(that's mostly), let the compiler do
 FA> it.  :)

    Only if it does it satisfactorily.  How many times have you had
trouble tracking down a bug, only to find it was some quirk of the
compiler, not your coding? It's sure happened to me a few times.

 FA> There is also something about it not being ISO, and i believe
 FA> i've heard that certain compilers don't support inline asm. (??)

    In-line assembly was one of the very first thing specified in
the original draft of C. It's a vital part of the language and all
compilers must support it.  One of C's strongest features is it's
expandability.  If the language doesn't have a function you can add
it.  K&R recognised right from the start that there are always going
to be many things which just can't be done very well in anything
other than assemler.

 FA> I personally rather look at one of my old code and be able to read it
 FA> without thinking much about it. Using the regpacks will do that for
 FA> me,  Borland's use of _AX,_CX etc. would make it even easier.
 FA> With compilers and PCs getting better and faster, the only time i'd
 FA> consider asm is if a program truely needs more speed.

    If Windows was written in assembler it would run on an XT.

    I supose readability is in the eye of the beholder.  I can never
make much sense of my own code the day after I write it in C. In
assembler I can come back to it years later and understand what I
was aiming for. To each his own :)

    If you ever do decide to use more assembler just remember that
there's one rule above all others that will determine how easy it is
to read.  Sensible labels!  This applies to all programming
languages.  The days when we were restricted to two letter variable
names are long gone.

    C and assembler, as well as other languages, each have their
good and bad points.  I firmly believe that by combining them in the
correct proportions results in the best overall result, with the
minimum of effort.

        John

... Everything I know is basic; everything else is complex.
---
* Origin: Melbourne PC User Group +61-3-9699-6788 (3:632/309)
SEEN-BY: 3/103 50/99 620/243 623/630 632/50 107 108 309 348 360 371 504 525
SEEN-BY: 632/530 533 562 633/371 634/388 396 635/301 502 503 506 544 639/252
SEEN-BY: 711/401 409 410 413 430 808 809 932 934 712/515 713/888 714/906
SEEN-BY: 800/1
@PATH: 632/309 107 635/503 50/99 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.