flat assembler
Message board for the users of flat assembler.

Index > Windows > About memory buffer

Author
Thread Post new topic Reply to topic
AE



Joined: 07 Apr 2022
Posts: 70
AE 19 Nov 2022, 18:17
The full title of the topic should sound something like "Memory buffer as a replacement for the data section for dynamic stuff".
Code:
struct ST
    a      dq ?
    b      dt ?
    c      dt ?
ends    

  1. Do I understand correctly that this structure will not take up space in the file after compilation, and only needed to generate offsets in the code?
  2. If I plan to allocate a memory buffer at the beginning of the code and use it as a variable store, how do I assign variable aliases to the addresses?
    It is logical to assume that a structure can be used for this, but here we go back to the previous question...

Could you give some advice on this?
Post 19 Nov 2022, 18:17
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1042
Location: Russia
macomics 19 Nov 2022, 18:58
I do not use the proposed struct macros, but define structures using struc. My solution for this problem
Code:
use64

struc assume base*, type& { if ~ type eq
    virtual at base
      . type
      .Length = $ - $$
    end virtual
  else
    label . at base
    .Length = 0
  end if }

struc POINT vX=0, vY=0 {
  .x  dd vX
  .y  dd vY }

struc ST {
  .a  dq ?
  .b  dt ?
  .c  dt ?
  .d  POINT ?, ? }
; ...
memory_pointer dq ? ; A pointer to the allocated memory block is stored here
; ...
memory_area     assume  rbx, ST
cursor  assume  memory_area.d, POINT ?, ?
    mov rbx, [memory_pointer]
    mov rax, [memory_area.a] ; rax = qword [rbx + 0]
    fld [memory_area.b] ; st0 = tbyte [rbx + 8]
    fbstp [memory_area.c] ; tbyte [rbx + 18] = bcd(st0)
    mov edx, [memory_area.d.x] ; edx = dword [rbx + 28]
    mov [cursor.x], eax ; dword [rbx + 28] = eax

; flat assembler  version 1.73.30  (1024 kilobytes memory)
; 1 passes, 30 bytes.

; 00000000  00 00 00 00 00 00 00 00  48 8b 1d f1 ff ff ff 48  |........H......H|
; 00000010  8b 03 db 6b 08 df 73 12  8b 53 1c 89 43 1c        |...k..s..S..C.|    


Last edited by macomics on 20 Nov 2022, 09:11; edited 4 times in total
Post 19 Nov 2022, 18:58
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1847
Roman 20 Nov 2022, 08:28
macomics wrote:
I do not use the proposed struct macros, but define structures using struc. My solution for this problem
Code:
struc assume base*, type& { if ~ type eq
    virtual at base
      . type
      .Length = $ - .
    end virtual
  else
    label . at base
    .Length = 0
  end if }

struc POINT vX=0, vY=0 {
  .x  dd vX
  .y  dd vY
}

struc ST {
  .a  dq ?
  .b  dt ?
  .c  dt ?
  .d POINT
}
...
memory_pointer dq ? ; A pointer to the allocated memory block is stored here
...
memory_area assume rbx, ST
cursor assume memory_area.d, POINT ?, ?
    mov rbx, [memory_pointer]
    mov rax, [memory_area.a] ; rax = qword [rbx + 0]
    fld [memory_area.b] ; st0 = tbyte [rbx + 8]
    fbstp [memory_area.c] ; tbyte [rbx + 18] = bcd(st0)
    mov edx, [memory_area.d.x] ; edx = dword [rbx + 28]
    mov [cursor.x], eax ; dword [rbx + 28] = eax    


cursor assume memory_area.d, POINT 0, 0 ;error invalid macro arguments fasmw 1.73
assume cool stuff !
I changed:
Code:
 ppcursor assume memory_area.d, POINT 0, 0 ;and fasm compiled this.
...
 mov [ppcursor.x], eax 
    
Post 20 Nov 2022, 08:28
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1042
Location: Russia
macomics 20 Nov 2022, 08:53
Corrected typos.
Post 20 Nov 2022, 08:53
View user's profile Send private message Reply with quote
AE



Joined: 07 Apr 2022
Posts: 70
AE 20 Nov 2022, 10:49
Thank you!
Is there no way to keep the variable names in their original form?
Otherwise I have to rewrite a lot of code where they are used.
Post 20 Nov 2022, 10:49
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1042
Location: Russia
macomics 20 Nov 2022, 13:29
Code:
; ./test.asm
use64

macro global name& { if 0
        label name
    end if }

struc assume base*, type& { if ~ type eq
    virtual at base
      . type
      .Length = $ - $$
    end virtual
    global .
  else
    label . at base
    .Length = 0
  end if }

struc POINT vX=0, vY=0 {
  .x  dd vX
  .y  dd vY }

struc ST {
  .a  dq ?
  .b  dt ?
  .c  dt ?
  .d  POINT ?, ? }
