Re: B0
- From: "randyhyde@xxxxxxxxxxxxx" <randyhyde@xxxxxxxxxxxxx>
- Date: 19 Aug 2005 12:43:49 -0700
¬a\/b wrote:
> >
> >Hi Randy,
> >
> >I believe what Rene means, is type safety and variable type
> >enforcement. A feature that I like in fasm, and is useful in most
> >cases.
>
> i not agree
On what basis?
>
> >Basically:
> >my_var db 0
> my_va1 db 0
> my_va2 db 0
> my_va3 db 0
>
>
> >mov eax, [my_var]
>
> and i don't see why not use
> mov eax, [my_var]
Because you're trying to load a byte object into a dword register? Does
this not seem peculiar to you? Sure, I know what you're *attempting* to
do, but it's fraught with errors. Further, what about the poor person
who comes along after the fact and is wondering *why* on earth you're
loading a byte into a dword register. And what happens in the future,
when during modification, you decided that my_va2 really needs to be a
word and you change your declarations thusly:
my_var db 0
my_va1 db 0
my_va2 dw 0
my_va3 db 0
Now your code breaks without any warning whatsoever at all. The tiny
amount of efficiency you gained by loading those four bytes with a
single operation has come back to haunt you.
Or, you do something simple like inserting a new variable declaration
into the middle of this sequence:
my_var db 0
my_va0 db 0
my_va1 db 0
my_va2 dw 0
my_va3 db 0
Again, the original logic of the program is broken with no warning and
no error. Of course, you suffer the same problem if you do the
following (in FASM):
mov eax, dword [my_var]
But at least you've let the world know that you *really* intended to do
this, rather than it was a mistake on your part.
Now there *are* structured and safe ways of doing this (loading four
bytes with a single instruction into a 32-bit register). For example,
if you've got an assembler like MASM, TASM, or HLA that supports
unions, you can declare structures like the following (HLA syntax):
type
variant:
union
all_my_vars: dword;
record
my_var:byte;
my_va1:byte;
my_va2:byte;
my_va3:byte;
endrecord;
endunion
Now you can load "all_my_vars" into EAX without error (or without an
explicit type coercion operator). And unlike the previous
declarations, this union declaration makes it very clear that my_va*
must appear in this order and must have the specified sizes. Anyone
looking at such a union is *not* going to aribtrarily rearrange the
declarations or insert new fields into it (assuming, of course, that
they understand what unions are all about). This "enscapsulation" of
the data type makes it *clear* that the programmer is treating the
entire object as a single structure. That is, this structure is
self-documenting with respect to the access patterns.
Pulling stunts like taking advantage of lack of type checking in
hobby-level assemblers is a prime example of why assembly language
programs are well-known for being difficult to read, understand, and
maintain. Tricks like what you're suggesting exacerbate the problems
with assembly language's reputation. It's stuff like this that nearly
killed assembly language off in the first place. If you want to see
assembly's reputation change, you want to stay away from writing code
like this except in very rare and specialized circumstances. And as
long as there is a straight-forward and structured solution, that lets
you do exactly the same thing without the problem of producing
unmaintainable code, you should pick the structured solution.
Anyone who suggests otherwise is simply teaching you to be a hack
programmer rather than a great programmer.
Cheers,
Randy Hyde
.
- References:
- Prev by Date: Re: B0
- Next by Date: Re: What are your applications?
- Previous by thread: Re: B0
- Next by thread: Re: B0
- Index(es):
Relevant Pages
|