myC32.mac 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ; NASM macro set to make interfacing to 32-bit programs easier
  2. ; Also cool little macros to make NASM emulate some MASM things.
  3. ;
  4. ; Originally included in NASM. Modifications by Peter Johnson, 1999.
  5. ;
  6. %imacro proc 1 ; begin a procedure definition
  7. %push proc
  8. global %1
  9. %1: push ebp
  10. mov ebp, esp
  11. %assign %$arg 8
  12. ;%assign %$argnum 0
  13. %define %$procname %1
  14. %endmacro
  15. %imacro arg 0-1 4 ; used with the argument name as a label
  16. %00 equ %$arg
  17. ;%assign %$argnum %$argnum+1
  18. ;.arg%$argnum equ %1
  19. %assign %$arg %1+%$arg
  20. %endmacro
  21. %imacro endproc 0
  22. %ifnctx proc
  23. %error Mismatched `endproc'/`proc'
  24. %else
  25. ; mov esp, ebp
  26. ; pop ebp
  27. %ifdef LEGACY_ENDPROC
  28. ret
  29. %endif
  30. ;__end_%$procname: ; useful for calculating function size
  31. ; global %{$procname}_arglen
  32. ;%{$procname}_arglen equ %$arg-8
  33. ;%assign %$i 1
  34. ;%rep %$argnum
  35. ; global %{$procname}_arg%$i
  36. ;%{$procname}_arg%$i equ %{$procname}.arg%$i
  37. ;%assign %$i %$i+1
  38. ;%endrep
  39. %pop
  40. %endif
  41. %endmacro
  42. ; redefine ret instructions for in-proc cases
  43. %imacro ret 0-1
  44. %ifnctx proc
  45. ret %1
  46. %else
  47. mov esp, ebp
  48. pop ebp
  49. ret %1
  50. %endif
  51. %endmacro
  52. %imacro retf 0-1
  53. %ifnctx proc
  54. retf %1
  55. %else
  56. mov esp, ebp
  57. pop ebp
  58. retf %1
  59. %endif
  60. %endmacro
  61. %imacro retn 0-1
  62. %ifnctx proc
  63. retn %1
  64. %else
  65. mov esp, ebp
  66. pop ebp
  67. retn %1
  68. %endif
  69. %endmacro
  70. ; invoke calls a C function
  71. ; defaults to word (16 bit) size parameters
  72. %macro invoke 1-*
  73. %rotate -1
  74. ;%define invoketype word
  75. %rep (%0-1)
  76. ; %ifidni %1,dword
  77. ; %define invoketype dword
  78. ; %elifidni %1,word
  79. ; %define invoketype word
  80. ; %elifidni %1,byte
  81. ; %define invoketype byte
  82. ; %else
  83. %ifidni %1, cs
  84. o16 push %1
  85. %elifidni %1, ds
  86. o16 push %1
  87. %elifidni %1, es
  88. o16 push %1
  89. %elifidni %1, fs
  90. o16 push %1
  91. %elifidni %1, gs
  92. o16 push %1
  93. %elifidni %1, word cs
  94. o16 push %1
  95. %elifidni %1, word ds
  96. o16 push %1
  97. %elifidni %1, word es
  98. o16 push %1
  99. %elifidni %1, word fs
  100. o16 push %1
  101. %elifidni %1, word gs
  102. o16 push %1
  103. %else
  104. push %1
  105. %endif
  106. ; %endif
  107. %rotate -1
  108. %endrep
  109. call %1
  110. %if (%0!=1)
  111. add esp, byte %{1}_arglen
  112. %endif
  113. %endmacro