flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > preprocessor/ include confusion

Author
Thread Post new topic Reply to topic
B.Dubious



Joined: 04 Apr 2020
Posts: 17
B.Dubious
I'm new to fasm, and have very little assembly programming experience in general.
I was under the impression that the include was part of the preprocessor and was just a
text dump like with c, but that doesn't appear to be the case. I was trying to pull my
.data section out into a main_data.asm and include it, such that I could see my data
in a seperate vim split. I could easily enough just have the same buffer open, but I
appear to be confused about how the preprocessor stuff works? Why doesn't including the .data section in this manner achieve the desired functionality? Confused

ps. any tips on debugging would be helpful, I was looking for something that would convert the .fas to .pdb so I can use visual studio, but I couldn't find anything.
Post 04 Apr 2020, 01:18
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
include should work perfectly fine in the way you say.

Show your code and we can help.
Post 04 Apr 2020, 01:53
View user's profile Send private message Visit poster's website Reply with quote
B.Dubious



Joined: 04 Apr 2020
Posts: 17
B.Dubious
I'm sure this will be embarrassing for me!
I'll paste the code below as I don't see a way to upload files, nor do I see a method to format.
I've marked the relevant section which I saved out into a seperate file with //@start and //@end blocks... Sorry!
I'm also struggling to workout how to deal with structs as you can probably see...
I just simply yanked the data section out into the seperate file and saved the changes. When I went to build it failed on:
mov [WindowClass.hInstance], rax
in the .text section.
If its relevant I'm building from the command line through a .bat file.

If you could give some advice regarding the structs and the most appropriate way to set local aliases or whatever they're properly called. I'm still trying to grok rather a large proportion of the fundamentals.

--------------------
Code:
format PE64
entry main 

include 'fasm\INCLUDE\win64a.inc'
; include '.\main_data.asm'

//@start This here is the section I wanted to store in main_data.asm
section '.data' data readable writeable 

        GlobalRunning db 0


struc draw_buffer BmpInfoHeader, PixelPtr, Width, Height
{
        .BitmapInfoHeader dq BmpInfoHeader
        .PixelPointer dq PixelPtr
        .Width dq Width
        .Height dq Height
}

        DrawBufferBMPInfoHeader BITMAPINFOHEADER
        GlobalDrawBuffer draw_buffer DrawBufferBMPInfoHeader,0,0,0
        
        WindowClassName db "win32app",0
        WindowTitle db "win32 Apllication",0
        WindowRegErrorMessage db "Error encountered when registering window class: closing",0
        WindowCreateErrorMessage db "Error encountered when creating window: closing",0

  WindowClass WNDCLASSEX sizeof.WNDCLASSEX,0,Win32CallbackProc,0,0,0,0,0,COLOR_WINDOW,0,WindowClassName,0
        Message MSG
        WindowHandle dq 0


//@end 

section '.text' code readable executable 
main:

        sub     rsp,8
        invoke  GetModuleHandle,0
        mov     [WindowClass.hInstance], rax

        invoke LoadIcon,0,IDI_APPLICATION
        mov     [WindowClass.hIcon],rax
        mov     [WindowClass.hIconSm],rax
        invoke LoadCursor,0,IDC_ARROW
        mov     [WindowClass.hCursor],rax
        
        invoke RegisterClassExA,WindowClass
        test rax, rax
        jz registration_error 

        invoke CreateWindowExA,0,WindowClassName,WindowTitle, WS_VISIBLE+WS_OVERLAPPEDWINDOW,\
                        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0,0,[WindowClass.hInstance],0
        mov [WindowHandle], rax

        test rax, rax
        jz creation_error

        
        stdcall InitDrawBuffer,GlobalDrawBuffer

        mov [GlobalRunning], 1
.loop:
        cmp [GlobalRunning], 0
        je exit

        call Win32MessagePump
        jmp .loop


registration_error:
        invoke MessageBoxA,0,WindowRegErrorMessage,WindowTitle,MB_ICONERROR+MB_OK
        jmp exit

creation_error:
        invoke MessageBoxA,0,WindowCreateErrorMessage,WindowTitle,MB_ICONERROR+MB_OK
        ;jmp exit

exit:
invoke  ExitProcess,[Message.wParam]

;---

proc InitDrawBuffer uses BitmapInfo, Pixels, Width, Height

int3
;TODO lea? whats a better way to do this... this is **** 
        mov [BitmapInfo], rcx
        mov rax, [rcx+8]
        mov [Pixels], rax
        mov rax, [rcx+16]
        mov [Width], rax
        mov rax, [rcx+24]
        mov [Height], rax

        mov [bmiHeader.biSize], sizeof.BitmapInfo.bmiHeader
        ;mov [bmiHeader.biWidth], [Width]
        ;mov [bmiHeader.biHeight], [Height]
        mov [bmiHeader.biWidth], 500
        mov [bmiHeader.biHeight], 900
        mov [bmiHeader.biPlanes], 1
        mov [bmiHeader.biBitCount], 32
        mov [bmiHeader.biCompression], BI_RGB

        ret
