flat assembler
Message board for the users of flat assembler.

Index > Windows > Have I found a bug? Alignment problemswith wide-char string

Author
Thread Post new topic Reply to topic
fasm14



Joined: 23 Jan 2021
Posts: 14
Location: Russia
fasm14 06 May 2021, 05:58
So I was trying to add some Unicode text to my app but it wasn't working, I tried to find out what the issue was and I got this:
Code:
format PE GUI 4.0
entry start
include "win32a.inc"
include "encoding/utf8.inc"

section '.bss' data readable writeable
msg MSG
hwnd dd 0

ASCIIText db 0
UnicodeText du "It worked!",0
section '.rsrc' resource data readable writeable

directory RT_DIALOG, dialogs
resource dialogs,\
1,LANG_NEUTRAL, main
dialog main, 'Unicode Test', 100, 100, 150, 20, WS_VISIBLE+WS_CAPTION+WS_SYSMENU+DS_FIXEDSYS+WS_MINIMIZEBOX
dialogitem 'STATIC',"It didn't work...",2,3,7,50,8,WS_VISIBLE
enddialog

section '.idata' import data readable writeable
library kernel, 'KERNEL32.DLL',\
        user, 'USER32.DLL'

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

import user,\
CreateDialogParam, 'CreateDialogParamA',\
DestroyWindow, 'DestroyWindow',\
GetMessage, 'GetMessageA',\
SetDlgItemTextW, 'SetDlgItemTextW',\
IsDialogMessage, 'IsDialogMessageA'


section '.code' readable writeable executable

start:
invoke GetModuleHandle, 0
invoke CreateDialogParam,eax,1,0,DProc,0
cmp eax, 0
je DestroyDialog
mov [hwnd], eax
invoke SetDlgItemTextW,[hwnd],2, UnicodeText

StartLoop:
invoke GetMessage, msg, NULL, 0, 0
cmp eax, 1
jb DestroyDialog

invoke IsDialogMessage, [hwnd], msg
jmp StartLoop

proc DProc hwnddlg, msg, wparam, lparam
xor eax, eax
cmp [msg], WM_CLOSE
je DestroyDialog
ret

DestroyDialog:
invoke DestroyWindow, [hwnd]
invoke ExitProcess, 0
endp

    


For some reason, when 'ASCIIText db 0' is present before 'UnicodeText du "It worked!",0', the app wouldn't change the static element's text, but when I remove it, it works. If I put 'ASCIIText db 0' after 'UnicodeText du "It worked!",0', it works also. When ASCIIText looks something like 'ASCIIText db "a",0' it works as well. Am I not understanding something or is this a bug?
Post 06 May 2021, 05:58
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20526
Location: In your JS exploiting you and your system
revolution 06 May 2021, 06:25
Windows can be fussy with alignment for some functions.

Using "db 0" will make the following data unaligned.

BTW: Did you know that all Windows functions return an error code in EAX. You can check that value to see which function fails and what the problem was.
Post 06 May 2021, 06:25
View user's profile Send private message Visit poster's website Reply with quote
fasm14



Joined: 23 Jan 2021
Posts: 14
Location: Russia
fasm14 06 May 2021, 08:32
Thanks, but the same thing happens if I do 'db 1', 'db "a"', 'db 10', etc. The only time it didn't happen was with a short string terminated with a 0, but when I used a longer string it happened again... (the error code in EAX was just 1 ("ERROR_INVALID_FUNCTION") every time)
Post 06 May 2021, 08:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20526
Location: In your JS exploiting you and your system
revolution 06 May 2021, 13:30
It is the length of the "db" that matters. The value it not important.

If you have a single byte then all after are unaligned. The same for 3 bytes, or 5, or any odd number of bytes.
Post 06 May 2021, 13:30
View user's profile Send private message Visit poster's website Reply with quote
fasm14



Joined: 23 Jan 2021
Posts: 14
Location: Russia
fasm14 07 May 2021, 07:08
Thanks for explaining!
Post 07 May 2021, 07: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.