; ...
memory_pointer dq ? ; A pointer to the allocated memory block is stored here
; ...
memory_area     assume  rbx, ST
    mov rbx, [memory_pointer]
    mov rax, [.a] ; rax = qword [rbx + 0]
    fld [.b] ; st0 = tbyte [rbx + 8]

cursor  assume  .d, POINT ?, ?

    mov [.x], eax ; dword [rbx + 28] = eax

global memory_area

    fbstp [.c] ; tbyte [rbx + 18] = bcd(st0)
    mov edx, [.d.y] ; edx = dword [rbx + 32]

; $ fasm -m 1024 ./test.asm ./test.bin
; flat assembler  version 1.73.30  (1024 kilobytes memory)
; 1 passes, 30 bytes.

; $ hexdump -C ./test.bin
; 00000000  00 00 00 00 00 00 00 00  48 8b 1d f1 ff ff ff 48  |........H......H|
; 00000010  8b 03 db 6b 08 89 43 1c  df 73 12 8b 53 20        |...k..C..s..S |    
Post 20 Nov 2022, 13:29
View user's profile Send private message Reply with quote
AE



Joined: 07 Apr 2022
Posts: 70
AE 20 Nov 2022, 13:47
Image
Thanks!
Post 20 Nov 2022, 13:47
View user's profile Send private message Reply with quote
AE



Joined: 07 Apr 2022
Posts: 70
AE 12 Jan 2023, 04:30
A lot of questions...

At first everything seems very convenient, but as the code is written, inconveniences begin to come to light.
It looks like this directive significantly changes how the compiler works.

  • Named labels in code give errors (Anonymous labels works fine)
  • Some types of structures give errors [Solved]

  • Can I use structure members without dots?
  • How to "turn this mode off"?

Maybe it makes sense to include 'MACRO\MASM.INC' and use 'assume reg:STRUCT' ?
What is the purpose of 'assumed' macro in MASM.INC ?

I'll try to explain again
I would like a simple way to "map" the page of allocated memory for variables in the form of a structure.
Why structure?
Of course, you can access variables by offset, but when adding or changing some of them the offsets will no longer be relevant and you will have to change a lot of places in the code.
When accessing structured data, there is no such problem.

Simple example:
Code:
    struc PMEM {
        MajorVersion                    dd ?
        MinorVersion                    dd ?
        ImagePathName                   dq ?
        Mouse                           POINT 
        ImageName                       dq ?
        wc                              WNDCLASSEX
        hWnd                            dq ?
        MonInfo                         MONITORINFO
    }

; alloc some memory
mov     [MyMem], baseaddr ; save addr
; ...
; Uncomfortable version
mov     rax, [MyMem+8]  ; get ImagePathName
; And I would like at least (or kind of)
mov     rax, [MyMem.ImagePathName]
; looks like just
mov     rax, [ImagePathName]
; requires complex code and introduces more confusion and compiler errors    


A little chaotic, but I hope you understand Smile
In general, is there a simple way to do what was intended without changing the usual style of writing code and without breaking the usual behavior of the compiler?


Last edited by AE on 12 Jan 2023, 07:00; edited 2 times in total
Post 12 Jan 2023, 04:30
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1042
Location: Russia
macomics 12 Jan 2023, 05:29
AE wrote:
Code:
IMEM    assume  rbx, PMEM
[45] assume [2]:
          . type
[91] PMEM [11]:
        .MonInfo                         MONITORINFO ?
error: invalid macro arguments.    
Check the definition of the '.MonInfo MONITORINFO ?' structure. This line contains an extra argument for the structure (?).

The dots inside the struct macro are attached to each name for you. Just for the sake of this, I don't see the point in using this macro.

The compiler's behavior most likely violates global. Try not to use this macro all the time and inside functions, because function macros also change the names of labels.
Post 12 Jan 2023, 05:29
View user's profile Send private message Reply with quote
AE



Joined: 07 Apr 2022
Posts: 70
AE 12 Jan 2023, 06:20
Yes tis was a typo (extra ?). Thx.

Also can't use it in invoke args, have to use lea
Like this
Code:
lea     rcx, [.wc]
invoke  RegisterClassEx, rcx    

And seems like it can't handle
Code:
mov     rax, [.MonInfo.rcMonitor.right]
; processed: mov rax,[.MonInfo.rcMonitor.right]
; error: undefined symbol 'IMEM.MonInfo.rcMonitor.right'
    


Quote:
Try not to use this macro all the time

I just use it ones in the beginning of the code.

Maybe there is a simpler and more flexible way to "map" structure to a "memory variable"?
Let it be a little less convenient, the main thing is not to create such problems.

Something like
Code:
    struc PMEM {
        MajorVersion                    dd ?
        MinorVersion                    dd ?
        ImagePathName                   dq ?
        Mouse                           POINT
        ImageName                       dq ?
        wc                              WNDCLASSEX
        hWnd                            dq ?
        MonInfo                         MONITORINFO
    }

  MyMem    PMEM  sizeof.PMEM    

