zoukankan      html  css  js  c++  java
  • Directory Listing Program

    Let’s write a short program that clears the screen, displays the current disk directory, and asks
    the user to enter a filename. (You might want to extend this program so it opens and displays the
    selected file.)
    C++ Stub Module The C++ module contains only a call to asm_main, so we can call it a
    stub module:

    // main.cpp
    // stub module: launches assembly language program
    extern "C" void asm_main(); // asm startup proc
    void main()
    {
    asm_main();
    }

    ASM Module The assembly language module contains the function prototypes, several strings,
    and a fileName variable. It calls the system function twice, passing it “cls” and “dir” commands.

    Then printf is called, displaying a prompt for a filename, and scanf is called so the user can input
    the name. It does not make any calls to the Irvine32 library, so we can set the .MODEL directive to
    the C language convention:

    ; ASM program launched from C++ (asmMain.asm)
    .586
    .MODEL flat,C
    ; Standard C library functions:
    system PROTO, pCommand:PTR BYTE
    printf PROTO, pString:PTR BYTE, args:VARARG
    scanf PROTO, pFormat:PTR BYTE,pBuffer:PTR BYTE, args:VARARG
    fopen PROTO, mode:PTR BYTE, filename:PTR BYTE
    fclose PROTO, pFile:DWORD
    BUFFER_SIZE = 5000
    .data
    str1 BYTE "cls",0
    str2 BYTE "dir/w",0
    str3 BYTE "Enter the name of a file:",0
    str4 BYTE "%s",0
    str5 BYTE "cannot open file",0dh,0ah,0
    str6 BYTE "The file has been opened",0dh,0ah,0
    modeStr BYTE "r",0
    fileName BYTE 60 DUP(0)
    pBuf DWORD ?
    pFile DWORD ?
    .code
    asm_main PROC
    ; clear the screen, display disk directory
    INVOKE system,ADDR str1
    INVOKE system,ADDR str2
    ; ask for a filename
    INVOKE printf,ADDR str3
    INVOKE scanf, ADDR str4, ADDR filename
    ; try to open the file
    INVOKE fopen, ADDR fileName, ADDR modeStr
    mov pFile,eax
    .IF eax == 0 ; cannot open file?
    INVOKE printf,ADDR str5
    jmp quit
    .ELSE
    INVOKE printf,ADDR str6
    .ENDIF
    ; Close the file
    INVOKE fclose, pFile
    quit:
    ret ; return to C++ main
    asm_main ENDP
    END

    The scanf function requires two arguments: the first is a pointer to a format string (“%s”), and
    the second is a pointer to the input string variable (fileName). We will not take the time to
    explain standard C functions because there is ample documentation on the Web. An excellent
    reference is Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, 2nd
    Ed., Prentice Hall, 1988.

  • 相关阅读:
    Egret白鹭开发小游戏中容易犯的错
    egret之消除游戏开发
    Jmeter学习之— 参数化、关联、断言、数据库的操作
    JMeter学习-031-JMeter 3.0 POST Body Data 中文乱码问题
    Nginx负载均衡的五种策略
    Linux性能监控分析命令(五)—free命令介绍
    Linux性能监控分析命令(四)—top命令介绍
    ssh连接时提示THE AUTHENTICITY OF HOST XX CAN'T BE ESTABLISHED
    Linux性能监控分析命令(二)—sar命令介绍
    jProfiler远程连接Linux监控jvm1运行状态
  • 原文地址:https://www.cnblogs.com/dreamafar/p/6006194.html
Copyright © 2011-2022 走看看