I was trying to create a structure under TASM 3.0 but i have a small doubt.
I created and initialized the structure on the .data segment with no
problem. But can't the initialization be done in the .code segment? When i
tried it, the program just stopped.
Like this:
.....
.data
Food struc
price db ?
weight db ?
Food ends
.code
My_Food Food <1,1>
Thanx,
Custodio, Leonardo C.
cold...@uol.com.br
::Nitrogen::
if this ^^^ is what CPU tries to execute... Do I have to continue the
phrase?
--
Alexei A. Frounze
Program? Do you mean the assembler or your executable? If you put data
in a code segment, make sure that the code branches around it. For
example:
.code
jmp short Past
My_Food Food <1,1>
Past: ; other code here
Otherwise your app may end up trying to execute the data. Also
remember that if you don't use segment prefixes when accessing data,
the default is to use the segment pointed to by DS (and now your
My_Food resides in the code segment).
AriL
--
Pain and disappointment are inevitable. Misery is optional.
Homepaged at http://www.angelfire.com/or/lukumies
* Do not reply via email *
I suspect that your program attempted to execute the data in
this structure as code. This would probably lock up the
machine. If you put the structure in your code segment,
be sure to put a jump (or other unconditional transfer of
control instruction) before the structure so you never
execute its data as code.
Randy Hyde
<snip>
>Otherwise your app may end up trying to execute the data. Also
>remember that if you don't use segment prefixes when accessing data,
>the default is to use the segment pointed to by DS (and now your
>My_Food resides in the code segment).
I think that you find an Assembler usually handles references to data
stored in code space. For Example:
Microsoft (R) Macro Assembler Version 5.10 2/14/1 Page 1-1
.model medium,C
assume cs:@code,ds:@data,ss:@data
.data
0000 _DATA segment 'DATA'
Food struc
0000 00 price db ?
0001 00 weight db ?
0002 Food ends
.code
0000 @CurSeg ends
0000 x_TEXT segment 'CODE'
0000 EB 02 jmp short Past
0002 01 My_Food Food <1,1>
0003 01
0004 Past: ; other code here
0004 2E: A0 0002 R mov al,My_Food.price
0008 2E: 8A 26 0003 R mov ah,My_Food.weight
End
000D @CurSeg ends
<rest of listing snipped>
--
Arargh (at enteract dot com) http://www.arargh.com
>I was trying to create a structure under TASM 3.0 but i have a small doubt.
>I created and initialized the structure on the .data segment with no
>problem. But can't the initialization be done in the .code segment? When i
>tried it, the program just stopped.
>Like this:
>
>.....
>.data
>Food struc
> price db ?
> weight db ?
>Food ends
>
>.code
> My_Food Food <1,1>
What do you mean by "created and initialized the structure on the
.data segment"? Or rather, do you know exactly what that means?
I never declare my structures in the .data segment. Because declaring
a structure is all you do with
Food struc
price db ?
weight db ?
Food ends
Now, the former just stands for an equate declaration. What you are
doing is telling TASM that
price EQU 0
weight EQU 1
You are not instantiating the structure at all, you are just giving
TASM information about the structure. So much so, that you may even
declare it, use it, but not instantiate it at all. The extract from
MASM's Knowledge Base which closes this posting explains this.
Thus, it looks more logical to put your structure declarations outside
of any segment, at the beggining of your source, along with all the
other equates.
/* Sample follows
.386
.MODEL FLAT STDCALL
Include ...
Const1 EQU 1
Const2 EQU 2
Food struc
price db ?
weight db ?
Food ends
.data
....
. code
...
Sample ends */
As for what sense might it have to declare a structure but not
instantiate it, think of a buffer which is to be filled by a device
driver all in a bunch, but "whose" inner structure you declare in
order to be able to access sections of the buffer conveying different
pieces of information. You can declare and instantiate a structure to
handle this, but the structure declaration + buffer instantiation
looks like a more consistent and ergonomical approach.
Quote from "A Structure Template Can Be Referenced in a MASM Program",
Article ID: Q74924:
---------------------
SUMMARY
With the Microsoft Macro Assembler (MASM), if a structure type is
declared but no instance of the structure is ever defined, the
assembler will still permit access to the structure.
This is expected behavior for the assembler. A reference to a
structure field in this manner is equivalent to taking the offset of
the field from the beginning of the structure.
MORE INFORMATION
The sample code below illustrates this situation. The example declares
an "animal" structure type, but does not define an instance of the
structure. A reference to this structure type is then made in the
following line:
mov ax, animal.dog
As described above, the reference to animal.dog just generates the
offset of the field, dog, from the beginning of the structure;
therefore, this line is effectively the same as the statement "mov ax,
2".
Sample Code
; Assemble options needed: none
.MODEL SMALL
animal struc
cat dw 11
dog dw 12
animal ends
.CODE
start: mov ax, @data
mov ds, ax
mov ax, animal.dog
END start
Manuel Algora
cen...@wanadoo.es