Page 1 of 1

NASM local fields

Posted: Mon Sep 29, 2008 5:09 am
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...

Re: NASM local fields

Posted: Mon Sep 29, 2008 5:50 am
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)

Re: NASM local fields

Posted: Mon Sep 29, 2008 6:42 am
by Love4Boobies
No, I actually checked the manual before and it's not really clear on this :(

Re: NASM local fields

Posted: Mon Sep 29, 2008 6:46 am
by Combuster
Let's first start with defining "Wrong Code"

Re: NASM local fields

Posted: Mon Sep 29, 2008 7:06 am
by i586coder
wrong code

Code: Select all

at .bar, db 00h
fixed code

Code: Select all

at foo.bar, db 00h
:wink:

Re: NASM local fields

Posted: Mon Sep 29, 2008 7:16 am
by Love4Boobies
I could have sworn that I already tried that... Weird! Thanks :P

Re: NASM local fields

Posted: Mon Sep 29, 2008 7:21 am
by i586coder
you welcome :mrgreen:

Re: NASM local fields

Posted: Mon Sep 29, 2008 7:22 am
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

Re: NASM local fields

Posted: Mon Sep 29, 2008 7:30 am
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:

Re: NASM local fields

Posted: Mon Sep 29, 2008 8:31 am
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.

Re: NASM local fields

Posted: Tue Sep 30, 2008 3:59 am
by CodeCat
So then the foo.bar syntax is actually just a sneaky shorthand for .bar - start_of_foo or something?

Re: NASM local fields

Posted: Thu Nov 27, 2008 10:20 am
by Love4Boobies
exactly.

Re: NASM local fields

Posted: Thu Nov 27, 2008 10:18 pm
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

Re: NASM local fields

Posted: Fri Nov 28, 2008 7:26 am
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

Re: NASM local fields

Posted: Tue Dec 02, 2008 9:03 am
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.