Author Topic: working with struc  (Read 6141 times)

Offline greco558

  • Jr. Member
  • *
  • Posts: 32
working with struc
« on: July 14, 2016, 05:40:34 PM »
I am still learning Assembly language, so my question is what is the advantage or disadvantage of
accessing a struc using the stack or using the .bss section to access struc.

Of the two code snippets below is there any real difference in speed or code size or is it just
a matter of preference.

Example 1
---------------------------
struc MYSTRUC
    .thisvar    resd 1
    .thatvar    resb 1
endstruc

section .bss

mystruc      resb   MYSTRUC_size

section .text

mov dword[mystruc+MYSTRUC.thisvar],eax

-----------------------------------------------------------
or use the stack
-----------------------------------------------------------
Example 2
----------------------
struc MYSTRUC
    .thisvar    resd 1
    .thatvar    resb 1
endstruc

section .text

   push ebp
   mov ebp,esp
   sub esp,MYSTRUC_size

   mov dword[ebp-MYSTRUC_size+MYSTRUC.thisvar],eax

   leave



John

Offline soulvomit

  • Jr. Member
  • *
  • Posts: 31
Re: working with struc
« Reply #1 on: July 18, 2016, 07:55:32 AM »
Hi,

In your first example you do not "create" new space for the struct. So when storing a value in .thisvar, .thatvar you would overwrite the previous value stored there.

In your second example you make space on the stack to store the values of .thisvar, .thatvar. Next time you run the code you would make new space on the stack to add more .vars.

In other words the first example is like having one "global struct". The second example is like using stack dynamic allocation, to create different structs.

EDIT: As for which one is slower, it is obviously the second example. It has more instructions and the memory management is more complex.

EDIT2: I should probably also point out that although the second is slower, it has nothing to do with the fact, that you are using the stack. Your query seems confused by the age old STACK vs. HEAP debate, which is prevalent in the programming community. Non of the examples uses "the heap" to do dynamic allocation.
« Last Edit: July 18, 2016, 10:52:47 AM by soulvomit »

Offline greco558

  • Jr. Member
  • *
  • Posts: 32
Re: working with struc
« Reply #2 on: July 18, 2016, 11:10:17 AM »
 Thank you for taking the time to explain. That makes sense to me.

soulvomit Thanks for the help
John