Re: B0




¬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

.



Relevant Pages

  • Re: To C.
    ... > How do you plan to implement the FPU Data Declarations ... the assembler could also interpret any ... _don't_ touch the machine instructions one bit because _THAT_ is ... macro facilities, ...
    (alt.lang.asm)
  • Re: question about Clojure immutable structures
    ... to low-level machinery in the compiler. ... asked with just declarations and optimization settings today. ... instruction or 256 bit register type that your assembler doesn't use ... implementations can but don't necessarily provide: ...
    (comp.lang.lisp)
  • Re: variable declaration
    ... > think about the cost of lack of var declarations. ... > programmer will waste while remembering exact variable name. ... Traceback: ...
    (comp.lang.python)
  • Re: [Clax86list] Optimizing Static Variable Layout
    ... > The ability of the assembler to preserve the programmer's desired ... possible, by moving declarations around. ... We are talking about HLA syntax here. ... > rather than as an "optimizer". ...
    (comp.lang.asm.x86)
  • Re: Why I dont believe in static typing
    ... > good ways to deduce automatically the types, why should the programmer ... So static typing by no means mandates writing down all your types. ... he could make a logical mistake that still resulted in a well-typed (but ... pure inference without declarations is less work for the programmer ...
    (comp.lang.lisp)