Author Topic: How to map storage onto structs or other .bss items?  (Read 12356 times)

Offline JoeCoder

  • Jr. Member
  • *
  • Posts: 72
  • Country: by
How to map storage onto structs or other .bss items?
« on: June 12, 2011, 05:40:28 PM »
Sorry if this question was answered already, I checked the forums and manual and couldn't find what I am wondering about. If I did find it I didn't realize it.

Environment is Linux, UNIX, etc. where I understand there is no native storage allocation provided by the OS other than yanking around with the storage break [gag me with a spoon, dude!]

So if I want to declare a mapping for some storage, I guess we could call it a structure even though I am not a C programmer because I see there is support in NASM's macro facility for generating structures. There are several things about this I don't get. Is section .bss the right place to put these mappings or is there some other section for pure mappings that is more appropriate?

Now we have the right section and the structure defined the way we want. We allocate some storage by some mysterious method and we have the address of this storage in a general purpose register. How do I code references to that storage using the desired mapping(s)? Thank you.
« Last Edit: June 12, 2011, 06:09:36 PM by JoeCoder »
If you can't code it in assembly, it can't be coded!

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to map storage onto structs or other .bss items?
« Reply #1 on: June 12, 2011, 07:11:38 PM »
I don't know why sys_brk makes you gag - try it with a swallow of beer. :) You could also use sys_mmap (to an anonymous object) to allocate some memory on-the-fly. Of course, libc and its malloc() are sitting in memory waiting to be used, if you care to (I take it you don't).

But section .bss is the right place to reserve some "permanent" (for the life of your process) memory. You can also subtract something from esp to get some memory on the stack - often used for "local"/"automatic" variables within a function ("automatic" because it's "automatically" freed when the function exits).

Here's an example (prints a "ctime-like" string) which uses some structures... I don't know if it's "clear" or not. Memory for some of the structures is reserved in section .bss, and also on the stack (in the "time2string" function). If it doesn't clear up how to do it, perhaps we could try a more "purpose-built" example...

Best,
Frank


Offline JoeCoder

  • Jr. Member
  • *
  • Posts: 72
  • Country: by
Re: How to map storage onto structs or other .bss items?
« Reply #2 on: June 12, 2011, 07:47:07 PM »
I don't know why sys_brk makes you gag - try it with a swallow of beer. :)

That won't help. I'm used to much finer grained storage and resource management and trying to learn x86 and NIX at the same time is a shocker/disappointment. NASM and you guys are a bright spot, though  ;D

You could also use sys_mmap (to an anonymous object) to allocate some memory on-the-fly. Of course, libc and its malloc() are sitting in memory waiting to be used, if you care to (I take it you don't).

Thanks, I read about mmap and may try it first, it sounds interesting and probably less prone to ahhhhh wipeout than break dancing with break. Then again I decided to install Linux in a VM under my main Linux so I can only hurt myself so much. I don't figure there's much point in being slimed by libc or any GNU dependencies since we're coding in assembly, the Man's Programming Language! If I wanted to be a girly man and use libc I would just use C.  :D  ;D  >:( Like I saw you write somewhere else, assembly is about freedom from bloat. I really have a problem with dependency after dependency the way a lot of the gnu crap is written. I'm used to just me and the OS so I'm trying to get things done the same way in x86 Linux land. No middleware, no dependencies, no problem  :P

But section .bss is the right place to reserve some "permanent" (for the life of your process) memory. You can also subtract something from esp to get some memory on the stack - often used for "local"/"automatic" variables within a function ("automatic" because it's "automatically" freed when the function exits).

Oh, if .bss is life of process than it is not what I am thinking of. I want to be able to allocate and free during the process. I think from your example the structures are defined ahead of anything else so they are pure mappings. That is what I am looking for.

Here's an example (prints a "ctime-like" string) which uses some structures... I don't know if it's "clear" or not. Memory for some of the structures is reserved in section .bss, and also on the stack (in the "time2string" function). If it doesn't clear up how to do it, perhaps we could try a more "purpose-built" example...

Thank you. If I understand what you did, you defined some mappings and then used the names to offset from the base address of the structure after you had its address. If so, this is exactly what I am looking for. Of course managing the storage is a whole separate issue, but I believe you answered my main question. I'll monkey around with the storage stuff now that I have an approach.

Thanks a lot Frank.

Joe
If you can't code it in assembly, it can't be coded!