Hello, im pretty new to ASM so im not really capable to fix my own code yet. Thats why im asking here, im hoping someone can tell me exactly what im doing wrong. So i can learn from your experience.
I can write the rwally basic program that displays a console window and where you can just type in it.

.MODEL SMALL
.CODE
MOV AH, 08H
INT 21H
MOV DL, AL
MOV AH, 02H
INT 21H
MOV AH, 4CH
INT 21H
END

Just to show you how 'far' i am. As you can see i just started out.
But i do know C++, so i thought im gonna write a simple app that just brings up the console window.
I wrote this small code.


#include <windows.h>

void main()
{
char buf[4];
buf[0] = 'c';
buf[1] = 'm';
buf[2] = 'd';
buf[3] = '\0';

WinExec(buf,SW_SHOW);

exit(1);
}


In MSVC++ you can disassemble you own code, so i did that and i got this output.

push        ebp
mov        ebp,esp
sub        esp,44h
push        ebx
push        esi
push        edi
lea        edi,
mov        ecx,11h
mov        eax,0CCCCCCCCh
rep stos    dword ptr

mov        byte ptr ,63h
mov        byte ptr ,6Dh
mov        byte ptr ,64h
mov        byte ptr ,0

mov        esi,esp
push        5
lea        eax,
push        eax
call        dword ptr [KERNEL32_NULL_THUNK_DATA (004241f8)]
cmp        esi,esp
call        __chkesp (00401070)

push        1
call        exit (00401a30)


Okay, im not that good yet so this doesnt make much sens for me yet. But i tried to compile this code with a MASM compiler. But i gave an error for everyline of code.
All i added was this line at the top:

.MODEL SMALL
.CODE

and under that the ASM code i got from the disassembler.

This is the error i got most:
error A2005: instruction or register not accepted in current CPU mode.

Can someone just tell me why im getting this errors, and why  it isnt possible to use that ASM code and all.
Thanks for any help.
Posted on 2005-09-29 10:25:49 by vivendi
Hello,

The first snipplet is actually code for dos and not console. Your c/c++ code produce asm code for the console, and hence the difference.

I would propose you try using hutch's marcos if you are really keen on playing with console. (Could be found at masmforum.com if I am not wrong.)

Regards,
Victor
Posted on 2005-09-29 11:03:27 by roticv

Hello,

The first snipplet is actually code for dos and not console. Your c/c++ code produce asm code for the console, and hence the difference.

I would propose you try using hutch's marcos if you are really keen on playing with console. (Could be found at masmforum.com if I am not wrong.)

Regards,
Victor


I know the difference between the ASM snippet i posted and the c++ code.
But i was wondering why the disassembled ASM code doesnt work.
It uses WinExec to open the cmd window, atleast thats what suppose to be happening iwth the (disassembled) ASM part.
btw, thanks for the link, ill take a look at it later. Im also reading a book about the basics of ASM.
Posted on 2005-09-29 11:16:41 by vivendi
Well, you're only copying a small fragment of the program, not the whole of it, so even if it assembled it would crash. Also MASM needs different directives for Windows programming (a different memory model, for example).

You can look up the first of Iczelion's tutorials for a "skeleton" asm program in Windows.
http://spiff.tripnet.se/~iczelion/tutorials.html

And the section of this message board dedicated to Iczelion's tutorials:
http://www.asmcommunity.net/board/index.php?board=17.0

Hope that helps! ;)
Posted on 2005-09-29 12:00:29 by QvasiModo
vivendi,

Here is your C program in Win32 style MASM just to give you a start. I would recommend you forget about DOS based MASM unless you really have a need to write DOS programs. Go through the tutorials QvasiModo mentioned, they are all about writing Win32 programs in MASM and they are good.

.386
.model flat,stdcall
option casemap:none

include c:\masm32\include\windows.inc
include c:\masm32\include\kernel32.inc
include c:\masm32\include\user32.inc
includelib c:\masm32\lib\kernel32.lib
includelib c:\masm32\lib\user32.lib

main PROTO

.DATA

    buf BYTE 4 dup(0)

.CODE

start:

    call main
    invoke ExitProcess, 0    ; exit is from the C run-time library, use
                             ; this Win32 API function instead. You can
                             ; use the C run-time library from MASM, but
                             ; this is simpler.
main PROC

    mov buf[0], 'c'
    mov buf[1], 'm'
    mov buf[2], 'd'
    mov buf[3], 0
   
    ; Call WinExec the manual way
    ;
    ;push SW_SHOW
    ;lea  eax, buf
    ;push eax
    ;call WinExec
   
    ; Or you can use invoke
    ;
    invoke WinExec, ADDR buf, SW_SHOW
   
    ret
   
main ENDP

end start


Posted on 2005-10-02 23:37:25 by Greg
WinExec should run an application. "cmd" is not a valid path to application. try to use ShellExecute or CreateProcess, where omit application name and use "cmd" as commandline. And even better get COMSPEC system variable value - it can be a command.com on win9x etc.
Posted on 2005-10-03 03:34:45 by Shoo
...And:

WinExec

The WinExec function runs the specified application.

Note  This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.
Posted on 2005-10-03 13:24:17 by ti_mo_n
Shoo,

"cmd" is not a valid path to application.


It is cmd.exe, which is in %SYSTEMROOT%\system32 and is in the default path.

The idea was to duplicate his C program in MASM. I agree, he should use the newer API functions.

Posted on 2005-10-03 14:23:16 by Greg
WinExec is fine to use. It's only a wrapper on CreateProcess, and Microsoft is not likely to drop support for it in 32-bit platforms since a lot of apps would break.
Posted on 2005-10-03 14:24:28 by QvasiModo
i know about cmd.exe and was wrong winexec need exactly full pathname :) but still think it is wrong way to run command interpreter which place can vary on os or even user (admin :)) - it is better to use comspec environment variable to get it's path. i made a toy - you can play with it:
---
this even better
Attachments:
Posted on 2005-10-04 02:14:37 by Shoo
Shoo,

My goal was to duplicate his C program in MASM so he could see how the same program is written in MASM.

I agree with you, it is better to use the comspec environment variable to get it's path. I guess I should have pointed that out in my example. Sheesh :roll:

Posted on 2005-10-04 13:58:50 by Greg
It depends on how much control you need when you start an external application. ShellExcute works fine, WinExec() works fine but if you need extra information, CreateProcess() can do more so its very much a case of use what will do the job.

Regards,

hutch at movsd dot com
Posted on 2005-10-04 18:01:34 by hutch--