载入中…

您现在的位置: 四联自学考试网 >> 网络学院 >> 程序设计 >> 汇编语言 >> 技巧教程 >> 学院正文
相 关 文 章
菜鸟入门:VB编程规约
Visual Basic 安装程序的制…
.NET和VB编程创建SQL Serve…
用VB实现任意修改Win桌面背…
Visual Basic串口通信程序设…
多用户应用程序中应注意问题…
多用户应用程序中应注意问题
怎样在VB中声明和使用API函…
让VB使用彩色及动画光标的方…
VB操作.ini后缀文件的方法
精 彩 推 荐
最 新 热 门
最 新 推 荐
欢迎光临四联自学,现在是: 祝您自考成功!
Windows下控制台输出
原文作者:未知  文章录入:4Lzx.com  发布时间:2005-9-1 17:21:39    

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

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

    0002H  标准错误设备 (serr)
    0003H  标准辅助设备 (saux)
    0004H  标准打印设备 (sprn)

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

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

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

nSHandle->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,scall
        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?
SOut  dd ?    ;存放标准输出的把柄
CharOut dd ?    ;记录实际输出的字符数

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

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

        lea ecx,CharOut
        invoke WriteFile,SOut,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中有函数SOut的声明,可用来直接输出信息,把上面的例子修改后就是下面的样子,可见来得更简炼,供大家参考:

        .386
        .model flat,scall
        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 SOut,addr mess
        invoke ExitProcess,NULL
        end start

< > 
  • 上一篇学院:

  • 下一篇学院:
  • 论坛交流】【发表评论】【打印本文】【关闭窗口