News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Negative offset in structure

Started by peter_asm, June 24, 2014, 05:17:27 AM

Previous topic - Next topic

peter_asm

Let's say I have a structure with 3 or more members.

STRUCT mystruct
  _a  dd ?
  _b  dd ?
  _c  dd ?
ENDS mystruct


I would like _a to be negative when mystruct is being addressed.


_a = -4
_b = 0
_c = 4


Rather than use EQU, is it possible to address _a in a structure as negative offset?
Say I use instruction

inc dword ptr [eax][mystruct._a]

I'd like this to be translated into

inc dword ptr [eax-4]

RuiLoureiro

No.
          mystruct._a= 0       mystruct._b= 4       mystruct._c= 8

jj2007

Not possible with a STRUCT, but no problem for a macro, e.g. SetGlobals.
Example:

include \masm32\MasmBasic\MasmBasic.inc      ; download

mystruct STRUCT
  _a  dd ?
  _b  dd ?
  _c  dd ?
mystruct ENDS

  SetGlobals ms:mystruct
  Init
  SetGlobals      ; no args = set ebx
  inc ms._a      ; Olly shows inc dword ptr [ebx-80]
  inkey str$(ms._a), 13, 10
  Exit
end start


I guess you want the short encodings for a structure that is longer than 128 bytes, right?

qWord

Quote from: peter_asm on June 24, 2014, 05:17:27 AMRather than use EQU, is it possible to address _a in a structure as negative offset?
subtract the corresponding structure field offset:
e.g. : inc [eax-mystruct._b].mystruct._a

However, it seems like a bad design - you might better describe your actual problem.
MREAL macros - when you need floating point arithmetic while assembling!

dedndave

i'm with qWord - seems like bad design
but, it can be done

this might work....

mystruct STRUCT
  _a  dd ?
  _b  dd ?
  _c  dd ?
mystruct ENDS

o_a EQU mystruct._a - mystruct._b
o_b EQU mystruct._b - mystruct._b
o_c EQU mystruct._c - mystruct._b

    .DATA?

mst mystruct <>

    .CODE

    mov     eax,offset mst._b
    mov     ecx,[eax+o_a]
    mov     edx,[eax+o_c]


i theory, you should be able to replace the "+" in the last 2 lines with "."

peter_asm

okay, thanks for feedback. I'll stick with EQU instead of using structure.
To answer why I wanted to do it, I was avoiding prologue/epilogue code for local stack variables.
Just playing around with the stack, that's all.

nidud

#6
deleted

nidud

#7
deleted

RuiLoureiro