But only in runtime for allocated memory address...
Post 12 Jan 2023, 06:20
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1042
Location: Russia
macomics 12 Jan 2023, 08:14
AE wrote:
Code:
mov     rax, [.MonInfo.rcMonitor.right]
; processed: mov rax,[.MonInfo.rcMonitor.right]
; error: undefined symbol 'IMEM.MonInfo.rcMonitor.right'    
Show me how you have declared the MONITORINFO structure?

AE wrote:
macomics wrote:
Try not to use this macro all the time

I just use it ones in the beginning of the code.
I'm talking about the global macro. The assume macro should be used 1 time. This is more than enough to define a global description of the block.

AE wrote:
Something like
Code:
    struc PMEM {
        MajorVersion                    dd ?
        MinorVersion                    dd ?
        ImagePathName                   dq ?
        Mouse                           POINT
        ImageName                       dq ?
        wc                              WNDCLASSEX
        hWnd                            dq ?
        MonInfo                         MONITORINFO
    }

  MyMem    PMEM  sizeof.PMEM     
Instead of sizeof, the assume macro sets the .Length value.
Code:
struc assume base*, type& { if ~ type eq
    virtual at base
      . type
      .Length = $ - $$
    end virtual
  else
    label . at base
    .Length = 0
  end if }

    struc PMEM {
        MajorVersion                    dd ?
        MinorVersion                    dd ?
        ImagePathName                   dq ?
        Mouse                           POINT
        ImageName                       dq ?
        wc                              WNDCLASSEX
        hWnd                            dq ?
        MonInfo                         MONITORINFO
    }

  MyMem    assume rbx
  mov eax, MyMem.Length    



If you need to declare only one description, then you can not use the assume structure, but write everything manually.
Code:
    struc PMEM {
        MajorVersion                    dd ?
        MinorVersion                    dd ?
        ImagePathName                   dq ?
        Mouse                           POINT
        ImageName                       dq ?
        wc                              WNDCLASSEX
        hWnd                            dq ?
        MonInfo                         MONITORINFO
    }

virtual at rbx
  MyMem PMEM
end virtual    



P.S. Why do you have a parameter passed to it in the form of sizeof.PMEM when declaring MyMem? However there are no formal parameters in the PMEM declaration.
Post 12 Jan 2023, 08:14
View user's profile Send private message Reply with quote
AE



Joined: 07 Apr 2022
Posts: 70
AE 12 Jan 2023, 08:45
macomics wrote:
Show me how you have declared the MONITORINFO structure?
Code:
    struc MONITORINFO {
        cbSize          dd ?
        rcMonitor       RECT ?
        rcWork          RECT ?
        dwFlags         dd ?
    }    

Damn, again that "?" in structures types Smile
But error have nothing to do with that BTW. So the question is open.
Quote:
If you need to declare only one description

I don't quite understand that sentence. What does "one description" mean?
Quote:
P.S
Unfortunately, I didn't understand exactly what you are talking about either.

And I guess in your last example all of the structure members must be prepended with dots for it works with [MyMem.hWnd].
Post 12 Jan 2023, 08:45
View user's profile Send private message Reply with quote
AE



Joined: 07 Apr 2022
Posts: 70
AE 12 Jan 2023, 10:43
I eventually came to avoid using macros and just do this
Code:
    struct MONITORINFO
        cbSize          dd ?
        rcMonitor       RECT
        rcWork          RECT
        dwFlags         dd ?
    ends

    struct M
        MajorVersion                    dd ?
        MinorVersion                    dd ?
        ImagePathName                   dq ?
        Mouse                           POINT
        ImageName                       dq ?
        wc                              WNDCLASSEX
        hWnd                            dq ?
        MonInfo                         MONITORINFO
    ends


    mov     r15, [IntMem]
    ;...
    invoke MonitorFromPoint, qword [r15+M.Mouse], 2
    mov     [r15+M.MonInfo.cbSize], sizeof.MONITORINFO
    lea     rdx, [r15+M.MonInfo]
    invoke GetMonitorInfo, rax, rdx    


not very pretty, but it works and there are no side effects...

Thanks anyway, learned a couple of cool tricks!
Post 12 Jan 2023, 10:43
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 1042
Location: Russia
macomics 12 Jan 2023, 10:48
AE wrote:
Unfortunately, I didn't understand exactly what you are talking about either.


Code:
;             v - no formal parameters
    struc PMEM {
        MajorVersion                    dd ?
        MinorVersion                    dd ?
        ImagePathName                   dq ?
        Mouse                           POINT
        ImageName                       dq ?
        wc                              WNDCLASSEX
        hWnd                            dq ?
        MonInfo                         MONITORINFO
    }
;                 v - actual parameter for PMEM
  MyMem    PMEM  sizeof.PMEM    
Post 12 Jan 2023, 10:48
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.