Hi everyone, I'm learning assembly with the NASM compiler and ALINK (to accompany and debug my C++ apps). I've tried to piece meal something together with my prior win32 background and the wee bit of ASM I know and came up with the below example. It compiles and links but does not run. I'm not entirely sure what the problem is, I'm suspecting maybe my entry or misuse of jumps? Thanks for the help!



%INCLUDE "..\inc\win32n.inc"

EXTERN GetModuleHandleA
EXTERN LoadIconA
EXTERN LoadCursorA
EXTERN GetStockObject
EXTERN RegisterClassA
EXTERN CreateWindowExA
EXTERN ShowWindow
EXTERN UpdateWindow
EXTERN GetMessageA
EXTERN TranslateMessage
EXTERN DispatchMessageA
EXTERN DestroyWindow
EXTERN PostQuitMessage
EXTERN DefWindowProcA
EXTERN GetLastError
EXTERN ExitProcess
EXTERN MessageBoxA

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SECTION DATA USE32 CLASS=DATA
WindowClassStruc: ISTRUC WNDCLASS
AT WNDCLASS.style, DD CS_HREDRAW | CS_VREDRAW
AT WNDCLASS.lpfnWndProc, DD MainProc
AT WNDCLASS.cbClsExtra, DD 0
AT WNDCLASS.cbWndExtra, DD 0
AT WNDCLASS.hInstance, DD 0
AT WNDCLASS.hIcon, DD NULL
AT WNDCLASS.hCursor, DD NULL
AT WNDCLASS.hbrBackground, DD NULL
AT WNDCLASS.lpszMenuName, DD NULL
AT WNDCLASS.lpszClassName, DD ClassName
IEND

WindowMSG:
ISTRUC MSG
AT MSG.hwnd, DD 0
AT MSG.message, DD 0
AT MSG.wParam, DD 0
AT MSG.lParam, DD 0
AT MSG.time, DD 0
AT MSG.pt, DD 0
IEND

WindowTitle DB 'testing',0
ClassName DB 'asmprogthing',0
SECTION BSS USE32 CLASS=DATA
WindowHandle RESD 1
g_hInst RESD 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
SECTION CODE USE32 CLASS=CODE
global main
main:

PUSH LPCTSTR NULL
CALL [GetModuleHandleA]
MOV [WindowClassStruc+WNDCLASS.hInstance],EAX

PUSH LPCTSTR IDI_APPLICATION
PUSH HINSTANCE NULL
CALL [LoadIconA]
MOV [WindowClassStruc+WNDCLASS.hIcon],EAX

PUSH LPCTSTR IDC_ARROW
PUSH HINSTANCE NULL
CALL [LoadCursorA]
MOV [WindowClassStruc+WNDCLASS.hCursor],EAX

PUSH INTEGER WHITE_BRUSH
CALL [GetStockObject]
MOV [WindowClassStruc+WNDCLASS.hbrBackground],EAX

PUSH DWORD WindowClassStruc
CALL [RegisterClassA]
TEST EAX,EAX
JZ NEAR error

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PUSH LPCTSTR NULL
CALL [GetModuleHandleA]

PUSH LPVOID NULL
PUSH HINSTANCE EAX
PUSH HMENU NULL
PUSH HWND NULL
PUSH INTEGER CW_USEDEFAULT
PUSH INTEGER CW_USEDEFAULT
PUSH INTEGER CW_USEDEFAULT
PUSH INTEGER CW_USEDEFAULT
PUSH DWORD WS_OVERLAPPEDWINDOW
PUSH LPCTSTR WindowTitle
PUSH LPCTSTR ClassName
PUSH DWORD 0
CALL [CreateWindowExA]
TEST EAX,EAX
JZ NEAR error
MOV [WindowHandle],EAX

PUSH INTEGER SW_SHOW
PUSH HWND EAX
CALL [ShowWindow]
PUSH HWND [WindowHandle]
CALL [UpdateWindow]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
msgloop_:
PUSH UINT 0
PUSH UINT 0
PUSH HWND NULL
PUSH DWORD WindowMSG
CALL [GetMessageA]
TEST EAX,EAX
JZ quit
PUSH DWORD WindowMSG
CALL [TranslateMessage]
PUSH DWORD WindowMSG
CALL [DispatchMessageA]
JMP msgloop_
quit:
PUSH WPARAM [WindowMSG+MSG.wParam]
CALL [ExitProcess]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MainProc:
%DEFINE EBP_hWnd EBP+8 ; handle of window
%DEFINE EBP_Msg EBP+12 ; message
%DEFINE EBP_wParam EBP+16 ; first message parameter
%DEFINE EBP_lParam EBP+20 ; second message parameter

ENTER 0,0

CMP UINT [EBP_Msg],WM_CLOSE
JE CLS_

PUSH LPARAM [EBP_lParam]
PUSH WPARAM [EBP_wParam]
PUSH UINT [EBP_Msg]
PUSH HWND [EBP_hWnd]
CALL [DefWindowProcA]

