AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > 汇编语言

Windows下控制台输出

51自学网 http://www.51zixue.net

控制台输出就象dos下的输出,可不是图形界面。象ping/ipconfig/ftp等命令都是这类程序。

    回忆过去,在dos下进行文件操作时,常用到“文件把柄”的概念,使用文件把柄操作时,非常方便,操作时,只要知道把柄号就可以,而不用操心文件的位置。dos下,设备也都有自己的专用把柄,这些把柄是:
    0000H  标准输入设备 (stdin)
    0001H  标准输出设备 (stdout)

    0002H  标准错误设备 (stderr)
    0003H  标准辅助设备 (stdaux)
    0004H  标准打印设备 (stdprn)

    stdin和stdout可以被再定向,dos下常用的“输入改向”和“输出改向”就是这个意思。

    下面的dos功能调用中将向屏幕输出信息:
        mov ah,40h      ;写到文件或设备
        mov bx,1        ;标准输出设备
        lea dx,OutData  ;DS:DX->要输出的数据
        mov cx,Count    ;要输出字符的个数
        int 21h         ;执行dos功能调用
        
    利用同样的道理,在windows下,也可向屏幕输出信息。这要用到两个Api函数,一个是GetStdHandle,另一个是WriteFile,在Win32 developer's References中它们是这样定义的:
------------------------------------------------------------
HANDLE GetStdHandle(
    DWORD nStdHandle    // input, output, or error device
    );  

    The GetStdHandle function returns a handle for the standard input, standard output, or standard error device.

nStdHandle->Specifies the device for which to return the handle. This parameter can have one of the following values:
        Value               |   Meaning
        --------------------+---------------------------
        STD_INPUT_HANDLE    |   Standard input handle
        STD_OUTPUT_HANDLE   |   Standard output handle
        STD_ERROR_HANDLE    |   Standard error handle

If the function succeeds, the return value is a handle of the specified device.
------------------------------------------------------------
BOOL WriteFile(
    HANDLE hFile,       // handle to file to write to
    LPCVOID lpBuffer,   // pointer to data to write to file
    DWORD nNumberOfBytesToWrite,        // number of bytes to write
    LPDWORD lpNumberOfBytesWritten,     // pointer to number of bytes written
    LPOVERLAPPED lpOverlapped   // pointer to structure needed for overlapped I/O
    );  

=============================================================

;下面我们看一个程序,作用是显示一个字符串信息!

        .386
        .model flat,stdcall
        option casemap:none

include windows.inc

include kernel32.inc
include user32.inc

includelib kernel32.lib
includelib user32.lib

        .data
mess    db 'How are you !',0    ;要显示的信息

        .data?
StdOut  dd ?    ;存放标准输出的把柄
CharOut dd ?    ;记录实际输出的字符数

        .code
start:  
        invoke GetStdHandle,STD_OUTPUT_HANDLE   ;获取标准输出的把柄
        mov StdOut,eax      ;保存把柄号

        lea eax,mess
        invoke lstrlen,eax  ;求字符串的长度

        lea ecx,CharOut
        invoke WriteFile,StdOut,addr mess,eax,ecx,NULL  ;写文件
        
        invoke ExitProcess,NULL   ;程序结束
        end start
--------------------------------------------------------------
编译链接,下面给出详尽的信息,供分析参考:
D:/MASM7>dir /ad

Volume in drive D has no label
Volume Serial Number is 18F0-186B
Directory of D:/MASM7

.              〈DIR〉        02-12-03  17:36 .
..             〈DIR〉        02-12-03  17:36 ..
LIB            〈DIR〉       02-12-03  17:38 LIB
INCLUDE        〈DIR〉        02-12-03  17:38 INCLUDE
         0 file(s)              0 bytes
         4 dir(s)   2,411,925,504 bytes free

D:/MASM7>ml /coff /I include 4.asm /link /subsystem:console /libpath:lib
Microsoft (R) Macro Assembler Version 6.14.8444         └─ 控制台程序
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

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

/subsystem:console /libpath:lib
"4.obj"
"/OUT:4.exe"

D:/MASM7>4
How are you !
D:/MASM7>
--------------------------------------------------------------
另外,在masm32.inc中有函数StdOut的声明,可用来直接输出信息,把上面的例子修改后就是下面的样子,可见来得更简炼,供大家参考:

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

include windows.inc

include kernel32.inc
include masm32.inc

includelib kernel32.lib
includelib masm32.lib

        .data
mess    db 'How are you !',0    

        .code
start:  
        invoke StdOut,addr mess
        invoke ExitProcess,NULL
        end start

 

 

 
上一篇:小试Win32汇编  下一篇:Windows下程序的反汇编