flat assembler
Message board for the users of flat assembler.

Index > Windows > Having trouble using C variables from FASM on windows

Author
Thread Post new topic Reply to topic
therektafire



Joined: 06 Dec 2023
Posts: 4
therektafire 01 Jan 2026, 17:44
So I started writing a retro 2.5d game engine in C recently and I decided I wanted to try to use SIMD code in the renderer, I wanted to use FASM to write the code and link it with my C code which I've sort of figured out how to do, unfortunately I had to remove some optimization options and just use -O2 only in order for either GCC or clang to actually accept my .o file for some reason but that's beside the point. Anyway the main issue is I can't seem to properly access my C global variables from my FASM code even when I have them "extrn"ed which is obviously going to be kind of an issue for me later on, I *can* access functions though. Here's some example C and asm code that I'm having an issue with:

The c:
Code:
#include<stdio.h>

const char *helloMessage = "Hello from Fasm...\n";

int main(int argc, char *argv[])
{
    puts("from c:");
    printf(helloMessage);
    puts("from assembly:");
    helloFromFasm();
    return 69;
}
    


And the asm:

Code:
format ELF64

extrn helloMessage
extrn printf

public helloFromFasm
helloFromFasm:
    sub rsp, 32
    xor rax, rax
    ;works
    ;lea rcx, [infasmhello]
    ;mov rcx, infasmhello
    ;doesn't work for some reason?
    ;lea rcx, [helloMessage]
    mov rcx, helloMessage
    call printf
    add rsp, 32
    ret

infasmhello:
db "hello inside of fasm...", 10, 0

    


Printing the "infasmhello" message works when calling printf but trying to print "helloMessage" just gives either nothing or garbage even though I was literally able to link the asm and c code with no linker errors so presumably the linker was able to find helloMessage and use it in the assembly code...

I am pretty inexperienced with c > asm interop though so maybe there's something obvious I'm not doing correctly that's causing it to not work, if so let me know. Also if it matters the GCC and Clang I'm using are from msys2 ("MINGW64" gcc and "CLANG64" clang respectively)
Post 01 Jan 2026, 17:44
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20814
Location: In your JS exploiting you and your system
revolution 01 Jan 2026, 20:18
The C code has helloMessage as a pointer, so:
Code:
mov rcx, [helloMessage]    
Post 01 Jan 2026, 20:18
View user's profile Send private message Visit poster's website Reply with quote
therektafire



Joined: 06 Dec 2023
Posts: 4
therektafire 01 Jan 2026, 21:23
revolution wrote:
The C code has helloMessage as a pointer, so:
Code:
mov rcx, [helloMessage]    


Oh yeah duh, that makes sense lol.

I do still technically have another issue though. When I add
Code:
-ffunction-sections -fdata-sections -Wl, --gc-sections
    

to my compile options I get an undefined reference to printf error in the asm. What would cause that when the function shouldn't be getting stripped out since it's also being used in C as you can see in the code I originally posted? Is it just renaming the function to something else? It doesn't seem to have done the same thing to helloMessage so i'm pretty confused
Post 01 Jan 2026, 21:23
View user's profile Send private message Visit poster's website Reply with quote
therektafire



Joined: 06 Dec 2023
Posts: 4
therektafire 01 Jan 2026, 22:43
Never mind, I was finally able to figure it out, I enabled LTO and the LTO actually optimized out printf from the C code because it noticed I wasn't using any actual formatting in there and replaced it with puts, replacing the call to printf with puts allowed it to compile again. Though I'm noticing my c + asm test program is twice as big as the c only one for some reason, 251kb vs 117kb Rolling Eyes I mean I guess it doesn't matter *that* much since that isn't that big of an increase in the grand scheme of things but I'm not sure why the c + asm version isn't able to remove as much stuff with the exact same compile options

edit: actually wait, it didn't replace printf with puts, it replaced printf with... __mingw_printf. UUUUGH
Post 01 Jan 2026, 22:43
View user's profile Send private message Visit poster's website 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.