2 Replies - 554 Views - Last Post: 15 April 2018 - 12:14 AM

#1 sunshine33   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 15-September 17

Why is programming a windows application in NASM , MASM so different ?

Posted 14 April 2018 - 08:17 PM

in NASM

%include '..\..\windemos.inc'

cdXPos      EQU  128
cdYPos      EQU  128
cdXSize     EQU  320
cdYSize     EQU  200
cdColFondo  EQU  COLOR_BTNFACE + 1
cdVIcono    EQU  IDI_APPLICATION
cdVCursor   EQU  IDC_ARROW
cdVBarTipo  EQU  NULL
cdVBtnTipo  EQU  WS_VISIBLE+WS_OVERLAPPEDWINDOW

entry Entrada

[section .text]

  proc   WndProc, ptrdiff_t hwnd, dword msg, size_t wparam, size_t lparam
      locals none
      cmp      dword [argv(.msg)], WM_DESTROY
      je       .wmdestroy
      .wmdefault:
          invoke   DefWindowProc, ptrdiff_t [argv(.hwnd)], dword [argv(.msg)], size_t [argv(.wparam)], size_t [argv(.lparam)]
          jmp      .finish
      .wmdestroy:
          invoke    DestroyWindow, ptrdiff_t [argv(.hwnd)]
          invoke   PostQuitMessage, NULL
      .finish:
  endproc

  proc   WinMain, ptrdiff_t hinst, ptrdiff_t hpinst, ptrdiff_t cmdln, dword dwshow
  locals none

      ; invoke GetStockObject, LTGRAY_BRUSH
      ; mov    ptrdiff_t [wc + WNDCLASSEX.hbrBackground], __AX
      invoke LoadIcon, NULL, IDI_APPLICATION
      mov    __DX, __AX
      mov    __AX, ptrdiff_t [argv(.hinst)]
      mov    __BX, ptrdiff_t NombreClase
      mov    __CX, ptrdiff_t WndProc
      mov    ptrdiff_t [wc + WNDCLASSEX.hInstance], __AX
      mov    ptrdiff_t [wc + WNDCLASSEX.lpszClassName], __BX
      mov    ptrdiff_t [wc + WNDCLASSEX.lpfnWndProc], __CX
      mov    ptrdiff_t [wc + WNDCLASSEX.hIcon], __DX
      mov    ptrdiff_t [wc + WNDCLASSEX.hIconSm], __DX
      invoke LoadCursorA, NULL, IDC_ARROW
      mov    dword [wc + WNDCLASSEX.hCursor], eax
      invoke RegisterClassEx, wc

      invoke CreateWindowEx, cdVBarTipo, NombreClase, MsgCabecera, \
                  cdVBtnTipo,\
                  cdXPos, cdYPos, cdXSize, cdYSize, NULL, NULL, \
                  ptrdiff_t [wc + WNDCLASSEX.hInstance], NULL
      mov    size_t[hWnd], __AX
      invoke ShowWindow, [hWnd], dword[argv(.dwshow)]
      invoke UpdateWindow, [hWnd]

  .msgloop:
      invoke GetMessage, message, NULL, NULL, NULL
      cmp    eax, dword 0
      je     .exit
      invoke TranslateMessage, message
      invoke DispatchMessage, message
      jmp    .msgloop

  .exit:

      mov __AX, size_t[message + MSG.wParam]

  endproc

  proc    Entrada, ptrdiff_t argcount, ptrdiff_t cmdline
      locals none
      invoke GetModuleHandle, NULL
      mov    ptrdiff_t [hInstance], __AX
      invoke WinMain, hInstance, NULL, NULL, SW_SHOWNORMAL
      invoke ExitProcess, __AX
  endproc


[section .bss]
  hInstance:   reserve(ptrdiff_t) 1
  hWnd:        reserve(ptrdiff_t) 1

