flat assembler
Message board for the users of flat assembler.

Index > Main > Address of local variable in fasmg

Author
Thread Post new topic Reply to topic
donn



Joined: 05 Mar 2010
Posts: 321
donn 01 Oct 2019, 17:12
Newbie fasmg question, wasn't able to find the answer yet via search:

I have a function defined like so:

Code:
storage:
namespace storage
storage.retrieve:
        push rbp 
        mov rbp, rsp 
        sub rsp, (8*6);+(8*6)

        ;mov rcx, [rbp+8] 
        ;mov rdx, [rbp+12] 
        label retrieve.heapAddress qword at rbp-8  
        label retrieve.handleAllocatedMemory qword at rbp-16
        label retrieve.list qword at rbp-24 
        label retrieve.item qword at rbp-32
        label retrieve.recordAddr qword at rbp-40
        label retrieve.sizeRetrieved qword at rbp-48
        ;virtual at rbp-64
        ;       .list List
        ;end virtual    


        mov [retrieve.recordAddr], rcx


        and rsp, -32
        push rbx 
        push rbp 
        push rdi 
        push rsi
        push rsp 
        push r12 
        push r13 
        push r14 
        push r15
        sub rsp, 8

        sub rsp, 8*8
        mov qword [rsp+6*8], 0                                  ;hTemplateFile
        mov qword [rsp+5*8], 128                                ;dwFlagsandAttributes
        mov qword [rsp+4*8], 000000100b                         ;dwCreationDisposition
        mov r9, 0                                               ;lpSecurityAttributes
        mov r8, 000000010b                                      ;dwShareMode
        mov rdx, 10100000000000000000000100000101b              ;dwDesiredAccess
        mov rbx, [retrieve.recordAddr]
        add rbx, Record.builtLocation
        mov rcx, [rbx]
        call [CreateFile]
        add rsp, 8*8
        mov rbx, [retrieve.recordAddr]
        add rbx, Record.handle
        mov [rbx], rax

        mov rbx, [retrieve.recordAddr]
        add rbx, Record.handle
        mov rcx, [rbx]
        sub rsp, 8*4
        call [SetEndOfFile]
        add rsp, 8*4


        sub rsp, 8*8
        mov rdx, 0                              ; File size high
        mov rbx, [retrieve.recordAddr]
        add rbx, Record.handle
        mov rcx, [rbx]
        call [GetFileSize]
        add rsp, 8*8
        mov rbx, [retrieve.recordAddr]
        add rbx, Record.allocSize
        mov rcx, 0
        mov ecx, eax
        mov [rbx], rcx

        sub rsp, 8*4
        call [GetProcessHeap]
        mov [retrieve.heapAddress], rax
        add rsp, 8*4
        

        sub rsp, 8*4
        mov rbx, [retrieve.recordAddr]
        add rbx, Record.allocSize
        mov r8, [rbx]   
        mov rdx, 000001000b
        mov rcx, [retrieve.heapAddress]
        call [ HeapAlloc] 
        mov rbx, [retrieve.recordAddr]
        add rbx, Record.allocAddr
        mov [rbx], rax
        add rsp, 8*4



        sub rsp, 8*8
        mov r10, 0
        mov [rsp+4*8], r10
        mov r9, retrieve.sizeRetrieved
        mov rbx, [retrieve.recordAddr]
        add rbx, Record.allocSize
        mov r8, [rbx]
        mov rbx, [retrieve.recordAddr]
        add rbx, Record.handle
        mov rdx, [rbx]
        mov rbx, [retrieve.recordAddr]
        add rbx, Record.allocAddr
        mov rcx, [rbx]
        call [ReadFile]
        add rsp, 8*8
...
    


Assembling is showing an error:

Code:
1>PreBuildEvent:
1>  flat assembler  version g.i4pue
1>  ..\src\main\asm\datap.asm [10] ..\src\main\asm\datap.inc [3] ..\src\main\asm\Includes.inc [4] ..\src\main\asm\Include/Storage.inc [1] ..\src\main\asm\Include/../Storage/Storage.inc [240]:
1>      mov r9, retrieve.sizeRetrieved
1>  macro mov [129] macro dq [13] macro qword [21]:
1>      dq v
1>  Processed: dq v
1>EXEC : error : variable term used where not expected.    


Code:
mov r9, [retrieve.sizeRetrieved]    


works.

Code:
mov r9, retrieve.sizeRetrieved    


does not. Think accessing the 'address of' local variables used to work this way with fasm1. Any pointers?


Much appreciated.
Post 01 Oct 2019, 17:12
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 01 Oct 2019, 17:23
It would not work with fasm 1 either, because what you try to do is assemble instruction like:
Code:
mov r9, rbp-48    
And this is not possible. You have to use LEA instead:
Code:
lea r9, [retrieve.sizeRetrieved]    


The FASTCALL/INVOKE macros (both for fasm and fasmg) provide special prefix ADDR that makes LEA be generated automatically by macro. But when you write your code directly, you need to choose the appropriate instruction yourself.
Post 01 Oct 2019, 17:23
View user's profile Send private message Visit poster's website Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 01 Oct 2019, 17:52
OK gotcha..!

Must have confused this in fasm1 with accessing struc member addresses:

Code:
        struc Record
                label .
                .allocAddr dq 0         ; Binary or text allocation addr
...
                .builtLocation dq 0     ; Built Path
                .handle dq 0
        end struc


        ; Used with address offsets
        virtual at 0
                Record Record
        end virtual


...

        mov rbx, [new.handleAllocatedMemory]
        add rbx, Record.handle
        mov rax, 0
        mov [rbx], rax    


Specifically this part:
Code:
        add rbx, Record.handle    


Your explanation of what the instruction becomes makes sense. Working on a new project with fasmg now by the way, utilizing GoogleTest with great satisfaction. Unit testing helps when I have to work on a project, switch gears, then come back days or weeks later and ensure things are still working. Curious if anyone else uses a test tool also...
Post 01 Oct 2019, 17:52
View user's profile Send private message Reply with quote
guignol



Joined: 06 Dec 2008
Posts: 763
guignol 02 Oct 2019, 07:16
only donkeys
Post 02 Oct 2019, 07:16
View user's profile Send private message Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 04 Oct 2019, 15:08
I see where you're coming from, but when refactoring large assembly projects, how do you test out the components to make sure they're still working? Manually? Isn't that donkey work?

I'm certainly not the authority on assembly unit testing, as you can see from this post I ignored a somewhat fundamental stack concept for years and am just now trying to come to terms with it, but I think assembly could take another step towards becoming a professional alternative to C with testing tooling.

Especially on collaborative-projects, an aunit could bridge communication and lead to bigger, more stable projects. I tried building a test tool briefly, fasmg seemed like it could perfectly enable and 'discover' test modules, but I'm not good enough at fasmg and was not sure how to hierarchically define test suite namespaces.

Conceptually, I definitely prefer the concept of a test harness as 'test driving' the software, instead of as acting like 'guard rails.' The latter makes the author or tool seem less serious or incapable.
Post 04 Oct 2019, 15:08
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.