1. Computing
Send to a Friend via Email
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
Learning Assembler with Delphi
Page 1: What, when, why?
 More of this Feature
• Page 2: Basic operations
• Page 3: A proper example
• Page 4: Matrices
• Page 5: Fast screen plotting
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Delphi Pascal tech. articles
• Win/API programming

Article submitted by: Ian Hodger

   Abstract
The purpose of this article is to fill some of the gaps left by the original documentation provided with Borland's Delphi Developer, however, at the time of writing, all of the code and concepts discussed herewithin are fully applicable to all variants of Delphi. This article was originally written in 1997.
The Principle area of discussion will be the use of assembler within Object Pascal. Nevertheless, other aspects of programming will be covered in passing, as required for the examples given.
For simplicity, only a subset of Intel's instructions will be introduced, thus enabling a more generalized approach with few special cases. Furthermore, all the examples of assembler given will be contained in Pascal wrappers with portability in mind. Issues such as File access in assembler will not be covered, as this is more reliably achieved using standard Object Pascal. Indeed the document will seek to emphasis that one should only use assembler when necessary.
In general, the document will take the following form. First an idea will be introduced, followed immediately by a relevent example, concluding with an explanation of greater depth. This format is abandoned where clarity demands.

Using Assembler in Borland's Delphi
Before we start, I should like to state the level of knowledge which I shall assume of the reader. Writing Object Pascal should be second nature. One should be familiar with Delphi's built in debugging facilities. Finally a general understanding of what is meant by terms such as instantiation, null pointer and memory allocation, is a must. If any of the above encourages feelings of doubt, please tread very carefully. Furthermore, only 32bit code will be discussed, and as such Delphi 2.0 is a necessity.

   Why use Assembler?
I have always found Object Pascal to produce fast and efficient code, add to this the Rapid Development Environment of Delphi, and the need to use assembler becomes questionable. In all of my work with Delphi, I have come across just two situations where I have felt one should consider the use of low level code.

(1) Processing large quantities of data. Nb. I exclude from this any situation where a data query language is employed. For reasons of compatibility one should not tinker.

(2) High speed display routines. Nb. I refer here to quick easy routines that sit well with pascal, not the esoteric C++ headers, external function libraries and hardware demands of DirectX.

I hope to introduce an example or two by the end of this article which meet the above criteria, and in doing so demonstrate not only how and when to use assembler, but also the seamless manner in which Delphi incorporates this code.

   What is Assembler?
I will assume that you have a rough idea of how a cpu goes about it's business. Basically we have a fancy calculator with a big memory. The memory is no more than an ordered sequence of binary digits arranged in blocks of eight bits, each forming a byte. Thus each byte can store an integer in the range of 0 to 255, and each byte's position in the sequence of memory gives it an unique address by which the cpu may change or recover it's value. The cpu also has a number of registers (you may like to think of these as global variables) with which to play. For example eax,ebx,ecx and edx are the general 32bit registers and throughout this article, these are the only ones we shall use. This means the largest number we can store in register eax is 2 to the power 32 minus 1, or 4294967295, for those of us brought up on 8bit machines, this is shear luxury.
The cpu has the ability to manipulate the values of the registers, so to add 10 to the value of eax, one would issue the hexadecimal operation code
05/0a/00/00/00
this is machine code, there being a specific 'number' for each function the cpu can implement. To say that writing machine code is tedious would be an understatement, and as for debugging ! Assembler Language is just an easy way of remembering what machine code operations are available. The job of converting to machine code is done by an Assembler. Borland's Turbo Assembler is built in to Delphi, and is rather more pleasant to use. If we look again at adding 10 to eax, the appropriate assembler instruction is
add eax,10 {a := a + 10}
Similarly, to subtract the value of ebx from eax
sub eax,ebx {a := a - b }
To save a value for a rainy day, we can move it to another register mov eax,ecx {a := c }
or even better, save the value to a memory address
mov [1536],eax {store the value of eax at address 1536}
and of course to retrieve it
mov eax,[1536]

A quick aside on memory addresses, please bear in mind the size of the values you are moving about. The mov [1536],eax instruction affects not only memory address 1536, but 1537,1538 and 1539 as well, because as you will recall eax is 32bits long, or rather 4 bytes. Memory is always addressed in bytes.

What does the Compiler do with all those variables ? In every program you will have written, the compiler will have had to cope with a number of variables.Consider the pascal line
Count := 0;
To the compiler, this is just a value it has to remember. Consequently it sets aside a memory address to store this value, and to make sure it doesn't get confused later, calls this memory address 'Count'. This means that the code generated by the compiler for this line is something like this mov eax,0
mov Count,eax

The complier can't use a line such as
mov Count,0
because at least one parameter of the instruction must be a register.
If we were to consider the line
Count := Count + 1;
we would get something like this
mov eax,Count
add eax,1
mov Count,eax

For variables of types other than integer, matters can become more complex. So more of that later, and lets use what we've learnt so far.

Our first snippet of assembler. Forgive the trivial nature of this example, but we've got to start somewhere. Consider the following lines of pascal code.

function Sum(X,Y:integer):integer; 
begin 
 Result := X+Y; 
end; 

Object Pascal provides the asm .. end block as a method of introducing assembler to our code. So we could rewrite the function Sum thus

function Sum(X,Y:integer):integer; 
begin 
 asm
  mov eax,X
  add eax,Y
  mov Result,eax
 end;
end;

This works, but there is little point. There is no speed gain and we've lost the readability of our code. There are, however, instances where our currently limited knowledge of assembler can be put to good use. Lets say we wish to convert explicit Red,Green,and Blue values into a colour of type TColor suitable for use in Delphi. The TColor type describes a 24bit True Colour stored in the format of an integer ie. four bytes, the highest of which is zero, thereafter in the order Red, Green, Blue.

function GetColour(Red,Green,Blue:integer):TColor; 
begin 
 asm
{ecx will hold the value of TColor}
  mov ecx,0 
{start with the Red component}
  mov eax,Red 
{make sure Red is in range 0<=Red<=255}
  and eax,255 
{shift the Red value to the correct position}
  shl eax,16 
{adjust value of TColor}
  xor ecx,eax 
{same again with Green component}
  mov eax,Green 
  and eax,255
  shl eax,8
  xor ecx,eax
{and again with Blue}
  mov eax,Blue 
  and eax,255
  xor ecx,eax
  mov Result, ecx
 end;
end; 

You'll have noticed that I've introduced a few binary operations. These operations are straightforward and are also defined in Object Pascal itself, but for clairity let's explicity define all the operations introduced thus far and a few more.

Next page > Basic Assembler Operations > Page 1, 2, 3, 4, 5

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: Searching for data - DB/9.
Chapter nine of the free Delphi Database Course for beginners. Walking through various methods of data seeking and locating while developing ADO based Delphi database applications.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?

©2014 About.com. All rights reserved.