endp


proc Win32DisplayBuffer 

        
        invoke GetDC,WindowHandle 
        ;invoke StretchDIBits,rax,0,0,Width,Height,0,0,Width,Height,Pixels,BitmapInfo,DIB_RGB_COLORS, SRCCOPY

        ret
endp

proc Win32MessagePump; uses WindowHandle

.while_message: 

        ;invoke PeekMessageA, Message, WindowHandle,0,0, PM_REMOVE   ;TODO why does this freeze up?
        invoke GetMessage,Message,0,0,0 ;TODO replace with peekmessage

        cmp eax, 0
        je .end
        
        cmp [Message.message], WM_QUIT
        mov [GlobalRunning], 0
        je .end

        cmp [Message.message], WM_PAINT
        je .paint

        cmp [Message.message], WM_KEYDOWN
        je .keydown

        invoke TranslateMessage,Message
        invoke DispatchMessage,Message

        jmp .while_message

.paint:
        push rax
        call Win32DisplayBuffer 
        pop rax
        jmp .while_message

.keydown:
        call Win32DisplayBuffer
        jmp .while_message

.end:
        
        ret
endp

proc Win32CallbackProc ;proc WndProc uses rbx rsi rdi, hwnd,wmsg,wparam,lparam
        cmp edx, WM_DESTROY 
        je .destroy

  .default:
                                invoke  DefWindowProcA,rcx,rdx,r8,r9
        jmp .end
  .destroy:
        invoke PostQuitMessage,0
        xor rax,rax
  .end:
        ret
endp


section '.idata' import data readable writeable

  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL',\
                gdi, 'GDI32.DLL',\

  import kernel,\
         GetModuleHandle,'GetModuleHandleA',\
         ExitProcess,'ExitProcess'

  import user,\
         RegisterClassExA,'RegisterClassExA',\
         CreateWindowExA,'CreateWindowExA',\
         ShowWindow,'ShowWindow',\
         UpdateWindow,'UpdateWindow',\
         DefWindowProcA,'DefWindowProcA',\
         GetMessage,'GetMessageA',\
         TranslateMessage,'TranslateMessage',\
         DispatchMessage,'DispatchMessageA',\
         LoadCursor,'LoadCursorA',\
         LoadIcon,'LoadIconA',\
         GetClientRect,'GetClientRect',\
         GetDC,'GetDC',\
         ReleaseDC,'ReleaseDC',\
         BeginPaint,'BeginPaint',\
         EndPaint,'EndPaint',\
         PostQuitMessage,'PostQuitMessage',\
         MessageBoxA, 'MessageBoxA',\
         PeekMessageA, 'PeekMessageA'

        import gdi,\
                StretchDIBits, 'StretchDIBits'    
Edit by revolution: added code tags
Post 04 Apr 2020, 02:07
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
The include is not a problem there.

But this will cause trouble:
Code:
                gdi, 'GDI32.DLL',\ ; <--- extra trailing comma    
And this is not ideal:
Code:
proc InitDrawBuffer uses BitmapInfo, Pixels, Width, Height    
The "uses" clause is mostly designed for registers to save/restore. If you want to assign local variables then you can use the "locals/endl" macros.
Post 04 Apr 2020, 02:23
View user's profile Send private message Visit poster's website Reply with quote
B.Dubious



Joined: 04 Apr 2020
Posts: 17
B.Dubious
I've tried again and it's working now, I'm not sure what was wrong before... it could be that I was being an eejit somehow, it is 0330. I'm entirely sure I saved the buffers so perhaps I introduced some other error.

I think that extra trailing was something I added since the first try though, I was building cleanly then, and I did try to add another .lib to the list though I removed it again.

I had a feeling I was misusing the uses keyword there, I'll look into these locals macros because all I really wanted was a local alias for the registers.
Thanks for the guidance Smile

I should read the macros section of the docs thoroughly tomorrow.

ps. are the code blocks markdown?
Post 04 Apr 2020, 02:33
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
B.Dubious wrote:
ps. are the code blocks markdown?
BBCode.

Examine one of the previous messages with the quote button to see how it works.

There is also the "Test" forum if you want to try stuff out.


Last edited by revolution on 04 Apr 2020, 03:04; edited 1 time in total
Post 04 Apr 2020, 02:38
View user's profile Send private message Visit poster's website Reply with quote
B.Dubious



Joined: 04 Apr 2020
Posts: 17
B.Dubious
Cool, I'll try to use the proper markup in future.
For now I'll try and make a little more progress with this and get to bed. Thanks for all your help Smile
Post 04 Apr 2020, 02:49
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.