News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Input

Started by HiddenDragon, December 12, 2010, 12:31:08 AM

Previous topic - Next topic

MichaelW

And here is a quick demonstration of why structures are generally passed by reference, instead of the individual elements being passed by value.

;==============================================================================
    include \masm32\include\masm32rt.inc
    .686
    include \masm32\macros\timers.asm
;==============================================================================
    .data
          wcx WNDCLASSEX <>
    .code
;==============================================================================
test0 proc pwcx:DWORD
    mov edx, pwcx
    mov eax, [edx].WNDCLASSEX.cbSize
    mov eax, [edx].WNDCLASSEX.style
    mov eax, [edx].WNDCLASSEX.lpfnWndProc
    mov eax, [edx].WNDCLASSEX.cbClsExtra
    mov eax, [edx].WNDCLASSEX.cbWndExtra
    mov eax, [edx].WNDCLASSEX.hInstance
    mov eax, [edx].WNDCLASSEX.hIcon
    mov eax, [edx].WNDCLASSEX.hCursor
    mov eax, [edx].WNDCLASSEX.hbrBackground
    mov eax, [edx].WNDCLASSEX.lpszMenuName
    mov eax, [edx].WNDCLASSEX.lpszClassName
    mov eax, [edx].WNDCLASSEX.hIconSm
    ret
test0 endp
;==============================================================================
test1 proc cbSize:DWORD,style:DWORD,lpfnWndProc:DWORD,cbClsExtra:DWORD,
           cbWndExtra:DWORD,hInstance:DWORD,hIcon:DWORD,hCursor:DWORD,
           hbrBackground:DWORD,lpszMenuName:DWORD,lpszClassName:DWORD,
           hIconSm:DWORD
    mov eax, cbSize
    mov eax, style
    mov eax, lpfnWndProc
    mov eax, cbClsExtra
    mov eax, cbWndExtra
    mov eax, hInstance
    mov eax, hIcon
    mov eax, hCursor
    mov eax, hbrBackground
    mov eax, lpszMenuName
    mov eax, lpszClassName
    mov eax, hIconSm
    ret
test1 endp
;==============================================================================
start:
;==============================================================================
    invoke Sleep, 3000

    counter_begin 10000, HIGH_PRIORITY_CLASS
        invoke test0, ADDR wcx
    counter_end
    print str$(eax)," cycles, by reference",13,10

    counter_begin 10000, HIGH_PRIORITY_CLASS
        invoke test1, wcx.cbSize,wcx.style,wcx.lpfnWndProc,wcx.cbClsExtra,
                      wcx.cbWndExtra,wcx.hInstance,wcx.hIcon,wcx.hCursor,
                      wcx.hbrBackground,wcx.lpszMenuName,wcx.lpszClassName,
                      wcx.hIconSm
    counter_end
    print str$(eax)," cycles, by value",13,10,13,10

    inkey "Press any key to exit..."
    exit

;==============================================================================
end start


Running on a P3:

13 cycles, by reference
31 cycles, by value


And note that the difference would be larger if the called procedures did not access all of the elements.

eschew obfuscation

HiddenDragon

Well in this:
push edi                                ;pointer to value through CreateStruct
    push esi                                ;handle instance
    push edi                                ;handle to menu or child
    push edi                                ;handle window parent
    push edi                                ;height in device units
    push ecx                                ;width in device units
    push edi                                ;initial vertical position
    push ecx                                ;initial horizontal position
    push WS_OVERLAPPEDWINDOW                ;style of window being created
    push ebx                                ;window name
    push ebx                                ;class name, must be created by RegisterClass function
    push edi                                ;extended style of the window
    call CreateWindowEx                     ; create the main window

The parts are pushed in reverse order, as they should be. However, in
; -----------------------------------
  ; manually coded WNDCLASSEX structure
  ; -----------------------------------
    mov DWORD PTR [ebp-96], 48                          ;size in bytes of the structure (48 bytes)

    mov DWORD PTR [ebp-92], CS_VREDRAW or CS_HREDRAW    ;window class style
                                                        ;vredraw redraws whole window if a movement or size adjustment
                                                        ;changes height of area
                                                        ;hredraw redraws if movement or size adjustment changes width

    mov DWORD PTR [ebp-88], OFFSET MyWndProc            ;pointer to window procedure

    mov DWORD PTR [ebp-84], edi                         ;number of bytes to allocate following window class structure
                                                        ;edi = 0 here

    mov DWORD PTR [ebp-80], edi                         ;number of bytes to allocate following window instance
                                                        ;edi = 0 here still

    mov DWORD PTR [ebp-76], esi                         ;400000h - handle instance that contains window procedure

    mov DWORD PTR [ebp-72], edi                         ;handle to class icon-must be a resource. 0 gives default icon
                                                        ;edi still = 0

    mov DWORD PTR [ebp-68], eax                         ;handle to class cursor-must be a resource. 0 means application
                                                        ;must explicitly set cursor shape whenever mouse moves into window

    mov DWORD PTR [ebp-64], COLOR_BTNFACE+1             ;handle to background brush-can be physical brush for painting or
                                                        ;can be color value

    mov DWORD PTR [ebp-60], edi                         ;specifies resource name of class menu. 0 means no default menu

    mov DWORD PTR [ebp-56], ebx                         ;specifies window class name
                                                        ;ebx contains the string in .data section

    mov DWORD PTR [ebp-52], edi                         ;handle to small icon associated with window class

The elements are moved in normal order which is backwards what it should be.

EDIT: Is this because the DWORDS for the WndClassEx structure are being moved, not pushed?

hutch--

No, its because you fail to understand the difference between a function and a structure. Windows function regularly use the VAX format of filling a structure then passing its ADDRESS to the function code.

Functions under STDCALL are pushed right to left where a structure is something like an array.

When you indicated that you did not want to use any high level constructs in MASM you were offered bare mnemonic code which is possible under MASM but the problem is you don't yet know enough to write that type of code.

Deriving theories about how this style of code works is a waste of members time.

What you need to do is learn some very basic things about x86 assembler, data SIZE to register SIZE, how function calls work under different calling conventions, differences between the address of a variable and its content, how and why data structures are written and some grasp of how the Windows API functions work.

If you can get this together it will make your questions a lot easier to answer and you will get off the ground much faster. When you know enough you will easily be able to write the lowest level of mnemonic code if you have some reason to do so.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

HiddenDragon

Alright, I'll start working with Iczelion's tutorials then hutch.