flat assembler
Message board for the users of flat assembler.
Index
> Windows > How windows execute settimer ? Goto page Previous 1, 2, 3 Next |
Author |
|
MatQuasar3 28 Nov 2024, 05:42
sinsi wrote:
Thank you for pointing this out. I also noticed it the requirement of nonzero timer identifier, but there is exception where nIDEvent can be zero. |
|||
28 Nov 2024, 05:42 |
|
macomics 28 Nov 2024, 07:38
MatQuasar3 wrote: Is the "align 16" for faster performance? The point here is completely different. In the data section, I have arranged the structures and the corresponding variables according to the Windows rules. A variable of a certain size must be at an address multiple of this size. All fields in the structures and variables in my data section are arranged according to this rule. At the same time, they are ordered so that uninitialized variables are located at the end of the section and do not take up extra space in the file. To comply with this rule, align 16 has been added before uninitialized variables. If you are going to catch the WM_PAINT message, then you should definitely redraw the window region. Otherwise, the WM_PAINT priority is raised and the message queue will be blocked with it. You should not return mov eax, 1 unnecessarily. If you are not going to process some message, then you should invoke DefWindowProc and return its value to eax. |
|||
28 Nov 2024, 07:38 |
|
revolution 28 Nov 2024, 09:13
macomics wrote: A variable of a certain size must be at an address multiple of this size. There are other reasons to prefer to align non-stack values, but Windows on x86 doesn't require that. So you are free to unalign values in the data section if you want to. There will be no issue with correctness of the code, it will work. You might notice performance or efficiency reductions if you look for them and it is on some critical path or something, but otherwise it will be fine. |
|||
28 Nov 2024, 09:13 |
|
macomics 28 Nov 2024, 10:30
revolution wrote: There is an alignment requirement for the stack in 64-bit Windows. But other variables not passed on the stack don't have that requirement. I can't find a link to MSDN, but there was something like that there too. This is done in Windows to reduce cache line and memory page misses. So that the variable does not fall on the boundaries of the cache lines and page boundaries. In the stack, the flag is responsible for alignment, but they had an agreement with the data sections (it seems to have come from C) |
|||
28 Nov 2024, 10:30 |
|
revolution 28 Nov 2024, 10:40
macomics wrote:
|
|||
28 Nov 2024, 10:40 |
|
macomics 28 Nov 2024, 11:04
revolution wrote: That is to address potential performance and/or efficiency hazards. It doesn't affect the correctness of the code. I'm telling you, I can't find this article on MSDN. The following was written there (in my own words): each variable in the Windows operating system should be located at addresses multiple of its size; word at addresses multiple of 2, dword -- 4, qword -- 8 etc (this was said about all variables, not just the stack). Network, but there was an article about general variables. |
|||
28 Nov 2024, 11:04 |
|
revolution 28 Nov 2024, 11:10
macomics wrote: A variable of a certain size must be at an address multiple of this size. macomics wrote: ... each variable in the Windows operating system should be located at addresses multiple of its size ... I agree with "should". The "must" is not correct, there is no requirement. |
|||
28 Nov 2024, 11:10 |
|
MatQuasar3 28 Nov 2024, 12:43
macomics wrote:
Is it this one? https://learn.microsoft.com/en-us/cpp/c-language/alignment-c?view=msvc-170 Thank you revolution for making things clear that it isn't a requirement on x86, that's why I still stick to x86 (chuckle). In overall, I think macomics adherence to the alignment rule is a good practice. I may want to start thinking about aligning variable (already quite familiar with stack alignment) once I move up to x64 programming. |
|||
28 Nov 2024, 12:43 |
|
macomics 28 Nov 2024, 12:48
revolution wrote: Must != should. MatQuasar3 wrote: Is it this one? |
|||
28 Nov 2024, 12:48 |
|
sinsi 28 Nov 2024, 12:59
There are instructions, like MOVAPS, that require alignment.
Quote: When the source or destination operand is a memory operand, the operand must be aligned on a 16-byte boundary or a general-protection exception (#GP) will be generated There is also the AC flag which enforces all memory accesses to be aligned but Windows doesn't use it. |
|||
28 Nov 2024, 12:59 |
|
AsmGuru62 28 Nov 2024, 13:01
Interesting fact: you know the Window Class Name that needs to be put into WNDCLASS structure?
Recently, I was coding the x64 app and my UNICODE string for a class name was misaligned. The string address was not divisible by 2 --- I had declared something as DB before the class name. The application did not run until I corrected that. Weird! This was never mentioned in any help files. |
|||
28 Nov 2024, 13:01 |
|
revolution 28 Nov 2024, 13:08
So to summarise, for those with limited reading time.
Code: IF is_Windows_64_bit AND is_data_on_the_stack_for_an_api_call THEN
align your stack or suffer crashes
ELSE
do whatever you want, but prefer aligned over unaligned
ENDIF |
|||
28 Nov 2024, 13:08 |
|
revolution 28 Nov 2024, 13:13
sinsi wrote: There are instructions, like MOVAPS, that require alignment. sinsi wrote: There is also the AC flag which enforces all memory accesses to be aligned but Windows doesn't use it. |
|||
28 Nov 2024, 13:13 |
|
revolution 28 Nov 2024, 13:20
AsmGuru62 wrote: Interesting fact: you know the Window Class Name that needs to be put into WNDCLASS structure? |
|||
28 Nov 2024, 13:20 |
|
AsmGuru62 28 Nov 2024, 17:03
This is an issue I was talking about, revolution.
Code: section '.data' data readable writeable glb_InputBuffer dq 0 glb_OutputBuffer dq 0 bool_Running db 0 ; cause of misalignment txt_FrameClass du 'TFrameWndClass',0 ... lea rsi, [my_wnd_cls] ; local variable mov [rsi + WNDCLASS.lpszClassName], txt_FrameClass invoke RegisterClassW, rsi ; crashes inside Adding another boolean flag fixed it: Code: section '.data' data readable writeable glb_InputBuffer dq 0 glb_OutputBuffer dq 0 bool_Running db 0 ; cause of misalignment bool_Dummy db 0 ; fixed misalignment txt_FrameClass du 'TFrameWndClass',0 ... lea rsi, [my_wnd_cls] ; local variable mov [rsi + WNDCLASS.lpszClassName], txt_FrameClass invoke RegisterClassW, rsi ; no crash |
|||
28 Nov 2024, 17:03 |
|
revolution 28 Nov 2024, 17:14
Oh, you mean the wide string address must be aligned!
That is the first I have heard of that. |
|||
28 Nov 2024, 17:14 |
|
bitRAKE 28 Nov 2024, 21:46
Data use can cause all types of errors. Often one's thinking needs to go beyond just alignment: ownership, allocation pool, etc.
For example, BSTR's should be created through the ole functions, but if it's constant and ownership isn't given away then we can break that rule. Basically, we must know that called function will not try to manipulate the string - which is rare. Oddly, even some of the conversion functions change the source string - yikes! Easiest to just use the ole functions when using BSTR's. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
28 Nov 2024, 21:46 |
|
revolution 28 Nov 2024, 23:12
revolution wrote: ... the wide string address must be aligned! Even rep scasw, rep cmpsw, or rep movsw don't have any alignment requirements. Is the code deliberately using and rsi,not 1 to reset bit 0? I can't think of any other way to make it not work when unaligned. If it is then for what purpose? What problem is solved by doing that? |
|||
28 Nov 2024, 23:12 |
|
sinsi 28 Nov 2024, 23:34
I tested it, CreateWindowExW returns 0 and GetLastError returns
Quote: 0x000003E6 ERROR_NOACCESS Invalid access to memory location. edit: Sorry it was RegisterClassExW Last edited by sinsi on 28 Nov 2024, 23:57; edited 1 time in total |
|||
28 Nov 2024, 23:34 |
|
Goto page Previous 1, 2, 3 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.