--->Diese Seite gibts auch in Deutsch.

General introduction into assembler

Overview

The main difference between assembly language and other programming languages is that you are writing exactly the code the processor will execute. This gives you the key to do optimizations at the max. And assembler grants you the most freedom in creating your code. On the other hand is the fact that assembly is specific for a certain machine. If you also want to use asm code on other processors, you have either to rewrite your code or to use a cross assembler. However, the output of a cross assembler cannot be as good as genuine code. If you are heavily using optimizations dealing with special capabilities for a certain CPU, cross-assembly output may be slower than corresponding programs written in a high-level language (HLL).

Many people say asm is outdated because it is difficult to use and modern compilers create faster code than assembly programmers do.

Well, beating non-x86 RISC compilers is not that easy. But if you are experienced enough, you can still make it faster. But most are interested in x86-programming. The x86 design is much more complex than the design of other processors. Most registers are specialized, instruction times differ when using special registers, you have less registers (x86 have 7 general purpose registers where other processors have 32) and there are more complex addressing modes. And the content of a register can be partly accessed without affecting the rest of the register content. This is a hard thing for compilers, but a quite easy one for the asm programmer

And asm is not harder to code than other languages. Once you have created the trunk of a program (read: Entry point, exit point, data and code fields) you can code as comfortable than in other programming languages. Of course, doing large calculations in asm is a bit more complex due to the lack of registers, but string manipulations are easier to code in asm. And you will not be confronted with strange error messages due to wrong definitions and this stupid type checking

And, at least on x86, asm is more up to date than all other languages. New instructions like MMX, SSE and 3DNow! can only be used effectively in assembler. Many other languages do not even know these instructions.

Optimization overview

There are two kinds of optimization: Many people think their programs will run twice as fast then being converted to asm. They are frustrated when they see that the difference is not remarkable. And I have been presented to C code which should show me that C is as fast as asm. Well, if you take a look at the code generated using a modern compiler one must say that it is not that bad. There are still some fragments of code one would never do in asm, but they do not affect performance drastically. This does not mean that compilers do create fast code automatically - the code I have seen showed me that it was made by people knowing more about processors and assembler than the average coder. It seemed to me that they knew how the code generated by the compiler would look like. I think doing it this way is not easier than coding in asm, but the source is more difficult to read.

Code size optimization is a different thing, it is especially important when creating programs running in the background like drivers, TSRs and several Tools. I have never seen a program below 1KB not being written in asm. The minimum overhead a HLL creates is about 2..4KB in C or TPascal and can reach hundreds of Kilobytes in Delphi.

OS-specific details

This part describes asm in conjunction with the following operating systems (others will follow):

DOS is an operating system which provides all of its services over interrupts. The data given to the service is stored in the processor registers. This predestinates DOS for assembly programmers because the data is transferred in the same way many assembler subroutines do. The only thing you need for using these services is a description of these services, like Ralph Browns Interrupt List. You can also do 32-Bit programming in DOS using DPMI.

Windows programs completely written in asm are quite rare. However, it is not harder to do than writing it in other languages without using a click-and-code GUI creation tool, for example pure C without a builder. Most windows routines are stored in a DLL. You are calling these routines via a short call. The data for the routine is stored on the stack, the return value resides in register EAX. In order to use these routines you have to link your code with one or more files containing information about the called DLLs. The more frequently used routines are defined in files coming with your assembler, for other routines and DLLs you create it with tools like IMPLIB. Note: If you want to link your assembler object files with object libraries created with Microsoft's compilers you have to use an assembler which can create COFF files like NASM and MASM. TASM can only create OBJ files.

Where to start as a beginner

First, you should know the basics of programming: Loops, jumps, variables, data fields,.. I started asm coding just after playing a bit with BASIC (I have never learnt more than the standard functions of it and after some weeks of asm I have stopped using BASIC :-). Once you have understood how to create a simple asm program you will find it easy to create more complex ones. Keep in mind that asm does not contain things that you will not be confronted with in other languages, too.

Second, get a good tutorial about asm. You may start using asm by writing smaller DOS programs in asm only or by using inline assembler if you are familiar with a HLL allowing it. In the first case it is easier to learn how the data is organized, especially in conjunction with the OS, in the second one it is easier to understand how the asm commands work. It is also a good idea to use a debugger with a simple (self-written) program and look what the directives do with the memory and the registers.

At last, you need an assembler and its tools. You can buy TASM or use assemblers like MASM or NASM which are for free. You may also look for A86, a shareware assembler. I recommend TASM or MASM when considering the amount of available sources and examples, but NASM is the better alternative if you want to code for several OSses due to its better macro and various object file capabilities.

I also recommend searching for code provided by other people. Try to understand them. They can help a lot in making yourself from a beginner into an advanced expert.

Mail the Author: webmeister@deinmeister.de

Homepage Programming Win32Asm Downloads Software Hardware Cartoons Sitemap