Directory Listing Program

來源:互聯網
上載者:User

標籤:分享   and   printf   src   model   contains   extern   system   nbsp   

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 programextern "C" void asm_main(); // asm startup procvoid 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 BYTEprintf PROTO, pString:PTR BYTE, args:VARARGscanf PROTO, pFormat:PTR BYTE,pBuffer:PTR BYTE, args:VARARGfopen PROTO, mode:PTR BYTE, filename:PTR BYTEfclose PROTO, pFile:DWORDBUFFER_SIZE = 5000.datastr1 BYTE "cls",0str2 BYTE "dir/w",0str3 BYTE "Enter the name of a file:",0str4 BYTE "%s",0str5 BYTE "cannot open file",0dh,0ah,0str6 BYTE "The file has been opened",0dh,0ah,0modeStr BYTE "r",0fileName BYTE 60 DUP(0)pBuf DWORD ?pFile DWORD ?.codeasm_main PROC; clear the screen, display disk directoryINVOKE system,ADDR str1INVOKE system,ADDR str2; ask for a filenameINVOKE printf,ADDR str3INVOKE scanf, ADDR str4, ADDR filename; try to open the fileINVOKE fopen, ADDR fileName, ADDR modeStrmov pFile,eax.IF eax == 0 ; cannot open file?INVOKE printf,ADDR str5jmp quit.ELSEINVOKE printf,ADDR str6.ENDIF; Close the fileINVOKE fclose, pFilequit:ret ; return to C++ mainasm_main ENDPEND

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.

 

Directory Listing Program

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.