LEAVE
RETN 16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
error:
CALL [GetLastError]
PUSH UINT EAX
CALL [ExitProcess]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
CLS_:
SUB EAX, EAX
LEAVE
RETN 16


Also, I've been able to find examples of windows apps for nasm using nagoa, but not plain nasm. Could someone explain to me what nagoa is and differences in syntax maybe? Thanks again. :)
Posted on 2004-03-20 10:57:23 by ben
hi ! :)


you can write all your nasm code like that always ! thats great , you can play around with that , and evean try to simplify , things .


then read this NAGOA tutorial :

http://194.65.3.199/win32asm/tutorial/nagoa-tutorial.html


nagoa is just a chain of NASM MACROS that speeds the code wrinting us MASM macros do , more a lot of equates and STRUC?s etc.. . and IF , END IF , PROC ENDPROC , Locals var?s etc... ONLY NASM MACROS .....



the macros are all at NAGOA.INC , you can look at them :)


its easy to understand them after read the:

http://194.65.3.199/win32asm/tutorial/nagoa-tutorial.html


and download the all NAGOA NASM WIN32ASM package .

NAGOA default is NASM + GORC + ALINK


but NAGOA can use ONLY NASM :)


to ouput very litle EXE or DLL ... ect..


after downloading all the package and after reading the above tutorial .

look at the examples projects that are in NAGOA NASM PACKAGE

you can also JOIN WIN32NASM group here , there are a lot of examples there .


http://groups.yahoo.com/group/win32-nasm-users/




ha with NAGOA you can always use the real NASM without any macros .


NAGOA does not use librarys for linking with ALINK , we call all API Functions
VIA IAT .



like this


in fact what this is doing is:

push arg_N
.
.
push arg_1
import FubctionOfDLL fmod.dll
extern FubctionOfDLL
CALL




so no need of LOADLIBRARY .... or librarys to link






example opening the OpenFileNameA dialog NASM + ALINK
---------------------------------------------------------------------------


bss use32 ; this section will no be in the exe :)

ofn:
.lStructSize dd 0
.hwndOwner dd 0
.hInstance dd 0
.lpstrFilter dd 0
.lpstrCustomFilter dd 0
.nMaxCustFilter dd 0
.nFilterIndex dd 0
.lpstrFile dd 0
.nMaxFile dd 0
.lpstrFileTitle dd 0
.nMaxFileTitle dd 0
.lpstrInitialDir dd 0
.lpstrTitle dd 0
.Flags dd 0
.nFileOffset dd 0
.nFileExtension dd 0
.lpstrDefExt dd 0
.lCustData dd 0
.lpfnHook dd 0
.lpTemplateName dd 0,0

ofn_size equ ofn.lpTemplateName - ofn





code use32 ; code section

..start:





mov dword , ofn_size
mov dword , 1020
push ebx ; going to use ebx so better save it :)
sub ebx , 120 ; making a bufer from ebx
mov , ebx
pop ebx ; restore ebx

call GetOpenFileNameA,ofn

call ExitProcess , 0






the same example another way :)
========================

bss use32
ofn db OPENFILEMNAME_size

code use32 ; code section

..start:





mov dword ,OPENFILEMNAME_size
mov dword , 1020
push ebx ; going to use ebx so better save it :)
sub ebx , 120 ; making a bufer from ebx
mov , ebx
pop ebx ; restore ebx

call GetOpenFileNameA,ofn

call ExitProcess , 0


this can seem bad but actualy can help you better to track you code :)



look here :)

mov dword ,eax
mov dword ,1
mov word ,32



we look ther and knows right way what is happening :)



another way of same example
=====================



code use32 ; code section

..start:


loccall MyOpenFile , 120 , 1020




proc MyOpenFile , Buffer , nMaxFile

stack ofn , OPENFILEMNAME_size

mov dword , OPENFILEMNAME_size
m2m dword ,
m2m ,
call GetOpenFileNameA,addr ofn

call ExitProcess,0

endproc MyOpenFile













here are the addr macro that makes

lea eax ,
push eax



the proc endproc macros makes automatition of ret 4*n arguments

push ebp
mov ebp,esp
and %define arg_1 ebp+8 etc...
etc..


endmacro

mov esp,ebp
pop ebp

ret 4* n arguments



the stack macro

%define ofn ebp-OPENFILEMNAME_size
sub esp,OPENFILEMNAME_size



as you can see the NASM syntax is the SAME .



look at this tutorial


http://194.65.3.199/win32asm/tutorial/nagoa-tutorial.html

join this group :

http://groups.yahoo.com/group/win32-nasm-users/


i hope i help you :) , i hope there is not to much errors here...
Posted on 2004-03-21 16:03:11 by Nguga
%include "\lab\vasm\inc\nagoa.inc"

; - - - - - - - - - - - - - - - - - - - - -
bss use32

