Author Topic: using struc with the stack  (Read 7239 times)

Offline florian

  • Jr. Member
  • *
  • Posts: 4
using struc with the stack
« on: January 19, 2012, 01:53:05 PM »
Hi!
It might be that I am talking bull**** :S if that is the case a want to appologise for that...

If I have the struct:
Code: [Select]
struc a
.a resd 1
.b resd 2
endstruc

and I want to use it on the stack with the ebp
like this:
Code: [Select]
mov edx, [ebp - a.a]
I will move the 4 bytes before a.a into edx
so I would have to do it like this:
Code: [Select]
mov edx, [ebp - a.b]
to got the 4 bytes of a.a

Now I want to ask you if there is a possebility to make this struc thing work whith the stack...
so that I han sub xy from esp and write there by using [ebp - a.a]

I hope you understood what I wanted to ask my english is very bad :S

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: using struc with the stack
« Reply #1 on: January 19, 2012, 04:50:12 PM »
Well, yeah...

Code: [Select]
struc a
.a resd 1
.b resd 2
endstruc

This is just a "typedef". It reserves no memory - that's okay, we're about to reserve memory on the stack. What we've got so far is equivalent to doing:

Code: [Select]
a.a equ 0
a.b equ 4
a_size equ 12

That's all. Now we can reserve space for such a structure on the stack...

Code: [Select]
my_routine:
    push ebp
    mov ebp, esp
    sub esp, a_size

At this point, all we have is uninitialized memory. In order to "use" it in any meaningful way, we'll want some values in it. In "real code" we probably have some function to fill up the structure. As I recall, you were interested in findfirst/findnext(?)... In dos, there's a separate "setDTA" interrupt, then we just use the "findfirst" and "findnext" interrupts. I don't know how Windows does it. We can "fake" something. In any case, we'll probably need the address of the structure. Since a stack variable doesn't have an "offset", we'll need "lea"...

Code: [Select]
...
lea eax, [ebp - a_size]
push eax
call fill_astruc
; maybe "add esp, 4" to clean up stack?

Now we can "use" it. Remember tha "a.a" is only the offset from the beginning of the structure (it looks a lot like some C syntax, which does something different!), so we need to do:

Code: [Select]
...
mov edx, [ebp - a_size + a.a]
; do what you like with it...

Maybe that answers your question and maybe it doesn't. It's untested, so I may have made a mistake, but I think that's the "general idea"...

Best,
Frank


Offline florian

  • Jr. Member
  • *
  • Posts: 4
Re: using struc with the stack
« Reply #2 on: January 19, 2012, 08:39:17 PM »
Thank you that solved my problems.