2

I have the following source for an assembly program that I got in a Youtube video tutorial:

.386
.model flat, stdcall
option casemap:none

include c:\masm32\include\windows.inc
include c:\masm32\include\masm32.inc
include c:\masm32\include\kernel32.inc

includelib c:\masm32\lib\masm32.lib
includelib c:\masm32\lib\kernel32.lib

.data
message1 db "Type your name: ", 0
message2 db "Your name is ", 0

.data?
buffer db 100 dup(?)

.code
start:

invoke StdOut, addr message1
invoke StdIn, addr buffer, 100
invoke StdOut, addr message2
invoke StdOut, addr buffer

invoke StdIn, addr buffer, 100
invoke ExitProcess, 0

end start

I compile the program with a bat file

ml /c /coff %1.asm
Link /SUBSYSTEM:WINDOWS %1.OBJ

I call the bat file assemble.bat so I call assemble source and it assembles the executable.

The problem is that when I run the program (the program assembles fine with no errors) the program simply does nothing. I call it in the console prompt and it simply does nothing, the program just shows a blank line and goes back to the command prompt as if nothing happened.

In the video tutorial the guy assembled his program and compiled and worked fine, but for me nothing happens.

Victor
  • 1,655
  • 9
  • 26
  • 38

3 Answers3

1

I solved the problem.

It was not working cause I was linking with the command "Link /SUBSYSTEM:WINDOWS %1.OBJ" For console applications the linking command should be "Link /SUBSYSTEM:CONSOLE %1.OBJ".

Victor
  • 1,655
  • 9
  • 26
  • 38
0

Add right after the MODEL flat statement:

includelib  \masm32\lib\kernel32.lib   ;fixed the problem!
JustBaron
  • 2,319
  • 7
  • 25
  • 37
0

At least normally StdIn and StdOut will be the handles to the standard input and output. To read/write, you'd need to invoke functions like ReadFile and WriteFile, passing StdIn or StdOut as parameters designating the file to read/write respectively.

Edit: here's a short example:

.386
.MODEL flat, stdcall

getstdout = -11

WriteFile PROTO NEAR32 stdcall, \
        handle:dword,           \
        buffer:ptr byte,        \
        bytes:dword,            \
        written: ptr dword,     \
        overlapped: ptr byte

GetStdHandle PROTO NEAR32, device:dword

ExitProcess PROTO NEAR32, exitcode:dword

.stack 8192

.data
message db "Hello World!"
msg_size equ $ - offset message

.data?
written  dd ?

.code
main proc   
    invoke GetStdHandle, getstdout
    invoke WriteFile,                   \
           eax,                         \
           offset message,              \
           msg_size,                    \
           offset written,              \
           0
    invoke ExitProcess, 0
main endp
        end main
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • but I don't want to read or write to files. It is intended to get and show data in the console. – Victor Jun 14 '11 at 21:53
  • @Victor: The handles to the console still work like file handles. You can *also* pass them to special console functions (e.g., `ReadConsoleInput`, `WriteConsoleOutput`), but since you just want to display simple text and don't care about things like changing the text color, `ReadFile`/`WriteFile` are easier. – Jerry Coffin Jun 14 '11 at 21:56
  • The problem here is that the code should be working. It's the same code as in the video tutorial, and it should be running. Are you saying that if I use the ReadConsoleInput and WriteConsoleOutput the program will run as expected? If it is, how do I use this functions? The program assembles fine but when I run it nothing happens. – Victor Jun 14 '11 at 21:59
  • I got the following errors when assembling your program: writefile.OBJ : error LNK2001: unresolved external symbol _WriteFile@20 writefile.OBJ : error LNK2001: unresolved external symbol _GetStdHandle@4 writefile.OBJ : error LNK2001: unresolved external symbol _ExitProcess@4 writefile.exe : fatal error LNK1120: 3 unresolved externals – Victor Jun 14 '11 at 22:05
  • @Victor: you need to link with kernel32.lib – Jerry Coffin Jun 14 '11 at 23:17
  • @Jerry Coffin: How do I link with kernel32.lib? – Victor Jun 15 '11 at 00:43
  • Just include it on the command line, something like: `ml whatever.asm kernel32.lib` – Jerry Coffin Jun 15 '11 at 01:04