News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

My second program!

Started by felipe, April 27, 2017, 11:23:21 AM

Previous topic - Next topic

felipe

include c:\masm32\include\masm32rt.inc                                        ; A good starting point for beginers.

.data
newfile         byte    'c:\Users\Administrador\Desktop\Programacion\newone\hello.txt',0

.code
start:
main            proc
                call    make_the_file
                call    ExitProcess
main            endp

make_the_file   proc
                push    0                           ; No template file.
                push    FILE_ATTRIBUTE_NORMAL       ; Normal file
                push    CREATE_NEW                  ; Create a new file.
                push    0                           ; The file handle can't be inherited.
                push    0                           ; Will not be shared (In runtime).
                push    GENERIC_WRITE               ; For write only.
                push    offset newfile
                call    CreateFile                  ; Windows do the job.
ret
make_the_file   endp
                end     start



So what do you think? (Well it's of course very simple, just for the campus right?  ;)).

Another question please: How can i combine the attributes. For example GENERIC_READ and GENERIC_WRITE?

Thanks a lot!  :t

raymond

You simply 'or' them. Ex.: GENERIC_READ or GENERIC_WRITE
Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html

hutch--

Ray is right here, it is a very common technique in Windows code to have the option of combining styles and in notation form it uses the "or" operator to combine them. When you have long lists of styles which is common when constructing a window, you OR a list of styles together in your code. The assembler actually performs the combination to a single value and places it in the code at the right location.

Another very common technique in Windows is the use of structures and this is a bit different to how 16 bit real mode DOS worked. Most of the system based structures are already defined in the include files so when you have an API function that requires an address of a structure, you either allocate a LOCAL variable for the structure OR if you need to for scope reasons, you can allocate a structure in either the initialised DATA section or the uninitialised DATA? structure.

felipe

Thanks both.

Quote from: hutch-- on April 27, 2017, 02:53:29 PM

Most of the system based structures are already defined in the include files so when you have an API function that requires an address of a structure, you either allocate a LOCAL variable for the structure OR if you need to for scope reasons, you can allocate a structure in either the initialised DATA section or the uninitialised DATA? structure.

Questions: 1) That local variable would be a pointer to that defined structure in the include file?
                  2) Why do you mean for scope reasons?

hutch--

Hi felipe,

When you make a LOCAL structure, you are allocating the amount of stack space required for the content of the structure.

LOCAL rct :RECT    ; a rectangle structure : 4 x DWORD addresses

mov rct.left, 100
mov rct.top, 50
mov rct.right, 400
mov rct.bottom, 250


All 4 item in the structure are written to the stack.

Scope is an easy one, if you only need a variable within a procedure, you allocate it as a LOCAL variable. When you need GLOBAL scope you place the variable in either the .DATA or .DATA? section and it can be accessed from anywhere in the module that it is allocated in.

felipe

I understand. One question: Are this elements of the structure pushed in the stack in the same order of the mov instructions?

jj2007

Get Olly, please - you need a debugger.include \masm32\include\masm32rt.inc

.code
MyTest proc arg1, arg2, arg3, arg4
LOCAL rct :RECT    ; a rectangle structure : 4 x DWORD addresses
  nop
  m2m rct.left, arg1
  nop
  m2m rct.top, arg2
  nop
  m2m rct.right, arg3
  nop
  m2m rct.bottom, arg4
  nop
  ret
MyTest endp

start:
  int 3   ; press F9 in Olly, it will stop here; then continue with F7
  invoke MyTest, 11111111h, 22222222h, 33333333h, 44444444h
  exit
end start

hutch--

The disassembly is probably the clearest way to see it.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    call teststruct
    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

teststruct proc

    LOCAL rct   :RECT

    mov rct.left, 100
    mov rct.top, 50
    mov rct.right, 400
    mov rct.bottom, 250

    ret

teststruct endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start

comment * -----------------------------------------------------------------

0040102B                    fn_0040102B:
0040102B 55                     push    ebp
0040102C 8BEC                   mov     ebp,esp                 ; set up the stack frame

0040102E 83C4F0                 add     esp,0FFFFFFF0h          ; allocate 16 bytes of stack space
                                                                ; this can also be done as sub esp, 16

                              ; ----------------------------------------
                              ; write each value to the structure member
                              ; ----------------------------------------

00401031 C745F064000000         mov     dword ptr [ebp-10h],64h
00401038 C745F432000000         mov     dword ptr [ebp-0Ch],32h
0040103F C745F890010000         mov     dword ptr [ebp-8],190h
00401046 C745FCFA000000         mov     dword ptr [ebp-4],0FAh

0040104D C9                     leave                           ; clean up the stack frame
0040104E C3                     ret

* -------------------------------------------------------------------------


Importantly you can see that the 4 struct members are written in memory order so if you need to you can assume that one follows the other in memory.

felipe

I have olly as a separate installation, i would like to have it integrated with masm (but i was thinking that masm32 comes with his own debuger). I want to learn use olly really well, but i haven't yet started that task.

I did understand how the stack looks like with those items on it. And in wich order (as you say, like in the normal memory order). Of course ebp it's the right register to use when you want to access the stack in that way (from down to top).

Thanks a lot, to both.  :t

jj2007

Quote from: felipe on April 29, 2017, 12:32:29 AMI have olly as a separate installation, i would like to have it integrated with masm

See Integrated debugging. If you need symbols, too, PM me for the necessary settings.

felipe

jj2007 i appreciate your interest, but i don't want to change qeditor for richmasm. Thanks anyway.  :icon14:


jj2007

No problem :biggrin:

You could add a batch file to qEditor's menus, and launch Olly from that file.

LordAdef

hey Felipe,

You don't need to change it. you could use both as I do. They are very compatible with each other.
Just my 2 cents

ps: JJ, please don't forget to send me the check  ::)

felipe

Quote from: jj2007 on April 29, 2017, 02:08:36 AM
No problem :biggrin:

You could add a batch file to qEditor's menus, and launch Olly from that file.

Thanks a lot man, i'm gonna prove that!  :biggrin:

jj2007

Quote from: LordAdef on April 29, 2017, 02:13:43 AMps: JJ, please don't forget to send me the check  ::)

Damnit, in this forum I can't delete your posts :eusa_snooty: