switch_x64_masm.asm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ;
  2. ; stack switching code for MASM on x641
  3. ; Kristjan Valur Jonsson, sept 2005
  4. ;
  5. ;prototypes for our calls
  6. slp_save_state_asm PROTO
  7. slp_restore_state_asm PROTO
  8. pushxmm MACRO reg
  9. sub rsp, 16
  10. .allocstack 16
  11. movaps [rsp], reg ; faster than movups, but we must be aligned
  12. ; .savexmm128 reg, offset (don't know what offset is, no documentation)
  13. ENDM
  14. popxmm MACRO reg
  15. movaps reg, [rsp] ; faster than movups, but we must be aligned
  16. add rsp, 16
  17. ENDM
  18. pushreg MACRO reg
  19. push reg
  20. .pushreg reg
  21. ENDM
  22. popreg MACRO reg
  23. pop reg
  24. ENDM
  25. .code
  26. slp_switch PROC FRAME
  27. ;realign stack to 16 bytes after return address push, makes the following faster
  28. sub rsp,8
  29. .allocstack 8
  30. pushxmm xmm15
  31. pushxmm xmm14
  32. pushxmm xmm13
  33. pushxmm xmm12
  34. pushxmm xmm11
  35. pushxmm xmm10
  36. pushxmm xmm9
  37. pushxmm xmm8
  38. pushxmm xmm7
  39. pushxmm xmm6
  40. pushreg r15
  41. pushreg r14
  42. pushreg r13
  43. pushreg r12
  44. pushreg rbp
  45. pushreg rbx
  46. pushreg rdi
  47. pushreg rsi
  48. sub rsp, 10h ;allocate the singlefunction argument (must be multiple of 16)
  49. .allocstack 10h
  50. .endprolog
  51. lea rcx, [rsp+10h] ;load stack base that we are saving
  52. call slp_save_state_asm ;pass stackpointer, return offset in eax
  53. cmp rax, 1
  54. je EXIT1
  55. cmp rax, -1
  56. je EXIT2
  57. ;actual stack switch:
  58. add rsp, rax
  59. call slp_restore_state_asm
  60. xor rax, rax ;return 0
  61. EXIT:
  62. add rsp, 10h
  63. popreg rsi
  64. popreg rdi
  65. popreg rbx
  66. popreg rbp
  67. popreg r12
  68. popreg r13
  69. popreg r14
  70. popreg r15
  71. popxmm xmm6
  72. popxmm xmm7
  73. popxmm xmm8
  74. popxmm xmm9
  75. popxmm xmm10
  76. popxmm xmm11
  77. popxmm xmm12
  78. popxmm xmm13
  79. popxmm xmm14
  80. popxmm xmm15
  81. add rsp, 8
  82. ret
  83. EXIT1:
  84. mov rax, 1
  85. jmp EXIT
  86. EXIT2:
  87. sar rax, 1
  88. jmp EXIT
  89. slp_switch ENDP
  90. END