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 first program!

Started by felipe, April 26, 2017, 12:13:44 PM

Previous topic - Next topic

felipe

Finally i got it! My first program with the famous win32 (the apis)  :bgrin:
Of course the code it's so simple that i shame my self for posting, but i will if you want. :lol:
One question for you who know: The masm32rt.inc includes the directives for the memory model? (.model flat)
Thanks...
:redface:... :lol:

hutch--

felipe,

Post the example, we have all been there and perhaps we can help a bit.

jj2007

Quote from: felipe on April 26, 2017, 12:13:44 PMThe masm32rt.inc includes the directives for the memory model? (.model flat)

Go have a look! It's a plain text file, all editors can open it (don't save it, though).

felipe

Quote from: jj2007 on April 26, 2017, 04:27:07 PM
Quote from: felipe on April 26, 2017, 12:13:44 PMThe masm32rt.inc includes the directives for the memory model? (.model flat)

Go have a look! It's a plain text file, all editors can open it (don't save it, though).

You are right. I did it some day, but i will do it again. Well here is the simplest program:

.386
.model flat,stdcall
option casemap:none

include c:\masm32\include\masm32rt.inc

.stack  4096

.data
head    byte    'The first',0
inside  byte    'Hello!',0

.code
start:
main    proc
        call    push_parameters
        call    ExitProcess         ; Windows function.
main   endp

push_parameters proc       
        push    0                   ; Style of message box.
        mov     ebx,offset head
        push    ebx
        mov     ebx,offset inside
        push    ebx
        push    0                   ; No window, no handle?
        call    MessageBoxA         ; Windows function.
        ret
push_parameters endp
        end     start
       


I will later be online now i'm hurry, thanks for your interest.  :t

hutch--

felipe,


; .386
; .model flat,stdcall
; option casemap:none

include c:\masm32\include\masm32rt.inc  ; If you use this, you don't use the one before it.

.stack  4096  ; this is not used in 32 bit Windows. In 32 bit you set the stack if necessary in the linker options.

nidud

#5
deleted

hutch--

Here is a nice tidy MASM32 simple text output, does the same as a hello world but in Spanish.

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

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

    .data
      text db "Hola hombre",0   ; display text
      ptext dd text             ; pointer to display text
      crlf db 13,10,0           ; carriage return, line feed pair
      pcrlf dd crlf

    .code

start:                          ; start execution here
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

  ; -------------------------------------------
  ; StdOut is a procedure in the MASM32 library
  ; -------------------------------------------
    invoke StdOut,ptext         ; display message text
    invoke StdOut,pcrlf         ; add a crlf pair
    invoke StdOut,pcrlf         ; add a crlf pair

    inkey                       ; macro to wait for a key press to exit

  ; -------------------------------------
  ; ExitProcess is a Windows API function
  ; -------------------------------------
    invoke ExitProcess,0        ; exit the current process

main endp

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

end start                       ; tell MASM where the code end is

LordAdef


felipe

Ok, thanks a lot. I have two questions: 1) Is offset a directive from masm or a instruction mnemonic from intel?
and 2) Why i can't have the offset in a register with the lea instruction? I mean, there wasen't errors in the assembly process, but the program didn't run in windows (windows gave me an error using the lea for load a register with the desired offset).
:redface:

jj2007

#9
.data
text db "Hola hombre",0   ; display text
.code

start:                          ; start execution here
  mov eax, offset text
  lea edx, text     ; NOT: offset text (sorry for confusing you, Felipe!!)
...


Both instructions do the same, but lea is one byte longer. "offset" is not an opcode, but it is an instruction used by MASM and its clones (and maybe others, I don't know).

When addressing local variables, e.g. with
invoke MessageBox, 0, addr localtext, addr localtitle, MB_OK
lea is being used together with ebp.

To understand these things better,
- get Olly
- unzip it to \Masm32\OllyDbg
- open your program in RichMasm
- insert an int 3 after start:
- hit F6 to build and launch Olly
- hit F9 to arrive at the int 3
- hit F7 to continue step by step (same for F8, except that it will not go inside procs)

nidud

#10
deleted

felipe

But i coded:( lea ebx,memory_location) first, and the assembly was correct. But windows couldn't run the program, why?  :redface:

jj2007

Quote from: felipe on April 27, 2017, 04:14:39 AMBut windows couldn't run the program, why?  :redface:

Can't find my crystal ball right now, can you post the complete code please?

felipe

It was the same code of above man, but instead of the lines: (mov ebx,offset head) and (mov ebx,offset inside) was:
(lea ebx,head) and (lea ebx,inside).

It's the problem because i treated the instruction (lea) like in real mode? So like this instruction is used for dinamically addresing and the OS is in protected mode (moving the actual locations of the code) that dosen't work. So the correct instruction would be: (lea ebx, offset memory_location)?  :redface:

LordAdef

both below will work. You may have missed something else

push_parameters proc       
        push    0                   ; Style of message box.

        ;mov    ebx,offset head    ;<== this
        lea      ebx, head               ;<== or this

        push    ebx

        ;mov    ebx,offset inside
        lea      ebx, inside

        push    ebx
        push    0                   ; No window, no handle?
        call    MessageBoxA         ; Windows function.
        ret
push_parameters endp