Structures in NASM - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Structures in NASM

Hello fellow programmers,


I've been trying to switch from TASM to NASM.

There's one thing which still doesn't seem to work

properly in NASM.


I can't create structures. I've read the NASM documen-

tation and there was an example about how to use structures. I copied the example and compiled it, but that didn't work either.


Could anyone help me on this?


Thanks in advance,


Jaap.


Comments

  • I've had to do this for some windows coding in nasm.

    I'll use the WNDCLASS structure as an example.


    To start off, you need a structure definition, usually in a .inc file:



    struc WNDCLASS

    w_style: resd 1 ;class style

    w_lpfnWndProc: resd 1 ;window procedure

    w_cbClsExtra: resd 1 ;class extra data

    w_cbWndExtra: resd 1 ;window extra data

    w_hInstance: resd 1 ;class owner

    w_hIcon: resd 1 ;icon handle

    w_hCursor: resd 1 ;cursor handle

    w_hbrBackground: resd 1 ;background color

    w_lpszMenuName: resd 1 ;menu name

    w_lpszClassName: resd 1 ;class name

    endstruc


    note the starting and ending "struc/endstruc" tags, the use of unique names

    internal to the structure (w_hIcon rather than hIcon), and the use of

    "reserve" bytes (resb, resw, resd) rather than "define" bytes (db, dw, dd).


    After this, you can declare instances of the structure in the data (or code) segments of your program:



    WndClass:

    istruc WNDCLASS

    at w_style, dd 0 ;class style

    at w_lpfnWndProc, dd 0 ;window procedure

    at w_cbClsExtra, dd 0 ;class extra data

    at w_cbWndExtra, dd 0 ;window extra data

    at w_hInstance, dd 0 ;class owner

    at w_hIcon, dd 0 ;icon handle

    at w_hCursor, dd 0 ;cursor handle

    at w_hbrBackground, dd 0 ;background color

    at w_lpszMenuName, dd 0 ;menu name

    at w_lpszClassName, dd 0 ;class name

    iend


    note the label ("WndClass:"), such that structre members can be

    referred to as local labels (i.e., "WndClass.w_hIcon"), the use of

    the "istruc/iend" starting and ending tags, the use of the "at"

    keyword when defining bytes, and the "define" rather than "reserve"

    byte commands.




    ...dg


Sign In or Register to comment.