Assemblers

An assembler converts assembly language code to machine code. The assembly language instructions could be written by hand or, as in the case of Free Pascal, been generated as an intermediate language from a higher level language. The task of writing an assembler is more straightforward than that of writing a compiler. One assembly language statement generally translates to one line of machine code. For an instruction such as MOV EAX, MyVar, the assembler needs to look up the binary equivalent of the opcode and to replace the variable name (operand) with an address. An assembler, like a compiler, uses a symbol table to store addresses.

For examples of assembly language instructions that the assembler needs to translate, see our tutorial on in-line assembler and Let's Build a Compiler!.

Again we would like you to learn by doing, and we show you how to get started with the MASM assembler (a free download). MASM is well-equipped with help files and examples.

  1. Select menu item Code > Create New Console Application
  2. Select a directory. It is easiest to use the existing MASM directory for your first trial.
  3. Give the file a name without an extension.
  4. Select menu item File > Open then select your file.
  5. Delete the print statement and insert your code and data.
  6. Select menu item Project > MakeIt.Bat
  7. Select menu item Project > Run Program

We simplified the code and inserted the generated code from one of Jack Crenshaw's compilers.

This is the entire code of SquaresDiff.asm.

include \masm32\include\masm32rt.inc

    .data
      A DWORD 99
    .code

start:
  MOV EAX, 77
  PUSH EAX
  MOV EAX, 23
  POP ECX
  ADD EAX, ECX
  PUSH EAX
  MOV EAX, 77
  PUSH EAX
  MOV EAX, 23
  POP ECX
  SUB EAX, ECX
  NEG EAX
  POP ECX
  IMUL ECX
  PUSH EAX
  MOV EAX, 77
  PUSH EAX
  MOV EAX, 77
  POP ECX
  IMUL ECX
  PUSH EAX
  MOV EAX, 23
  PUSH EAX
  MOV EAX, 23
  POP ECX
  IMUL ECX
  POP ECX
  SUB EAX, ECX
  NEG EAX
  POP ECX
  SUB EAX, ECX
  NEG EAX
  MOV A, EAX

  print "A = "
  print str$(A),13,10
  inkey
  exit
  RET
end start
   

We obtained the following result, as expected.

Start of assembler source and result

Start of assembler source and result

You can see that we are very close to automating a complete compilation from source code in TINY to an executable file. Perhaps you with achieve this yourself. Having created a .asm file with your compiler, you could assemble, link and execute it automatically by running a batch file using a ShellExecute command. We tested a batch file manually. We created a new directory on the C drive to contain SquaresDiff.asm then saved in the same directory a text file AssembleLinkRun.bat containing the three line script:
\masm32\bin\ml /c /Zd /coff SquaresDiff.asm
\masm32\bin\Link /SUBSYSTEM:CONSOLE SquaresDiff.obj 
SquaresDiff

We have now demonstrated the integration of TINY Version 1.1 with the MASM assembler to achieve complete compilation. Further development resulted in TINY14E with procedures, input, three types of loop, comments and optional semicolons.

Follow the first two links below to MASM demonstration programs. The first shows how you can use the locate procedure in simple games and the second demonstrates arithmetic using real numbers. Follow the third link for an introduction to ARM assembly language.

Programming - a skill for life!

Compilers (including Jack Crenshaw's "Let's Build a Compiler" applied to Intel processors and the Raspberry Pi), assemblers and interpreters