Win32 Nasm

Posted on April 17, 2010, 12:05 am UTC by Iago Sousa (about 1 year ago)

Code (highlighted for ASM)

  1. ; NASM: nasmw -f obj nasmpad.asm
  2. ; ALINK: alink -c -oPE -subsys gui nasmpad win32.lib
  3.  
  4. extern MessageBoxA
  5. import MessageBoxA user32.dll
  6. extern CreateWindowExA
  7. import CreateWindowExA user32.dll
  8. extern RegisterClassExA
  9. import RegisterClassExA user32.dll
  10. extern GetModuleHandleA
  11. import GetModuleHandleA kernel32.dll
  12. extern LoadIconA
  13. import LoadIconA user32.dll
  14. extern LoadCursorA
  15. import LoadCursorA user32.dll
  16. extern ShowWindow
  17. import ShowWindow user32.dll
  18. extern UpdateWindow
  19. import UpdateWindow user32.dll
  20. extern GetMessageA
  21. import GetMessageA user32.dll
  22. extern TranslateMessage
  23. import TranslateMessage user32.dll
  24. extern DispatchMessageA
  25. import DispatchMessageA user32.dll
  26. extern ExitProcess
  27. import ExitProcess kernel32.dll
  28. extern DefWindowProcA
  29. import DefWindowProcA user32.dll
  30. extern PostQuitMessage
  31. import PostQuitMessage user32.dll
  32. extern PostMessageA
  33. import PostMessageA user32.dll
  34.  
  35. struc WNDCLASSEX
  36. .cbSize        resd 1
  37. .style         resd 1
  38. .lpfnWndProc   resd 1
  39. .cbClsExtra    resd 1
  40. .cbWndExtra    resd 1
  41. .hInstance     resd 1
  42. .hIcon         resd 1
  43. .hCursor       resd 1
  44. .hbrBackground resd 1
  45. .lpszMenuName  resd 1
  46. .lpszClassName resd 1
  47. .hIconSm       resd 1
  48. endstruc
  49.  
  50. struc POINT
  51. .x resd 1
  52. .y resd 1
  53. endstruc
  54.  
  55. struc MSG
  56. .hwnd    resd 1
  57. .message resd 1
  58. .wParam  resd 1
  59. .lParam  resd 1
  60. .time    resd 1
  61. .pt      resb POINT_size
  62. endstruc
  63.  
  64. segment .data use32
  65. CS_VREDRAW equ 1h
  66. CS_HREDRAW equ 2h
  67. WinClass:
  68. istruc WNDCLASSEX
  69. at WNDCLASSEX.cbSize,        dd  WNDCLASSEX_size
  70. at WNDCLASSEX.style,         dd  CS_VREDRAW + CS_HREDRAW
  71. at WNDCLASSEX.lpfnWndProc,   dd  Windowprocedure
  72. at WNDCLASSEX.cbClsExtra,    dd  0
  73. at WNDCLASSEX.cbWndExtra,    dd  0
  74. at WNDCLASSEX.hInstance,     dd  0
  75. at WNDCLASSEX.hIcon,         dd  0
  76. at WNDCLASSEX.hCursor,       dd  0
  77. at WNDCLASSEX.hbrBackground, dd  COLOR_BACKGROUND
  78. at WNDCLASSEX.lpszMenuName,  dd  0
  79. at WNDCLASSEX.lpszClassName, dd  szClassName
  80. at WNDCLASSEX.hIconSm,       dd  0
  81. iend
  82. WinMSG:
  83. istruc MSG
  84. at MSG.hwnd,    dd 0
  85. at MSG.message, dd 0
  86. at MSG.wParam,  dd 0
  87. at MSG.lParam,  dd 0
  88. at MSG.time,    dd 0
  89. at MSG.pt,      dd 0
  90. iend
  91. Handle                 dd 0
  92. Icon                   dd 0
  93. Cursor                 dd 0
  94. szClassName            db "nasmpad_0001"
  95. szTitle                db "Nasmpad"
  96. IDI_APPLICATION       equ 32512
  97. IDC_ARROW             equ IDI_APPLICATION
  98. CW_USEDEFAULT         equ 80000000h
  99. WS_EX_WINDOWEDGE       equ 00000100h
  100. WS_EX_CLIENTEDGE       equ 00000200h
  101. WS_OVERLAPPED         equ 0h
  102. WS_CAPTION             equ 0C00000h
  103. WS_SYSMENU             equ 80000h
  104. WS_THICKFRAME         equ 40000h
  105. WS_MINIMIZEBOX         equ 20000h
  106. WS_MAXIMIZEBOX         equ 10000h
  107. WS_EX_OVERLAPPEDWINDOW equ WS_EX_WINDOWEDGE|WS_EX_CLIENTEDGE
  108. WS_OVERLAPPEDWINDOW   equ WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX
  109. SW_SHOWNORMAL         equ 1
  110. WM_DESTROY             equ 2h
  111. COLOR_BACKGROUND       equ 1
  112. segment .code use32
  113. global ..start
  114. ..start:
  115.  
  116. push dword 0
  117. call [GetModuleHandleA]
  118. mov [WinClass+WNDCLASSEX.hInstance], eax
  119.  
  120. push dword IDI_APPLICATION
  121. push dword 0
  122. call [LoadIconA]
  123.  
  124. mov [WinClass+WNDCLASSEX.hIcon],eax
  125.  
  126. push dword IDC_ARROW
  127. push dword 0
  128. call [LoadCursorA]
  129.  
  130. mov [WinClass+WNDCLASSEX.hCursor],eax
  131.  
  132. push dword WinClass
  133. call [RegisterClassExA]
  134.  
  135. push dword 0
  136. push dword [WinClass+WNDCLASSEX.hInstance]
  137. push dword 0
  138. push dword 0
  139. push dword 480
  140. push dword 640
  141. push dword CW_USEDEFAULT
  142. push dword CW_USEDEFAULT
  143. push dword WS_OVERLAPPEDWINDOW
  144. push dword szTitle
  145. push dword szClassName
  146. push dword WS_EX_OVERLAPPEDWINDOW
  147. call [CreateWindowExA]
  148.  
  149. mov [Handle], eax
  150.  
  151. push dword SW_SHOWNORMAL
  152. push dword [Handle]
  153. call [ShowWindow]
  154.  
  155. push dword [Handle]
  156. call [UpdateWindow]
  157.  
  158. mov [Handle], eax
  159.  
  160. MsgLoop:
  161.  
  162. push dword 0
  163. push dword 0
  164. push dword 0
  165. push dword WinMSG
  166. call [GetMessageA]
  167.  
  168. push dword WinMSG
  169. call [TranslateMessage]
  170.  
  171. push dword WinMSG
  172. call [DispatchMessageA]
  173.  
  174. jmp MsgLoop
  175.  
  176. Windowprocedure:
  177.  
  178. %define hWnd ebp+8
  179. %define Msg ebp+0ch
  180. %define wParam ebp+10h
  181. %define lParam ebp+14h
  182.  
  183. push ebp
  184. mov ebp,esp
  185.  
  186. cmp dword [Msg],WM_DESTROY
  187. je Finish
  188.  
  189. .DefMsgHandler:
  190.  
  191. push dword [lParam]
  192. push dword [wParam]
  193. push dword [Msg]
  194. push dword [hWnd]
  195. call [DefWindowProcA]
  196.  
  197. .Exit:
  198.  
  199. mov esp, ebp
  200. pop ebp
  201. ret
  202.  
  203. Finish:
  204.  
  205. push dword 0
  206. call [ExitProcess]
  207.  
  208.  
  209. jmp Windowprocedure.DefMsgHandler