flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
revolution 22 May 2020, 06:58
1. The idata section is to link to the system DLLs and allow the code to talk to the OS.
2. All calls to the system APIs return either their success/failure in RAX, or the returned value in RAX. In this case GetStdHandle return the handle in RAX. 3. Same reason, RAX is set by the previous call to WriteFile. |
|||
![]() |
|
Cerebrum 22 May 2020, 07:21
Why do we reserve 8*7 bytes?
Code: sub rsp,8*7 ; reserve stack for API use and make stack dqword aligned 2. Ok, the return code is in RAX, but why are we moving it to RCX? 3. Why are we using EAX (32 bit) here and not RAX? We are programming for 64 bit. |
|||
![]() |
|
revolution 22 May 2020, 07:29
1. The FASTCALL convention in 64-bit Windows requires the code to reserve stack space for the API calls.
2. RCX, RDX, R8 and R9 are the inputs to the following API call to WriteFile. This is also defined by the FASTCALL convention. 3. If we don't expect RAX to have anything but zeros in the high part then we can use the 32-bit registers instead. They get automatically zero extended by the CPU. |
|||
![]() |
|
Cerebrum 22 May 2020, 07:44
1. And why exactly 8*7 bytes?
|
|||
![]() |
|
revolution 22 May 2020, 07:52
Each stack slot is 8 bytes.
The first is to align the stack to a multiple of 16. A FASTCALL requirement. The next 6 are space for up to 6 parameters passed to the API calls. WriteFile needs 5 parameters, and we have to align to the next multiple of 16 by allocating 6 slots. |
|||
![]() |
|
Cerebrum 22 May 2020, 08:03
8*7 is not a multiple of 16. Or are you including the return address, in which case we have 8*8 in total?
But if the return address is part of it and WriteFile needs only 5 parameters, then 8*5 (+ return address) will also be a multiple of 16, no? |
|||
![]() |
|
revolution 22 May 2020, 08:06
When the program is started the stack is not aligned, so we have to use one stack slot to align it.
After that all allocations are in multiples of two slots to keep the stack aligned. So that makes 6+1 == 7 slots needed. If you only allocated 8*6 then the stack remains unaligned and you risk the program failing inside the API call due to misalignment. |
|||
![]() |
|
Cerebrum 22 May 2020, 08:09
How do you know that the stack is not aligned when the program starts?
|
|||
![]() |
|
revolution 22 May 2020, 08:12
Cerebrum wrote: How do you know that the stack is not aligned when the program starts? |
|||
![]() |
|
Cerebrum 22 May 2020, 08:15
Where can I find documentation on WriteFile(which parameters are expected where and what is returned)?
|
|||
![]() |
|
revolution 22 May 2020, 08:28
Cerebrum wrote: Where can I find documentation on WriteFile(which parameters are expected where and what is returned)? |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.