flat assembler
Message board for the users of flat assembler.

Index > Windows > help converting 32 to 64 for displaying bitmaps from memory

Author
Thread Post new topic Reply to topic
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog
I found this bit of code from Vortex on the forum that will Displaying bitmaps from memory. http://board.flatassembler.net/topic.php?p=11521#11521

it works great for an about box with your websites logo. this is the meat of the program and the piece that I use in my templates

Code:

.wmcreate:
        invoke  GetClientRect,[hwnd],client
        lea     eax,[pBitmap+14] ; start of BITMAPINFOHEADER header
        invoke  CreateDIBSection,0,eax,DIB_RGB_COLORS,ppvBits,0,0
        mov     [hBitmap],eax
        lea     eax,[pBitmap+54] ; + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
        stdcall MemCopy,eax,[ppvBits],149346-54 ; copy bitmap's bit values

.wmpaint:
        lea   eax,[ps]
        invoke  BeginPaint,[hwnd],eax
        mov     [hdc],eax
        invoke  CreateCompatibleDC,eax
        mov     [hMemDC],eax
        invoke  SelectObject,eax,[hBitmap]
        invoke  BitBlt,[hdc],0,0,429,80,[hMemDC],0,0,SRCCOPY
        invoke  DeleteDC,[hMemDC]
        lea     eax,[ps]
        invoke  EndPaint,[hwnd],eax

proc MemCopy,Source,Dest,ln   ; procedure from masm32 library

        cld
        mov esi, [Source]
        mov edi, [Dest]
        mov ecx, [ln]
        shr ecx, 2
        rep movsd
        mov ecx, [ln]
        and ecx, 3
        rep movsb
        ret
endp

.data

pBitmap         FILE 'fish.bmp'
caption         db 'Bitmap from memory',0
class           db 'BitmapClass',0
mainhwnd        dd ?
hBitmap         dd ?
ppvBits         dd ?
ps PAINTSTRUCT
hdc dd ?
hMemDC dd ?                        


    


the problem is I am trying to figure out how to convert this code to 64 bit for my 64 bit programing template. I tired the usual eax->rax etc and changed some of the dd to dq. it compiles but does not display the image. instead it displays a black square. here is what I tried

Code:

.wmcreate:
        invoke  GetClientRect,[hwnd],client
        lea     rax,[pBitmap+14] ; start of BITMAPINFOHEADER header
        invoke  CreateDIBSection,0,rax,DIB_RGB_COLORS,ppvBits,0,0
        mov     [hBitmap],rax
        lea     rax,[pBitmap+54] ; + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)
        stdcall MemCopy,rax,[ppvBits],149346-54 ; copy bitmap's bit values 

.wmpaint:
        lea   rax,[ps]
        invoke  BeginPaint,[hwnd],rax
        mov     [hdc],rax
        invoke  CreateCompatibleDC,rax
        mov     [hMemDC],rax
        invoke  SelectObject,rax,[hBitmap]
        invoke  BitBlt,[hdc],0,0,429,80,[hMemDC],0,0,SRCCOPY
        invoke  DeleteDC,[hMemDC]
        lea     rax,[ps]
        invoke  EndPaint,[hwnd],rax

proc MemCopy,Source,Dest,ln   ; procedure from masm32 library

        cld
        mov rsi, [Source]
        mov rdi, [Dest]
        mov rcx, [ln]
        shr rcx, 2
        rep movsd
        mov rcx, [ln]
        and rcx, 3
        rep movsb
        ret
endp 

.data

pBitmap         FILE 'fish.bmp'
caption         db 'Bitmap from memory',0
class           db 'BitmapClass',0
mainhwnd        dq ?
hBitmap         dq ?
ppvBits         dq ?
ps PAINTSTRUCT
hdc dq ?
hMemDC dq ? 

    


there must be some small thing I missed or don't understand.

Thanks everybody
Post 10 Apr 2014, 04:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17450
Location: In your JS exploiting you and your system
revolution
A disassembler would show you that stdcall is not the same in 64-bit mode. It is actually fastcall with the first four values passed in by registers. So doing "mov rsi, [Source]" is not going to work here unless you spill the registers into the shadow space first.

Also watch your corruption of registers there, you are not following the specs by destroying RSI/ESI etc.
Post 10 Apr 2014, 05:08
View user's profile Send private message Visit poster's website Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog
I was able to get the program to work by removing the stdcall line and replacing it with this code
Code:
cld
         mov rsi, rax
        mov rdi, [ppvBits]
        mov rcx, 149346-54
        shr rcx, 2
        rep movsd
        mov rcx, 149346-54
        and rcx, 3
        rep movsb 
    


I would however like to figure out how to use fastcall for future programs. I read at [url] http://flatassembler.net/docs.php?article=win32#1.4 [/url] that fastcall uses rcx,rdx,r8,r9 so how to I fix the proc to have it work for 64 bit, so I know how to uses proc in other 64 bit programs

Also I wanted to know how did you get the info from a debugger? did use IDA or what other debugger.

sorry if these are newbie questions but I have never debugged my programs before.
thanks
Post 10 Apr 2014, 17:36
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17450
Location: In your JS exploiting you and your system
revolution
You can use fastcall like this:
Code:
proc MemCopy uses rsi rdi,Source,Dest,ln        ;<--- tell the assembler which registers you are corrupting
        ;spill
        mov     [Source],rcx            ;<--- fill the shadow space and free the register for use
        mov     [Dest],rdx              ;<--- fill the shadow space and free the register for use
        mov     [ln],r8                 ;<--- fill the shadow space and free the register for use
        ;use
        cld
        mov     rsi,[Source]
        mov     rdi,[Dest]
        mov     rcx,[ln]
        shr     rcx,2
        rep     movsd
        mov     rcx,[ln]
        and     rcx,3
        rep     movsb
        ret
endp    
Of course, this can be optimised better but I just wanted to show an example of how it works.

I didn't use a disassembler or a debugger on your code because you didn't post a complete example for me to assemble. But I was suggesting that you consider using one because such a useful tool should be part of your workflow.
Post 10 Apr 2014, 22:22
View user's profile Send private message Visit poster's website Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog
thank you revolution for explaining how to use procedures in 64 bit. this now explains why I could never get some other procedures working for 64 bit.
Post 11 Apr 2014, 00:51
View user's profile Send private message Reply with quote
patchariadog



Joined: 24 Mar 2013
Posts: 94
patchariadog
also thanks for pointing out i needed to add corruption to my registers for any procedure I wright. I did not know this.
Post 11 Apr 2014, 01:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17450
Location: In your JS exploiting you and your system
revolution
stdcall, fastcall, ccall, etc. all have specs that explain which registers are to be preserved and which can be freely altered.
Post 11 Apr 2014, 01:05
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: 17450
Location: In your JS exploiting you and your system
revolution
BTW: In 64-bit mode you have movsq:
Code:
        mov     rsi,[Source]
        mov     rdi,[Dest]
        mov     rcx,[ln]
        shr     rcx,3
        rep     movsq
        mov     rcx,[ln]
        and     rcx,7
        rep     movsb    
Post 11 Apr 2014, 07:47
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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.