[section .data]
    MsgCabecera: declare(NASMX_TCHAR) NASMX_TEXT("Hola mundo en ventana Windows"), 0x0
    NombreClase: declare(NASMX_TCHAR) NASMX_TEXT("SimpleWinClass"), 0x0
    MsgError:    declare(NASMX_TCHAR) NASMX_TEXT("Carga inicial fallida."),0x0

    NASMX_ISTRUC wc, WNDCLASSEX
        NASMX_AT cbSize,        sizeof(WNDCLASSEX)
        NASMX_AT style,         CS_VREDRAW + CS_HREDRAW
        NASMX_AT lpfnWndProc,   NULL
        NASMX_AT cbClsExtra,    NULL
        NASMX_AT cbWndExtra,    NULL
        NASMX_AT hInstance,     NULL
        NASMX_AT hIcon,         NULL
        NASMX_AT hCursor,       NULL
        NASMX_AT hbrBackground, COLOR_BTNFACE + 1
        NASMX_AT lpszMenuName,  NULL
        NASMX_AT lpszClassName, NULL
        NASMX_AT hIconSm,       NULL
    NASMX_IENDSTRUC

    NASMX_ISTRUC message, MSG
        NASMX_AT hwnd,     NULL
        NASMX_AT message,  NULL
        NASMX_AT wParam,   NULL
        NASMX_AT lParam,   NULL
        NASMX_AT time,     NULL
        NASMX_ISTRUC pt, POINT
            NASMX_AT x,       NULL
            NASMX_AT y,       NULL
        NASMX_IENDSTRUC
    NASMX_IENDSTRUC


in MASM

; #########################################################################

	.386
	.model flat, stdcall
	option casemap :none   

; #########################################################################

	include windows.inc
	include user32.inc
	include kernel32.inc

	includelib user32.lib
	includelib kernel32.lib

; #########################################################################

	szText macro name, text:vararg
		local 	lbl
		jmp	lbl
		name 	db text, 0
		lbl:
	endm

	WinMain proto :dword, :dword, :dword, :dword
	WndProc proto :dword, :dword, :dword, :dword

; #########################################################################

	.data

		hInstance 		dd ?
		lpszCmdLine		dd ?

; #########################################################################

	.code

start:

	invoke 	GetModuleHandle, NULL
	mov	hInstance, eax

	invoke	GetCommandLine
	mov	lpszCmdLine, eax

	invoke 	WinMain, hInstance, NULL, lpszCmdLine, SW_SHOWDEFAULT
	invoke	ExitProcess, eax


; ------------------------------------------------------------------------
; WinMain
;
; Main program execution entry point
; ------------------------------------------------------------------------
WinMain proc 	hInst 		:dword,
		hPrevInst 	:dword,
		szCmdLine 	:dword,
		nShowCmd 	:dword

	local 	wc 	:WNDCLASSEX
	local 	msg 	:MSG
	local 	hWnd 	:HWND

	szText	szClassName, "BasicWindow"
	szText	szWindowTitle, "First Window"

	mov	wc.cbSize, sizeof WNDCLASSEX
	mov	wc.style, CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
	mov 	wc.lpfnWndProc, WndProc
	mov 	wc.cbClsExtra, NULL
	mov	wc.cbWndExtra, NULL

	push	hInst
	pop 	wc.hInstance

	mov	wc.hbrBackground, COLOR_BTNFACE + 1
	mov	wc.lpszMenuName, NULL
	mov 	wc.lpszClassName, offset szClassName

	invoke	LoadIcon, hInst, IDI_APPLICATION
	mov	wc.hIcon, eax
	mov	wc.hIconSm, eax

	invoke	LoadCursor, hInst, IDC_ARROW
	mov	wc.hCursor, eax

	invoke	RegisterClassEx, addr wc

	invoke	CreateWindowEx, WS_EX_APPWINDOW, addr szClassName, addr szWindowTitle,
				WS_OVERLAPPEDWINDOW,
				CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
				NULL, NULL, hInst, NULL

	mov	hWnd, eax

	invoke	ShowWindow, hWnd, nShowCmd
	invoke	UpdateWindow, hWnd

MessagePump:

	invoke 	GetMessage, addr msg, NULL, 0, 0

	cmp 	eax, 0
	je 	MessagePumpEnd

	invoke	TranslateMessage, addr msg
	invoke	DispatchMessage, addr msg

	jmp 	MessagePump

MessagePumpEnd:

	mov	eax, msg.wParam
	ret

WinMain endp


; ------------------------------------------------------------------------
; WndProc
;
; Handles all of the messages sent to the window
; ------------------------------------------------------------------------
WndProc proc 	hWin 	:dword,
		uMsg 	:dword,
		wParam 	:dword,
		lParam 	:dword

	.if uMsg == WM_DESTROY

		invoke 	PostQuitMessage, 0

		xor	eax, eax
		ret

	.endif

	invoke	DefWindowProc, hWin, uMsg, wParam, lParam

	ret

