Win08.ASM : File Mapping 


;Win08.ASM :  Open And Load Files Using File Mapping (May 10, 2024)
;===================================================================
;NOTES:
;   The " %include " paths at the start of the file might need to be
;       changed to suit your own disk directory setup.
;   Command Lines:
;       Assembly: nasm -f win32 win08.asm -o win08.obj
;       Linking : golink.exe /entry:start win08.obj win08.res kernel32.dll user32.dll
;
%include '\nasm32\inc\win32\windows.inc'
%include '\nasm32\inc\win32\kernel32.inc'
%include '\nasm32\inc\win32\user32.inc'
%include '\nasm32\inc\win32\gdi32.inc'
%include '\nasm32\inc\nasm32.inc'

[section .data]


;----------
;STRUCS
;----------
wcx times WNDCLASSEX_size db 0
msg times MSG_size db 0
rect times RECT_size db 0

;-----------------
;FILE HANDLING
;-----------------
AddrFile dd 0   ; Starting adr of file object in memory.
FileName db "Win08.dat",0
FileSizeHigh dd 0
FileSizeLow dd 0
hFileMap dd 0
hFileName dd 0  ; Filename handle


fBuf times OFSTRUCT_size db 0

;---------------
;HANDLES
;----------------
hInstance dd 0
hWnd dd 0
hMenu dd 0
hDC dd 0
hFont dd 0
hDefaultFont dd 0
;------------------
;MENU
;----------------
MenuName db "Win08_Menu",0x0
IDM_EXIT equ 102
IDM_LOADFILE equ 104
;-----------------------
;VARIOUS LABELS
;------------------------
szTitle db 'win08 : NASM Sample To Open, Load and Display a File Using File Mapping',0x0
szClass db 'win08 Class',0x0
ArialName db "ARIAL",0

[section .text]

start:          ; Entry point for GoLink.exe

proc win08
      invoke GetModuleHandleA, dword NULL
      mov [hInstance], eax
      invoke WinMain,dword [hInstance],dword NULL,dword NULL,dword SW_SHOWNORMAL
      invoke ExitProcess,dword NULL
      ret
endproc ;win08

proc WinMain ,hInst,hPinst,CmdLn,dwShow

;Memory block reserved for wcx [WNDCLASSEX] has no data yet. 

;A different method of populating memory slots.
push dword WNDCLASSEX_size
pop dword [wcx  + WNDCLASSEX.cbSize]

push dword argv(hInst)      
pop dword [wcx  + WNDCLASSEX.hInstance]

push dword szClass
pop dword [wcx  + WNDCLASSEX.lpszClassName]

push dword WndProc    ; Adr of WndProc
pop dword [wcx  + WNDCLASSEX.lpfnWndProc]

push dword COLOR_BTNFACE +1
pop dword [wcx  + WNDCLASSEX.hbrBackground]


push dword MenuName
pop dword  [wcx + WNDCLASSEX.lpszMenuName]

      invoke RegisterClassExA, wcx

;-----------
;Load Menu
;-----------
    invoke LoadMenuA,[hInstance],100   ; see win08.rc
    mov dword [hMenu],eax

;-----------
;Load Window
;-----------
    invoke CreateWindowExA,\
     WS_EX_OVERLAPPEDWINDOW,\
                    szClass,\
                    szTitle,\
          WS_OVERLAPPEDWINDOW +WS_VISIBLE ,\
            100, 120, 700, 350,\
                          NULL,\
                       dword [hMenu],\
     [wcx + WNDCLASSEX.hInstance],\
                       NULL

;
;EAX returns handle to window. Store it
;
     mov [hWnd], eax

;-------------------------
;Process Windows Messages
;-------------------------
MsgGet:
    invoke GetMessageA, dword msg, dword NULL, dword NULL, dword NULL
    cmp eax,0
        jnz MsgProcess
    jmp MsgEnd
MsgProcess:
      invoke TranslateMessage, dword msg
      invoke DispatchMessageA, dword msg
      jmp MsgGet
MsgEnd:
   ret
endproc ;WinMain

;===========================================================================
;-------------------------
;Windows Callback Function  
;-------------------------                              

proc WndProc, hWin, uMsg, wParam, lParam

Is_Key?:
    cmp dword argv(uMsg),dword WM_KEYDOWN
        jz MsgKey

Is_Command?:
    cmp dword argv(uMsg),WM_COMMAND
        jz MsgCommand

