sorry if this is a basic question, but I can't seem to find the answer anywhere online.
I am learning assembly. Dev environment is VS2013 & MASM on Windows 7. I have a decent understanding of string manipulation and now I am trying to print a char to the console. The methods that I can find on the internet involve including MASM files, using MessageBoxA, or modifying the project settings to use DOS interrupts.
Including external files and modifying project settings are two things that I definitely don't want to do. MessageBoxA seems cool, but is there not a way to print to console in pure ASM? Thanks!
Windows is not DOS and has not been for a long time. Windows NT based versions does not allow you to write directly to hardware without a kernel driver.
If you don't care about Unicode then you just need to call GetStdHandle(STD_OUTPUT_HANDLE) to get a handle to stdout and then use this handle with WriteFile.
Use WriteConsole to write Unicode strings if GetConsoleMode returns true.
Related
How do I write a code which copies a text file using assembly and in windows?
My compiler is masm.
Not really sure whether you want to read from the file to memory and do something with that, or simply create a copy. In the first case, use CreateFile, otherwise go with CopyFile. You'll need to link with kernel32.dll to be able to use these functions.
In Windows, interacting with the OS involves calling API functions rather than making interrupt calls as in Linux.
If you just want to copy the file, call CopyFile. If you want to read the file, do some processing, and then write, you'll need CreateFile, ReadFile, and WriteFile. (You can find documentation for those functions from the CopyFile link above.)
I don't have a link to a good tutorial on calling Windows API functions from assembly language. Searching reveals some information, but nothing that I'd call a good tutorial. You'll have to look for examples and try things.
This question already has answers here:
System Calls in Windows & Native API?
(5 answers)
Closed 4 years ago.
I have a (very) basic understanding of assembly using system calls on Linux (I use the GNU assembler as).
On Windows 7, I am using the MinGW (32-bit) port of the GCC compiler suite to produce assembler programs. On Linux I regularily use the C library for some OS interactions in my assembler programs, and on my Windows platform this works perfectly as well using MinGW. Sometimes, however, I want to use low-level system calls -- mostly to keep my executables as small as possible. On Linux I know how to do this:
movl $0, %ebx
movl $1, %eax
int $0x80 ; exit with code 0
I also use these system calls for reading/writing chars to/from the terminal (for writing syscall with 4 in EAX for example). I was wondering how to do this on a Windows NT platform. Is it possible? I looked at this table, but I don't really understand the names of the syscalls. Any help is welcome.
The Nt* set of functions are undocumented with good reason: it's internal to Windows and changes between versions, meaning that programs that target it directly are at high-risk of breaking between Windows versions.
Really, there is not that big an overhead with targeting public, documented functions, and you get Microsoft's guarantee that your program will work with future versions of Windows provided you use the API correctly.
For that reason, I won't provide you with the answer you want. I strongly advise you to use the public console API: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073%28v=vs.85%29.aspx
Update
I assume that is for experimentation or fun, indeed the OP wrote:
Sometimes, however, I want to use low-level system calls -- mostly to keep my executables as small as possible.
...I find this line of reasoning problematic and I don't want to act as an enabler for these kinds of development practices, especially as there is no practical benefit to using syscalls for console tasks.
If someone wants to know how to use low-level syscalls in Windows then please post a new question, suitably framed, and I'll gladly answer it.
But as a starting point, see https://j00ru.vexillium.org/syscalls/nt/64/ for a reverse-engineered table of x64 NT system-call numbers broken down by Windows kernel version. (Do not use in portable code, only for experiments to satisfy your curiosity about how Windows and/or asm works.)
I am writing a program that handles mostly Unicode text. The C standard library function 'fopen' provides for writing the characters to file in utf-8 format by including in the mode string argument "..., ccs=utf-8". It seems that the Windows API 'CreateFile' does give such provision. Must I use 'fopen' then?
This is Specific to programming under Windows, using Visual Studio, and Microsoft tools. My personal advice is to not to use fopen with the extended syntax, otherwise later there will be compatibility issues when porting your application to other operating systems. When under Windows, do the Windows way, use CreateFile.
The contents of the file are defined not by the file-opening function, but the actual data you write. After you get the file handle (either by fopen or CreateFile), you can write in UTF-8, or ANSI, or whatever you like.
Note that some encodings require a special bit at the beginning of the file.
First, sorry for my poor English...
I want to add a virtual folder to Windows Explorer using a Namespace Extension (NSE), and I want users to be able to open this virtual folder to explore some path (e.g., c:\test).
How can I do this using Delphi? Thanks.
The place to start is the MSDN documentation: Introduction to the Shell Namespace. Naturally this is written from a C++ perspective but it's not too hard to map that across to Delphi.
Another excellent resource for such tasks is Code Project. For example: The Complete Idiot's Guide to Writing Namespace Extensions - Part I by Mike Dunn. In fact this is just part of an excellent series of articles on shell extensions.
At present Delphi is a poor choice because it does not produce 64 bit executables. This means that your shell extension will not run on 64 bit Windows which is now a serious limitation.
With the recent release of Delphi XE2 this limitation has been removed. XE2 is capable of producing 64 bit executables and can therefore be used to produce 64 bit shell extensions.
Basically a shell extension is a COM object that implements a set of interfaces. Which interfaces needs to be implemented depends on the type of the extension (there are some used by any extension, of course).
You can start reading here (you need some C -> Delphi translation, but when you start to work on such matters is better you get prepared to it), and then the reference is of course MSDN
http://www.shellplus.com/examples/namespace-extension-example.html
http://delphipower.tripod.com/winshell.htm
How can I operate on files in masm.
I mean using standard libraries included to microsoft (masm).
Or somethning available in windows without linkink libraries.
Start with the MASM32 site (it will provide you with the basic includes you need).
Use the Win32 API as your RTL (nothing to statically link).
For console IO, see the GetStdHandle call to get you started.
Use INVOKE to handle the Win32 parms call parameters.
Also remember that you can define local variables in a MASM function and MASM will generate all the BP relative addressing for you, as well as the entry and exit sequence. MASM (32-bit) has a number of very handy HLL features (that are broken in 64-bit MASM, btw).
Have fun!