Directory Listing Program

Source: Internet
Author: User

Let's write a short program that clears, displays the current disk directory, and asks
The user to enter a filename. (You might want to extend the "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 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 was called, displaying a prompt for a filename, and scanf are called so the user can input
The name. It does no 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 = the. 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 -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 modestrmovPfile,eax. IF EAX = =0 ;cannot open file?INVOKE printf,addr STR5jmpquit. Elseinvoke printf,addr STR6. ENDIF;Close the fileINVOKE fclose, PFilequit:ret ;return to C + + mainAsm_main endpend

The scanf function requires the arguments:the first is a pointer to a format string ("%s"), and
The second is a pointer to the input string variable (fileName). We won't take the time to
Explain standard C functions because there are 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.