Is_Destroy?:
    cmp dword argv(uMsg),dword WM_DESTROY ;Window SYSMENU "x" click (or
        jz Destroy                      ;  ALT/F4 )?
    jmp DefWndProc
Destroy:
    jmp ExitWin08

;---------------
;KEY STRUCK
;-----------

MsgKey:
    cmp argv(wParam), dword VK_ESCAPE   ; ESC Key ?
        jnz NotESCKey
    jmp ExitWin08       ; ESC struck. Exit.
NotESCKey:
        jmp DefWndProc

;-------------
;MENU COMMAND
;-------------
MsgCommand:
       cmp dword argv(wParam),IDM_EXIT   ; Exit Application ?
			jnz MenuLoadFile?
       jmp ExitWin08
MenuLoadFile?:
       cmp dword argv(wParam),IDM_LOADFILE   ; Load File ?
			jz Win08FileOpenRequest      ;
       jmp DefWndProc

;-------------------------------
;REQUEST TO LOAD FILE
;------------------------
Win08FileOpenRequest:

	cmp dword [hFileName],0x0	;Don't reopen file again. See Win08.txt
		jz Win08FileLoad
    jmp DefWndProc

;-------------------------
;Menu request to load a file
;-------------------------
Win08FileLoad:

;Open the file

    invoke CreateFileA,\
            FileName,\
         GENERIC_READ,\
                    0,\
                    0,\
        OPEN_EXISTING,\
      FILE_ATTRIBUTE_NORMAL,\
                     0

        mov dword [hFileName],eax   ; Store the file handle

;Get file size

    invoke GetFileSize,dword [hFileName],FileSizeHigh
    mov dword [FileSizeLow],eax ;GetFileSize reports low 32 bits of
                                ; fileszize in EAX.

;Create a file mapping object

    invoke CreateFileMappingA,\
        dword [hFileName],\
                        0,\
           PAGE_READONLY ,\
                    0,\
                    0,\
                    0

        mov dword [hFileMap],eax    ; Store the file object handle

;Map a View of the object

   invoke MapViewOfFile,\
    dword [hFileMap],\
    FILE_MAP_READ,\
                0,\
                0,\
                0
    
	mov dword [AddrFile],eax        ; Store the starting memory address of
                                    ;  the file object.

;Now, we can treat the file as a single long string without loading/re-
;loading buffers and moving file pointers.

;--------------------------
;Display the file contents
;--------------------------
    invoke DispWin08Txt,dword [AddrFile], dword [FileSizeLow]

;-----------------------
;CLEAN UP
;----------------


;Unmap the File View
    invoke UnmapViewOfFile, dword [AddrFile]

;Close the Map Handle
    invoke CloseHandle,dword [hFileMap]

;Close the File Handle
    invoke CloseHandle, dword [hFileName]

DefWndProc:
    invoke DefWindowProcA, dword argv(hWin), dword argv(uMsg), dword argv(wParam), dword argv(lParam)
    ret

;------------------
;EXIT PROCECURE
;------------------
ExitWin08:
    invoke PostQuitMessage,dword NULL
    xor eax,eax
    ret

endproc     ;WndProc
;==============================================


;===========================================================================
;======================      PROCEDURES       ==============================
;===========================================================================

proc DispWin08Txt , addrObj,sizeFile

    invoke GetDC,dword [hWnd]
    mov dword [hDC],eax

	invoke GetSysColor,COLOR_BTNFACE
	invoke SetBkColor,dword [hDC],eax

    invoke CreateFontA,17,8,0,0,500,0,0,0,0,0,0,0,0,ArialName
    mov dword [hFont],eax

;Select the font into the DC
        invoke SelectObject, dword [hDC],dword [hFont]
		mov dword [hDefaultFont], eax


    mov eax, dword argv (addrObj)
    mov ecx, dword argv (sizeFile)
	
    mov dword [rect +RECT.left],20
   	mov dword [rect +RECT.top],10
    mov dword [rect +RECT.right],500
   	mov dword [rect +RECT.bottom],500


	invoke DrawTextA, dword [hDC],eax,ecx,rect,DT_WORDBREAK

	invoke SelectObject, dword [hDC],dword [hDefaultFont]
	invoke DeleteObject, dword [hFont]

    ret 8   ;two 32 bit params were invoked

endproc ; DispWin08Txt


;mcamember October 22, 2008
;mcamember October 14, 2022
;mcamember May 10, 2024