WndProc endp

end start





Not sure which one to use to learn ?

Is This A Good Question/Topic? 0
  • +

Replies To: Why is programming a windows application in NASM , MASM so different ?

#2 Programmer2004   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 96
  • Joined: 25-October 17

Re: Why is programming a windows application in NASM , MASM so different ?

Posted 14 April 2018 - 11:50 PM

It's different because every assembler has different or partially different syntax. For example when setting bit mode to 64, in FASM you'll write:
use64
But in NASM it'll be:
[bits 64]


Quote

Not sure which one to use to learn ?

Learn the one which looks easier or more comfortable for you.
Was This Post Helpful? 0
  • +
  • -

#3 sunshine33   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 129
  • Joined: 15-September 17

Re: Why is programming a windows application in NASM , MASM so different ?

Posted 15 April 2018 - 12:14 AM

Thanks ,

Then i think i should stay with MASM , Looks a bit more easier

; #########################################################################

	.386
	.model flat, stdcall
	option casemap :none   

; #########################################################################

	include windows.inc
	include user32.inc
	include kernel32.inc

	includelib user32.lib
	includelib kernel32.lib

; #########################################################################

	szText macro name, text:vararg
		local 	lbl
		jmp	lbl
		name 	db text, 0
		lbl:
	endm

	WinMain proto :dword, :dword, :dword, :dword
	WndProc proto :dword, :dword, :dword, :dword

; #########################################################################

	.data

		hInstance 		dd ?
		lpszCmdLine		dd ?

; #########################################################################

	.code

start:

	invoke 	GetModuleHandle, NULL
	mov	hInstance, eax

	invoke	GetCommandLine
	mov	lpszCmdLine, eax

	invoke 	WinMain, hInstance, NULL, lpszCmdLine, SW_SHOWDEFAULT
	invoke	ExitProcess, eax


; ------------------------------------------------------------------------
; WinMain
;
; Main program execution entry point
; ------------------------------------------------------------------------
WinMain proc 	hInst 		:dword,
		hPrevInst 	:dword,
		szCmdLine 	:dword,
		nShowCmd 	:dword

	local 	wc 	:WNDCLASSEX
	local 	msg 	:MSG
	local 	hWnd 	:HWND

	szText	szClassName, "BasicWindow"
	szText	szWindowTitle, "First Window"

	mov	wc.cbSize, sizeof WNDCLASSEX
	mov	wc.style, CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
	mov 	wc.lpfnWndProc, WndProc
	mov 	wc.cbClsExtra, NULL
	mov	wc.cbWndExtra, NULL

	push	hInst
	pop 	wc.hInstance

	mov	wc.hbrBackground, COLOR_BTNFACE + 1
	mov	wc.lpszMenuName, NULL
	mov 	wc.lpszClassName, offset szClassName

	invoke	LoadIcon, hInst, IDI_APPLICATION
	mov	wc.hIcon, eax
	mov	wc.hIconSm, eax

	invoke	LoadCursor, hInst, IDC_ARROW
	mov	wc.hCursor, eax

	invoke	RegisterClassEx, addr wc

	invoke	CreateWindowEx, WS_EX_APPWINDOW, addr szClassName, addr szWindowTitle,
				WS_OVERLAPPEDWINDOW,
				CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
				NULL, NULL, hInst, NULL

	mov	hWnd, eax

	invoke	ShowWindow, hWnd, nShowCmd
	invoke	UpdateWindow, hWnd

MessagePump:

	invoke 	GetMessage, addr msg, NULL, 0, 0

	cmp 	eax, 0
	je 	MessagePumpEnd

	invoke	TranslateMessage, addr msg
	invoke	DispatchMessage, addr msg

	jmp 	MessagePump

MessagePumpEnd:

	mov	eax, msg.wParam
	ret

WinMain endp


; ------------------------------------------------------------------------
; WndProc
;
; Handles all of the messages sent to the window
; ------------------------------------------------------------------------
WndProc proc 	hWin 	:dword,
		uMsg 	:dword,
		wParam 	:dword,
		lParam 	:dword

	.if uMsg == WM_DESTROY

		invoke 	PostQuitMessage, 0

		xor	eax, eax
		ret

	.endif

	invoke	DefWindowProc, hWin, uMsg, wParam, lParam

	ret

WndProc endp

end start





Was This Post Helpful? 0
  • +
  • -

Page 1 of 1