flat assembler
Message board for the users of flat assembler.

Index > Windows > x64 calling convention (aligning rsp to 16)

Author
Thread Post new topic Reply to topic
Chewy509



Joined: 19 Jun 2003
Posts: 297
Location: Bris-vegas, Australia
Chewy509
Hi Guys,

With the excellent work by Feryno in working out the calling convention, I've come across a small stumbling block; the requirment to align rsp to 16.

The problem is, the majority of code I write is designed to be cross platform (linux and win64 console), and hence use a lot of code that abstracts the underlying OS in the form of API wrappers.

The main issue being how to make rsp aligned 16, if I'm not really paying attention to the current alignment of the stack...

However, in your opinion, would the following be viable to use in wrapping API code:

Code:
  push r15
  mov r15, 0fffffffffffffff0h
;... set up args to API
  mov [var_rsp], rsp
  and rsp, r15  ;; align to 16
  add rsp, (8*4)
  call [API_call]
  mov rsp, [var_rsp]
  pop r15
  ret    

where var_rsp is a local variable (which allows thread safe code).

Your thoughts, and other solutions would be greatly appreciated.
Post 31 Jul 2006, 03:33
View user's profile Send private message Visit poster's website Reply with quote
MazeGen



Joined: 06 Oct 2003
Posts: 977
Location: Czechoslovakia
MazeGen
Hi Chewy, this is how GoAsm works in win64. I'm implementing the same in MASM and I haven't found any better way.

(the numbers in the left column mean the order of instruction execution)

Code:
 Stack layouts for even and odd number of stack parameters:

 A. Total 6 integer parameters -> 2 stack parameters (uses AND method):

  a) a layout for inital 8-byte alignment (e.g., RSP = 88dec)

                                |      xyz      |       88
 1. PUSH RSP                    |      88       |       80
 2. PUSH [RSP]                  |      88       |       72 (used by POP RSP)
 3. AND RSP, NOT (16t-1)        |     hole      |       64
 5. MOV [RSP + 2*8 + 4*8], arg6 |     arg6      |       56
 6. MOV [RSP + 1*8 + 4*8], arg5 |     arg5      |       48
                                |    R9 home    |
                                |    R8 home    |
                                |    RDX home   |
 4. SUB RSP, 4*8 + 2*8          |    RCX home   |       16
 7. CALL func                   |  RETURN LINK  |       (8 at the entry of the function)
 8. ADD RSP, 4*8 + 2*8 + 8      |      88       |       72
 9. POP RSP                     |      xyz      |       88

  b) a layout for inital 16-byte alignment (e.g., RSP = 80dec)

                                |      xyz      |       80
 1. PUSH RSP                    |      80       |       72 (used by POP RSP)
 2. PUSH [RSP]                  |      80       |       64
 3. AND RSP, NOT (16t-1)        -   no change   -       64
 5. MOV [RSP + 2*8 + 4*8], arg6 |     arg6      |       56
 6. MOV [RSP + 1*8 + 4*8], arg5 |     arg5      |       48
                                |    R9 home    |
                                |    R8 home    |
                                |    RDX home   |
 4. SUB RSP, 4*8 + 2*8          |    RCX home   |       16
 7. CALL func                   |  RETURN LINK  |       (8 at the entry of the function)
 8. ADD RSP, 4*8 + 2*8 + 8      |      80       |       72
 9. POP RSP                     |      xyz      |       80

 B. Total 7 integer parameters -> 3 stack parameters (uses OR method):

  a) a layout for inital 8-byte alignment (e.g., RSP = 88dec)

                                |      xyz      |       88
 1. PUSH RSP                    |      88       |       80
 2. PUSH [RSP]                  |      88       |       72 (used by POP RSP)
 3. OR RSP, 16t/2               -   no change   -       72
 5. MOV [RSP + 3*8 + 4*8], arg7 |     arg7      |       64
 6. MOV [RSP + 2*8 + 4*8], arg6 |     arg6      |       56
 7. MOV [RSP + 1*8 + 4*8], arg5 |     arg5      |       48
                                |    R9 home    |
                                |    R8 home    |
                                |    RDX home   |
 4. SUB RSP, 4*8 + 3*8          |    RCX home   |       16
 8. CALL func                   |  RETURN LINK  |       (8 at the entry of the function)
 9. ADD RSP, 4*8 + 3*8          |      88       |       72
 10. POP RSP                    |      xyz      |       88

  b) a layout for inital 16-byte alignment (e.g., RSP = 80dec)

                                |      xyz      |       80
 1. PUSH RSP                    |      80       |       72 (used by POP RSP)
 2. PUSH [RSP]                  |      80       |       64
 3. OR RSP, 16t/2              - removes prev one -     72
 5. MOV [RSP + 3*8 + 4*8], arg7 |     arg6      |       64
 6. MOV [RSP + 2*8 + 4*8], arg6 |     arg6      |       56
 7. MOV [RSP + 1*8 + 4*8], arg5 |     arg5      |       48
                                |    R9 home    |
                                |    R8 home    |
                                |    RDX home   |
 4. SUB RSP, 4*8 + 3*8          |    RCX home   |       16
 8. CALL func                   |  RETURN LINK  |       (8 at the entry of the function)
 9. ADD RSP, 4*8 + 3*8          |      80       |       72
 10. POP RSP                    |      xyz      |       80
    
Post 31 Jul 2006, 07:39
View user's profile Send private message Visit poster's website Reply with quote
Chewy509



Joined: 19 Jun 2003
Posts: 297
Location: Bris-vegas, Australia
Chewy509
Thanks. Still wondering why MS came up with such a monsterousity...
Post 01 Aug 2006, 02:44
View user's profile Send private message Visit poster's website Reply with quote
MazeGen



Joined: 06 Oct 2003
Posts: 977
Location: Czechoslovakia
MazeGen
Me too. There was interesting discussion about it:

http://board.flatassembler.net/topic.php?t=4155#30448
Post 01 Aug 2006, 12:04
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.