NASM local fields

Programming, for all ages and all languages.
Post Reply
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

NASM local fields

Post by Love4Boobies »

Does anyone know why this NASM code won't work?

Code: Select all

struc foo
  .bar: resb 1
endstruc

foobar:
  istruc foo
    at .bar, db 00h
  iend
I've asked on #osdev, #reactos and no one knew...
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
i586coder
Member
Member
Posts: 143
Joined: Sat Sep 20, 2008 6:43 am

Re: NASM local fields

Post by i586coder »

you asked a very common question
some one told me http://www.google.com
i say you can use this link to help


http://home.comcast.net/~fbkotler/nasmd ... tion-4.8.5


if useless i am here 8)
Distance doesn't make you any smaller,
but it does make you part of a larger picture.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: NASM local fields

Post by Love4Boobies »

No, I actually checked the manual before and it's not really clear on this :(
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: NASM local fields

Post by Combuster »

Let's first start with defining "Wrong Code"
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
i586coder
Member
Member
Posts: 143
Joined: Sat Sep 20, 2008 6:43 am

Re: NASM local fields

Post by i586coder »

wrong code

Code: Select all

at .bar, db 00h
fixed code

Code: Select all

at foo.bar, db 00h
:wink:
Distance doesn't make you any smaller,
but it does make you part of a larger picture.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: NASM local fields

Post by Love4Boobies »

I could have sworn that I already tried that... Weird! Thanks :P
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
i586coder
Member
Member
Posts: 143
Joined: Sat Sep 20, 2008 6:43 am

Re: NASM local fields

Post by i586coder »

you welcome :mrgreen:
Distance doesn't make you any smaller,
but it does make you part of a larger picture.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: NASM local fields

Post by Love4Boobies »

Oh - I did try it out and it didn't work. I finally got the hang of it. If you define local fields within a structure, then all fields before it have to be local. So this isn't legal:

Code: Select all

struc foo
  bar1: resb 1
  .bar2: resb 1
endstruc
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
i586coder
Member
Member
Posts: 143
Joined: Sat Sep 20, 2008 6:43 am

Re: NASM local fields

Post by i586coder »

usualy in any struct body
there is no global var,

for example

Code: Select all

struct foo{
 int a,b;
};
a=3;     //illegal,may be another var outside our struct
foo.a=3; //much better
So, you can always use local var. inside your struct :idea:
Distance doesn't make you any smaller,
but it does make you part of a larger picture.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: NASM local fields

Post by Love4Boobies »

It's not the same thing. NASM has no actual support for structures. It just defines a bunch of labels relative to 0 (the beginning of the structure) and then those can be added to where the instance of the structure is and you get the fields. But it's just that - labels.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: NASM local fields

Post by CodeCat »

So then the foo.bar syntax is actually just a sneaky shorthand for .bar - start_of_foo or something?
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: NASM local fields

Post by Love4Boobies »

exactly.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: NASM local fields

Post by Brendan »

Hi,
CodeCat wrote:So then the foo.bar syntax is actually just a sneaky shorthand for .bar - start_of_foo or something?
For NASM, there's labels and there's local labels (and some other stuff). For example:

Code: Select all

foo:
    nop
.l1:
    nop
.l2:
    nop
bar:
    nop
.l1:
    nop
.l2:
    nop
    jmp .l2
Because the labels ".l1" and ".l2" are local labels, they can be defined as many times as you like. The assembler prepends the name of the previous non-local label to them, so ".l1" is actually "foo.l1" or "bar.l1", and the "jmp .l2" becomes "jmp foo.l2".

For structures it's the same. For an example, consider this:

Code: Select all

    absolute 0x00000000
myStructureType:
.foo:        resd 1
.bar:        resb 8
.woot:       resw 2
    section .data

myStructure:
    times (myStructureType.foo - myStructure) db 0
    dd 0x12345678
    times (myStructureType.foo - myStructure) db 0
    db "Hello",0
    times (myStructureType.woot - myStructure) db 0
    dw 0x1234,0x5678
From this, you should be able to see how the macros used for structures (struct, endstruct, istruct, at, etc) would be defined...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: NASM local fields

Post by DeletedAccount »

Hi,
Macros are not really required , you can achieve the result without them :) .So the struct macro / construct is not really needed . I really do not know which is recommeded way , I think there are some programmers of "pure assembly blood" hanging around here.

Regards
Sandeep
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: NASM local fields

Post by Love4Boobies »

Shrek wrote:Hi,
Macros are not really required , you can achieve the result without them :) .So the struct macro / construct is not really needed . I really do not know which is recommeded way , I think there are some programmers of "pure assembly blood" hanging around here.
Well it helps you spot missing fields, if nothing else.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply