News:

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

Main Menu

Demo programs hang after compilation

Started by asmator, December 03, 2015, 01:40:57 AM

Previous topic - Next topic

TWell

Example using msvcrt.dll.386
.model flat, stdcall
option casemap :none

printf proto cdecl :vararg
exit proto cdecl :dword
includelib msvcrt.lib

.data
mystring db "Hello, world!",13,10,0

.code
main:
invoke printf, addr mystring
invoke exit, 0
end main

asmator

Quote from: jj2007 on December 03, 2015, 11:35:23 PM
The same source should never produce different code. All our assemblers (ML 6.14 ... 11.0, JWasm, AsmC) do exactly the same. If there is no stack frame, the source was different (see also OPTION PROLOGUE). That is why it is so weird...

This is then very strange, because what I compile is the simple hello.asm example (see below), but I can see the compiled code is longer that it should be, and running the file on IDA shows all sort of instructions I did not place there... but the code seems to do the same. I can only think of two sensible things:

1. Would this be related to what I said in my 1st post? i.e. that I have installed MASM32 over XP and then copied it to my Win8 computer.

2. OR I am making some stupid mistake. What I do is opening the .asm in qeditor and pressing "Project - Console Assemble & Link"   (I have also tried" Project - Console Build All" and "Project - Build All"). Perhaps I am not linking in the right way or I have not done any necessary steps after installation (I did not modify anything, I am using all default options).


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

;                 Build this with the "Project" menu using
;                       "Console Assemble and Link"

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                                    ; create 32 bit code
    .model flat, stdcall                    ; 32 bit memory model
    option casemap :none                    ; case sensitive

    include \masm32\include\windows.inc     ; always first
    include \masm32\macros\macros.asm       ; MASM support macros

  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
    include \masm32\include\masm32.inc
    include \masm32\include\gdi32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\gdi32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib

    .code                       ; Tell MASM where the code starts

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                          ; The CODE entry point to the program

    print chr$("Hey, this actually works.",13,10)
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start                       ; Tell MASM where the program ends


Result from the Console Assemble nd Link:


Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

Assembling: E:\masm32\tutorial\console\demo1\hello.asm

***********
ASCII build
***********

Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Volume in drive E is E
Volume Serial Number is BAD6-0CEF

Directory of E:\masm32\tutorial\console\demo1

07/02/04  00:59             1,776 hello.asm
03/12/15  16:13             2,560 hello.exe
03/12/15  16:13               669 hello.obj
               3 File(s)          5,005 bytes
               0 Dir(s)  166,597,718,016 bytes free
Press any key to continue . . .

dedndave

it shouldn't make any difference which OS you install on - the results should be the same

there are, however, differences in assemblers
in some cases, there are different forms of the same instruction (places where either form will work)
and - some improvements (hopefully) along the way

