In-line Assembler

Writing code in assembly language is much more difficult and takes much longer than in a high level language such as Pascal. It is also easier to make errors and harder to find them in assembly code. The code can be difficult to understand and therefore difficult to maintain. Now that compilers produce efficient code and processors are so quick, any gains in speed of execution are likely to be small. Despite all of the disadvantages, we encourage confident Pascal programmers to try coding in assembler. Assembler programming is very instructive, helping to give you an understanding of the composition of the actual machine code instructions that a processor follows. It should make you really appreciate what the operating system and Pascal compiler have been doing for you! You might also be able to begin to understand:
  • parts of in-built Pascal units such as SysUtils that have been written in in-line assembler;
  • the CPU window available in Delphi;
  • the assembler window in Lazarus.
The following instructions for viewing the CPU and assembler windows should enable you to see the relevance of the tutorial. If they make no more sense after the tutorial then we have failed!

CPU Window in Delphi

In Delphi, press F7 to start stepping through a program then, from the View menu select Debug Windows then CPU (or use the shortcut key combination Ctrl+Alt+C).

CPU Window

CPU Window

Assembler Window in Lazarus

In Lazarus, press F7 to start stepping through a program then, from the View menu select Debug windows then Assembler to give you the AT&T syntax used by Lazarus.

Assembler Window

Assembler Window

This tutorial covers only assembly language for Intel processors. We include both Intel syntax, used in Delphi and provided for in Lazarus, and AT&T syntax which is the default for Lazarus. In order to compile code for so many platforms, Lazarus translates Pascal to AT&T assembler code and then performs the final stage of the compilation according to the requirements of the target processor.

The tutorial has a Getting Started section with some easy programs for you to try and a minimum of theory. The Advanced Assembler section takes a more detailed look at registers and addressing modes and includes examples that are more difficult to understand. The final section applies the knowledge to the encryption and decryption of files.

Several simulator programs such as GASP are available for teaching purposes, and may be easier to understand than the code for your processor. If you are taught how to use a simulator, try in-line assembler afterwards.

Follow the numbered links to the sections described above. See also programs Encrypt (by Peter Hearnshaw) and CollatzASM (by Felix Thompson) for further examples of the use of in-line assembler and Peter's teaching aid GASP to help you to learn assembler.

Programming - a skill for life!

Pascal Programming Tutorials