NASM - The Netwide Assembler

Please login or register.

Login with username, password and session length
Advanced search  


Pages: [1] 2 3 ... 10
 on: Today at 01:55:27 PM 
Started by coldflame - Last post by coldflame
Yeah I'm not so skilled in assembly language, just did some codes for my thesis, but they were made within x86 architecture. Now I need to work with x64 architecture so that's why I'm a little bit confused of this everything :D Thank you for your time (:

 on: Today at 01:02:59 PM 
Started by coldflame - Last post by dreamCoder
Yes, that's exactly my first reply regarding the "call" (that is, the SUB RSP,8 is required to re-align the stack)

Can't blame you for such confusions. I was once like you :D

 on: Today at 12:53:46 PM 
Started by coldflame - Last post by coldflame
That was just an example. Sorry for that. My filename is "okno1.asm" and "okno1.obj". And yes, I'm using GoLink with kernel32 and user 32 when linking.

I have found here: this:

The stack must be kept 16-byte aligned. Since the "call" instruction pushes an 8-byte return address, this means that every non-leaf function is going to adjust the stack by a value of the form 16n+8 in order to restore 16-byte alignment.

So it might be caused by call instruction if I got it right.

 on: Today at 12:38:24 PM 
Started by coldflame - Last post by dreamCoder
I found that using "call" as the file name will show nothing as the output. Try changing the file name to something else.

 on: Today at 12:24:28 PM 
Started by coldflame - Last post by dreamCoder
No universal solution for it. It depends on many things such as the linkers, compilers and how the OS allocates the stack for your code. You should start with a small use case / test code just to check for the stack alignment (for example, calling a MessageBox) at the start of any of your code. And of course there is the debugger.

My general strategy is to get the stack aligned at the start of the code and then maintain such alignment throughout the code. This is safe enough for my use. If u have other errors, then at least you know it didn't come from the stack misalignment.

Btw, I'm not sure how you used GoLink but I think it should be something like
Code: [Select]
golink call.obj user32.dll kernel32.dll

 on: Today at 12:05:47 PM 
Started by coldflame - Last post by coldflame
Thank you for the reply! However, I'm not sure about the functionality of it. I mean, should I ALWAYS "test" SUB RSP,8 and if it works, then I don't have to bother with it anymore? Because I get this error not only with WinAPI function.

In other words: if some code does not work, should I "test/add" SUB RSP,8 as something like "universal" solution and see if it works? Or is it needed to solve the alignment separately according to the function I call (let's say one of the WinAPI ones)?

Thank you!

 on: Today at 07:23:13 AM 
Started by coldflame - Last post by dreamCoder
To make it more clear;

1. It's not your code that needs stack alignment. It's the Win API functions such as MessageBox.
2. When a Win API function is called, it must see the current stack (Top Of Stack) be aligned to 16 byte boundary. Thus, to avoid bugs, scared programmers rarely touch the stack for other purposes or at least avoiding odd pushes when they have to play with the stack. They strive to maintain aligned stack at all times in their code.
3. So whatever you do, make sure the current TOS is aligned to 16-byte boundary prior to calling a WinAPI function.

The stack breakdown should be something like this;

Code: [Select]
sub rsp,8  ;align the stack
sub rsp,32  ;shadow space allocation

Although this seems tedious, but it is more readable and much more clearer to readers than
Code: [Select]
sub rsp,40

 on: Today at 07:11:24 AM 
Started by coldflame - Last post by dreamCoder
On entry to _start, the address of the stack is not aligned to 16.

When the OS allocates a stack for your code, it gives you a stack aligned to 16. But there's a prior "CALL" to _start that modifies the RSP a.k.a breaking the alignment - probably by the linkers. Thus you need to re-align it back again to at least SUB RSP,8

 on: February 26, 2017, 07:42:00 PM 
Started by coldflame - Last post by coldflame
I'm using NASM: nasm -fwin64 call.asm

And GoLink linker: golink call.obj

When I run the call.exe, it displays dialog window.

 on: February 26, 2017, 07:12:26 PM 
Started by coldflame - Last post by coldflame

It works with ANY 16 byte boundary +8 with ecx,0 before the ExitProcess call.

When I use rcx,0, it works ONLY with rsp,40.

When I use ecx,0, it works as mentioned above.

Pages: [1] 2 3 ... 10