i suggest you place macros.asm at the end of the includes
(before or after includelibs - doesn't matter)

as for the build....
perhaps you have some other version of ML or LINK in the PATH
check the chain of batch files (qeditor menus reference batch files)
you might verify by building directly from the command line, rather than QE menus

asmator

Quote from: dedndave on December 04, 2015, 02:21:50 AM
i suggest you place macros.asm at the end of the includes
(before or after includelibs - doesn't matter)

as for the build....
perhaps you have some other version of ML or LINK in the PATH
check the chain of batch files (qeditor menus reference batch files)
you might verify by building directly from the command line, rather than QE menus

I tried to compile & link manually with the same result.

e:\masm32\tutorial\console\demo1>\masm32\bin\ml /c /coff hello.asm
e:\masm32\tutorial\console\demo1>\masm32\bin\Link /SUBSYSTEM:CONSOLE /OPT:NOREF hello.obj


The funny thing is that I did not mess anything. Just install MASM32 and compile the examples in the "tutorial" dir, which I suppose where compiled with the same ml and link I am using. What I am thinking is if the macros are being interpreted in some way that generates the code in the way it does. I imagine "print" will call:

invoke StdOut,expand_prefix(reparg(arg1))

Correct? Because in that case I think my approach should be to try to go one level deeper each time (using invoke -> using invoke without expand_prefix() and reparg() -> using CALL directly) to see what is generating the different instructions.

dedndave

i can't answer that specific question - never used that macro

but, let's try this...

Microsoft (R) Macro Assembler Version 6.15.8803
Copyright (C) Microsoft Corp 1981-2000.  All rights reserved.

Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.


dedndave

i am able to build that tutorial program with no problem
it runs in the console (and quickly disappears)

to see it, open a console window first, then type "hello" at the prompt

asmator

That is not a problem for me either, I am able to execute it when the AV is off. What is strange is that the compiler is producing a very different code (and much bigger to do the same thing) from the one pre-compiled one.

Can you please send me the .exe to verify the code?  (or tell me if the one you compiled is equal to the code in the .exe that was included in masm32)

Not sure this will have any impact, but can you please confirm if you are using MASM32 SDK Version 11 as I do?

hutch--

Default build in MASM32.


Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

Assembling: K:\masm32\tutorial\console\demo1\hello.asm

***********
ASCII build
***********

Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Volume in drive K is disk3_k
Volume Serial Number is F679-CB12

Directory of K:\masm32\tutorial\console\demo1

02/07/2004  10:59 AM             1,776 hello.asm
12/07/2015  09:45 PM             2,560 hello.exe
12/07/2015  09:45 PM               669 hello.obj
               3 File(s)          5,005 bytes
               0 Dir(s)  975,485,345,792 bytes free
Press any key to continue . . .


This is the total code in disassembly.


    start:
    push offset off_00403000
    call fn_00401014

    push 0
    call ExitProcess

    db 3 dup (0CCh)
; ----------------------------
fn_00401014:

    push ebp
    mov ebp, esp
    add esp, 0FFFFFFF4h
    push 0FFFFFFF5h
    call GetStdHandle

    mov [ebp-4], eax
    push DWORD PTR [ebp+8]
    call fn_00401050

    mov [ebp-0Ch], eax
    push 0
    lea eax, [ebp-8]
    push eax
    push DWORD PTR [ebp-0Ch]
    push DWORD PTR [ebp+8]
    push DWORD PTR [ebp-4]
    call WriteFile

    mov eax, [ebp-8]
    leave
    ret 4

    db 6 dup (0CCh)
; ----------------------------
fn_00401050:

    mov eax, [esp+4]
    lea edx, [eax+3]
    push ebp
    push edi
    mov ebp, 80808080h

  lbl0:
    mov edi, [eax]
    add eax, 4
    lea ecx, [edi-1010101h]
    not edi
    and ecx, edi
    and ecx, ebp
    jnz lbl1
    mov edi, [eax]
    add eax, 4
    lea ecx, [edi-1010101h]
    not edi
    and ecx, edi
    and ecx, ebp
    jnz lbl1
    mov edi, [eax]
    add eax, 4
    lea ecx, [edi-1010101h]
    not edi
    and ecx, edi
    and ecx, ebp
    jnz lbl1
    mov edi, [eax]
    add eax, 4
    lea ecx, [edi-1010101h]
    not edi
    and ecx, edi
    and ecx, ebp
    jz lbl0

  lbl1:
    test ecx, 8080h
    jnz lbl2
    shr ecx, 10h
    add eax, 2

  lbl2:
    shl cl, 1
    sbb eax, edx
    pop edi
    pop ebp
    ret 4

asmator

Thanks, that is reassuring... because this is the exact same output I get.

However, the code seems longer than it should be (I know that in this particular case it does not take more space in the file, tough). This is what jj2007 was mentioning here:

QuoteQuote from: jj2007 on December 03, 2015, 09:47:50 PM
One thing I see in diff_code.jpg is that "original" has a stack frame (push ebp ... leave) while "my version" doesn't have one.

Mmm... and how do I configure this behaviour? Is it something coming from "masm32 RTL" somehow?

The same source should never produce different code. All our assemblers (ML 6.14 ... 11.0, JWasm, AsmC) do exactly the same. If there is no stack frame, the source was different (see also OPTION PROLOGUE). That is why it is so weird...

In particular I see the produced code has this repeating block, not sure why:

mov edi, [eax]
    add eax, 4
    lea ecx, [edi-1010101h]
    not edi
    and ecx, edi
    and ecx, ebp
    jnz lbl1


And this does not really exist neither in the original .asm file (unless the macro is inserting such code) nor in the pre-compiled hello.exe that comes with MASM32 by default. And this "mysterious" code is what makes my AV complain. Not sure why MASM creates such code.

By the way, the generated 0xCC are created for alignment?

nidud

#24
deleted

asmator

Ok, it is perhaps worth it to leave the discussion here, concluding:

  • I suppose some of the macros are creating the extra code for some reason, even though it does not seem necessary.
  • The extra code is blocked by my AV or it makes my AV collapse.
  • The example .exe was compiled with another version of MASM32 (the macros it includes).