#8
Quote from: peter_asm on June 24, 2014, 07:54:04 AM
okay, thanks for feedback. I'll stick with EQU instead of using structure.
To answer why I wanted to do it, I was avoiding prologue/epilogue code for local stack variables.
Just playing around with the stack, that's all.
peter,
            it seems to be a good idea but you never need any negative offset
            to access parameters or local variables. And you should use ESP.
            The problem is to write one structure for each procedure.
            Using EQU seems to be more easy.
            nidud gives you an example (don't need «mov eax, esp»).
            See the example below

nidud

#9
deleted

FORTRANS


pre-P4 (SSE1)
------------------------------------------------------
3266213 cycles - 0: standard (ebp)
3268743 cycles - 1: no pro/epilogue (esp)
3272430 cycles - 2: no pro/epilogue (eax)

3270374 cycles - 0: standard (ebp)
3267652 cycles - 1: no pro/epilogue (esp)
3269672 cycles - 2: no pro/epilogue (eax)

3264680 cycles - 0: standard (ebp)
3267378 cycles - 1: no pro/epilogue (esp)
3288766 cycles - 2: no pro/epilogue (eax)

--- ok ---

Gunther

Older AMD chip from my University Computer:

AMD Athlon(tm) Dual Core Processor 5000B (SSE3)
------------------------------------------------------
1697913 cycles - 0: standard (ebp)
1706533 cycles - 1: no pro/epilogue (esp)
1700249 cycles - 2: no pro/epilogue (eax)

1695385 cycles - 0: standard (ebp)
1712269 cycles - 1: no pro/epilogue (esp)
1693233 cycles - 2: no pro/epilogue (eax)

1693354 cycles - 0: standard (ebp)
1711493 cycles - 1: no pro/epilogue (esp)
1695662 cycles - 2: no pro/epilogue (eax)

--- ok ---


Gunther
You have to know the facts before you can distort them.

RuiLoureiro

nidud,
           yes i know this (i did a lot of tests about it -
           for instance in converters that i posted here
           the results are here elsewhere)
           But we may gain the ebp if we need it.
           Using eax or ebp seems to give the same result in your tests.
           Generally, i write standard versions and then i modify it
           to use ESP and i test it. More or less generally it gives me
           the same result but i gain ebp.
           It seems to me that your test is not a good/best test
           in this particular case (but ... ?).
            :t

qWord

Just as side note, jWasm has the option stackbase, which allows to omit EBP as frame pointer. When modifying ESP, the equate @StackBase must be correct appropriate:
option stackbase:esp

foo proc arg:DWORD
LOCAL x:DWORD,y:DWORD

mov eax,arg
mov x,eax

push eax
@StackBase = @StackBase + 4

mov y,-123

pop eax
@StackBase = @StackBase - 4

ret

foo endp

Unfortunately the INVOKE directive currently does not respect @StackBase, thus it can't be used with locals or procedure parameters.
MREAL macros - when you need floating point arithmetic while assembling!

RuiLoureiro

qWord,
            interesting note !
            I use invoke in any case
            and i compute all things i want.  :t

nidud,
            i got this

***** Time table *****
Intel(R) Pentium(R) 4 CPU 3.40GHz (SSE3)
341  cycles, ConvertFloat10DX, direct, fxam, fxtract, esp - 10 digits
341  cycles, ConvertFloat10DY, direct, examine, fxtract, esp - 10 digits
347  cycles, ConvertReal10DX, direct, fxam, fxtract, esp - 10 digits
356  cycles, ConvertReal10DY, direct, examine, fxtract, esp - 10 digits
356  cycles, ConvertReal10DR, direct, examine, fxtract, ebp - 10 digits
356  cycles, ConvertFloat10DR, direct, examine, fxtract, ebp - 10 digits
365  cycles, ConvertFloat10DRD, direct, examine, fxtract, ebp - 10 digits
365  cycles, ConvertReal10DRD, direct, examine, fxtract, ebp - 10 digits
411  cycles, ConvertFloat10DYD, direct, examine, fxtract, esp - 10 digits
414  cycles, ConvertFloat10DXD, direct, fxam, fxtract, esp - 10 digits
418  cycles, ConvertReal10DYD, direct, examine, fxtract, esp - 10 digits
423  cycles, ConvertReal10DXD, direct, fxam, fxtract, esp - 10 digits
475  cycles, ConvertFloat10DF, direct, examine, fyl2x, ebp - 10 digits
478  cycles, ConvertFloat10DS, direct, fxam, fyl2x, ebp - 10 digits
479  cycles, ConvertReal10DF, direct, examine, fyl2x, ebp - 10 digits
486  cycles, ConvertReal10DSD, direct, fxam, fyl2x, ebp - 10 digits
487  cycles, ConvertFloat10DFD, direct, examine, fyl2x, ebp - 10 digits
487  cycles, ConvertFloat10DSD, direct, fxam, fyl2x, ebp - 10 digits
489  cycles, ConvertReal10DS, direct, fxam, fyl2x, ebp - 10 digits
492  cycles, ConvertReal10DFD, direct, examine, fyl2x, ebp - 10 digits
716  cycles, ConvertFloat10CT, BCD-CT, fxam, fxtract, esp - 10 digits
724  cycles, ConvertFloat10BX, BCD, fxam, fxtract, ebp - 10 digits
729  cycles, ConvertReal10CTD, BCD-CT, fxam, fxtract, esp - 10 digits
731  cycles, ConvertReal10BX, BCD, fxam, fxtract, ebp - 10 digits
739  cycles, ConvertFloat10BY, BCD, examine, fxtract, esp - 10 digits
739  cycles, ConvertFloat10CTD, BCD-CT, fxam, fxtract, esp - 10 digits
742  cycles, ConvertReal10BFD, BCD, fxam, fxtract, esp - 10 digits
744  cycles, ConvertFloat10BF, BCD, fxam, fxtract, esp - 10 digits
744  cycles, ConvertReal10BYD, BCD, examine, fxtract, esp - 10 digits
745  cycles, ConvertFloat10BFD, BCD, fxam, fxtract, esp - 10 digits
745  cycles, ConvertFloat10BYD, BCD, examine, fxtract, esp - 10 digits
746  cycles, ConvertReal10BY, BCD, examine, fxtract, esp - 10 digits
749  cycles, ConvertReal10BXD, BCD, fxam, fxtract, ebp - 10 digits
750  cycles, ConvertFloat10BXD, BCD, fxam, fxtract, ebp - 10 digits
752  cycles, ConvertReal10BF, BCD, fxam, fxtract, esp - 10 digits
775  cycles, ConvertReal10CT, BCD-CT, fxam, fxtract, esp - 10 digits
1147  cycles, ConvertFloat10ZX, BCD - old - 10 digits
1154  cycles, ConvertFloat10Z, BCD -old - 10 digits
2704  cycles, ConvertFloat10DWD, direct,Save FPU, fxam, fxtract, esp -10 digits
2709  cycles, ConvertReal10DWD, direct,Save FPU, fxam, fxtract, esp -10 digits
2798  cycles, ConvertFloat10DW, direct,Save FPU, fxam, fxtract, esp -10 digits
2816  cycles, ConvertReal10DW, direct,Save FPU, fxam, fxtract, esp -10 digits
2989  cycles, ConvertFloat10BW, BCD, Save FPU, fxam, fxtract, ebp - 10 digits
2995  cycles, ConvertReal10BW, BCD, Save FPU, fxam, fxtract, ebp - 10 digits
3042  cycles, ConvertFloat10, BCD, Save FPU -old - 10 digits
3104  cycles, ConvertFloat10BWD, BCD, Save FPU, fxam, fxtract, ebp - 10 digits
3126  cycles, ConvertReal10BWD, BCD, Save FPU, fxam, fxtract, ebp - 10 digits
********** END **********