How can I create an Assembly program WITHOUT using libraries?
I've literally only just started looking to learn Assembly language. I'm using the NASM assembler on Windows Vista.
Usually, when I begin to learn a new language, I'll copy someone else's Hello World code and try to understand it line-by-line. However, I'm finding it suprisingy difficult to find a Hello World program that doesn't reference other libraries! You see, there's no point trying to understand each line of the code if it is closely linked with a whole library of additional code!
One of the reasons I want to learn Assembly is so that I can have near complete control over the programs I write. I don't want to be depending on any libraries.
And so my question is this: Can anyone give me NASM-compatible A开发者_如何学JAVAssembly code to a completely stand-alone Hello World program that can output to the Windows Vista console?
Alternatively, I appreciate that a library may be required to tell the pogram WHERE to print the output (ie. the Windows console). Other than that, I can't see why any libraries should be required. Am I overlooking anything?
Here is how:
.text
LC0:
.ascii "Hello, world!\12"
.globl _main
_main:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
pushl $-11
call _GetStdHandle@4
pushl $0
leal -4(%ebp), %ebx
pushl %ebx
pushl $14
pushl $LC0
pushl %eax
call _WriteFile@20
movl $0, %eax
leave
ret
You won't get around using operating system functions though. The downside of not using a library (such as the C library) is that you are coupling yourself to the specific OS.
As an alternative you could write a boot loader that prints "Hello, World" to the console directly through video memory. Then you have virtually nothing between you and the bare metal. You can find the instructions for making a boot loader pointed to by the OSDev Wiki. Start by reading that page to get an explanation of the concepts and ideas. After that follow the external link to the Hello World Bootloader. Note that the tutorial there uses the BIOS (a library) to print the message. You can, however, search around on the OSDev Wiki for information on how to print messages to the screen without the BIOS and make your required alterations.
Edited to add:
I strongly recommend that you use some kind of virtualisation for this activity. Don't be messing with boot sectors on your hard drive unless you REALLY know what you're doing.
Windows loads kernel32.dll
and ntdll.dll
libraries for all programs (independently from whether you use them on not). And you have to use them because low-level interface to the operating system depends on your processor and its version (i.e. it may change after some windows updates).
Windows is designed such that everything is in a library. The most basic library is Kernel32.dll, which includes functions for writing and reading files and other very basic operations. (Writing to the console is really just writing to a special file.)
On Linux, basic system functions are not in libraries, they are instead in "system calls" which are accessed through interrupt 80. Thus, on Linux it is possible (and pretty easy) to write some fairly advanced programs without any "library" calls at all.
But, you should probably embrace a few libraries. The whole point of libraries is to make your life easier. Go ahead and use the C standard library so that you can have some basic console and file input/output. From there you might not find any place where you really need libraries that you can't make yourself.
精彩评论