menuhand dd 0
hInstance dd 0
CommandLine dd 0
hwnd dd 0
hdc dd 0
wtop dd 0
wleft dd 0
IDM_PICK1 equ 2
IDM_PICK2 equ 3
IDM_EXIT equ 1
IDM_ABOUT equ 4

; - - - - - - - - - - - - - - - - - - - - -
data use32

ClassName db "SimpleWinClass",0
AppName db "Popup Menu",0
pick1 db "Pick Item 1 ",0
pick1_size equ $ - pick1
pick2 db "Pick Item 2 ",0
pick2_size equ $ - pick2
about db "About",0
exit db "Exit",0
menu db "Right click for menu item",0
menu_size equ $-menu
MenuName db "TheMenu",0
wabout db "Win32asm NaGoA Nasm" ,NEXT_LINE,NEXT_LINE,NEXT_LINE,\
"Menu Example made :",NEXT_LINE,\
"by Nguga ",NEXT_LINE,NEXT_LINE,NEXT_LINE,\
"site :",NEXT_LINE,NEXT_LINE,\
"http://visual-assembler.pt.vu",0
aboutcap db "Menu Example",0

; - - - - - - - - - - - - - - - - - - - - -
code use32

..start:

call GetModuleHandle, NULL
mov ,eax
call GetCommandLine
loccall WinMain, ,NULL,eax, SW_SHOWDEFAULT
call ExitProcess,eax

proc WinMain,hInst,hPrevInstace,lpCmdLine,nCmdShow
stack wc,WNDCLASSEX_size
stack msg,MSG_size

mov dword ,WNDCLASSEX_size
mov dword , CS_HREDRAW | CS_VREDRAW
mov dword , WndProc
mov dword ,NULL
mov dword ,NULL
push dword
pop dword
mov dword ,COLOR_WINDOW+1
mov dword , MenuName
mov dword , ClassName
call LoadIcon,NULL,IDI_APPLICATION

mov ,eax
mov dword ,0
call LoadCursor,NULL,IDC_ARROW

mov ,eax
call RegisterClassEx, addr wc

.DoAndShow:

call CreateWindowEx,NULL,ClassName, AppName,\
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,\
300,300, NULL,NULL,,NULL

mov , eax
call ShowWindow, , SW_SHOWNORMAL
call UpdateWindow,

.WHILE_TRUE
call GetMessage, addr msg,NULL,0,0
test eax,eax
jz .ENDW
call TranslateMessage, addr msg
call DispatchMessage, addr msg
jmp .WHILE_TRUE
.ENDW
mov eax,
endproc WinMain
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
proc WndProc ,hWnd , uMsg , wParam , lParam
stack rect,RECT_size
stack ps,PAINTSTRUCT_size
stack wmenu,4

IF dword ,e,WM_DESTROY
call PostQuitMessage,NULL
ENDIF
IF dword ,e,WM_PAINT
call GetWindowRect,,addr rect
mov eax,
mov ,eax
mov eax,
mov ,eax
call BeginPaint,, addr ps
mov ,eax
call TextOut,,0,0,menu,menu_size -1
call EndPaint,, addr ps
ENDIF
IF dword ,e,WM_MOVE
call GetWindowRect,,addr rect
mov eax,
mov ,eax
mov eax,
mov ,eax
ENDIF
IF dword ,e,WM_COMMAND
IF dword ,e,IDM_PICK1
call GetDC,
mov ,eax
call SendMessage,,WM_ERASEBKGND,,0
call TextOut,,0,0, pick1,pick1_size -1
call EndPaint,, addr ps
ENDIF
IF dword ,e,IDM_PICK2
call GetDC,
mov ,eax
call SendMessage,,WM_ERASEBKGND,,0
call TextOut,,0,0, pick2,pick2_size -1
call EndPaint,, addr ps
ENDIF
IF dword ,e,IDM_ABOUT
call MessageBox,NULL,wabout,aboutcap,MB_OK
ENDIF
IF dword ,e,IDM_EXIT
call DestroyWindow,
ENDIF
ENDIF
IF dword ,e,WM_RBUTTONDOWN
call CreatePopupMenu
mov ,eax
call AppendMenu,,MF_STRING,IDM_PICK1,pick1
call AppendMenu,,MF_STRING,IDM_PICK2,pick2
call AppendMenu,,MF_STRING,IDM_ABOUT,about
call AppendMenu,,MF_STRING,IDM_EXIT,exit
mov ebx,
mov ecx,ebx
and ebx,0ffffh
shr ecx,16
add ebx,
add ecx,
add ecx,20
call TrackPopupMenu,,\
TPM_CENTERALIGN + TPM_LEFTBUTTON,ebx,ecx,0,,NULL
ENDIF
call DefWindowProc,,,,
endproc WndProc
Posted on 2004-03-21 16:12:03 by Nguga
I downloaded NAGOA and it is very nice, thanks so much for the resources and help.

And you're right, those only NASM examples are amazingly small! :grin:
Posted on 2004-03-22 18:17:10 by ben
thanks BEN ! :)
Posted on 2004-03-22 18:53:10 by Nguga