Next: Further Assembly Programming Up: Computer Architecture - CSC Previous: The Central Processing Unit

Subsections

Assembly Language Programming

Introduction

We have dealt with the Level 0 view of computers in much detail in Chapters 3 and 4. In chapter 5 we have seen how digital logic components can be connected together, and, with a bit of microcode glue can be made to perform useful elementary operations - like adding the contents of a memory location to the accumulator (ADDD).

We now come to constructing sequences (programs) of these elementary operations (ADDD, LODD, JUMP etc. ...) to do useful tasks. This chapter will concentrate on the basics: sequence, selection,

In addition we will describe the operation of a two-pass assembler and show how this can be done manually.

Programs in Assembly Code

We now want to write some programs for the Mac-1A. For completeness, we repeat Figure 5.2 as Figure 6.1.


  
Figure 6.1: Mac-1a Instruction Set (limited version of Mac-1)
\begin{figure}\begin{tex2html_preform}\begin{verbatim}--------------------------...
...--------------------------------\end{verbatim}\end{tex2html_preform}\end{figure}

We can write in binary machine code, using Figure 6.1; but, that is tedious and error prone - because of the distraction of having to translate into a numeric code. It is better to use the mnemonics. The assembly code can then be assembled to machine (executable) code; this is done using a program called an assembler.

For the purposes of this chapter, assembly language is a language in which there is one-to-one relationship between symbolic instructions and machine instructions; we will come across pseudo-instructions that are really instructions to the assembler program, but these will be obvious.

Conventions

In the following examples, if we need to mention actual addresses, we will start all code at 100H and all data at 500H. Note again that the largest address is FFFH (12 bits). Also, be careful with FFFC onwards (409210 to 409510, which are used for memory-mapped input-output, see Chapter 5.

Often, for brevity, we will assume variables that variables a0, a1, a2 ... (integers) have been declared and are already in locations 500, 501, 502, .... Alternatively, we will use symbols for data. Use these assumptions in the exercises where appropriate.

Simple Program - add two integers

Program. Add the integer in a1 to the integer in a0 and put the result in a2, i.e.

a2 = a0 + a1;

In assembly code:

Start:    LODD a0   /get a0 into accum.
          ADDD a1   /accum. gets a0+a1
          STOD a2   /store result in a2

There are four fields in an assembly instruction:

Label.
Optional, only necessary if you need to jump to that instruction; can also be useful as comment, e.g. above. Always followed by ':', otherwise assembler will try to interpret it as an instruction.

Instruction mnemonic.

Operand
I.e. address of thing to suffer the operation. Either:

Comment
Comments are even more important for assembly language programs than the are for high-level language programs. Make your code maintainable by someone else!

Declaring variables

For a real assembler, we would have to declare all variables. And, tell it where to start main program at. E.g. in the example above,

DW a0 10H  /a0 is a WORD type, initialised to 10H
DW a1 22H  /these are not executable instructions
DW a2      /but pseudo-instructions 
           /ie. directives to the assembler to allocate 
           /storage and associate names with that storage
           / just like you declare variables in Pascal

ORG       100H      /start prog. at 100H

If-then

High-level language

      if(a0==a1) a2 = a2 + 1;
      else  next instructions...

Assembly language

If:       LODD a0
          SUBD a1
          JZER Then
          JUMP Next
Then:     LOCO 1   /load constant 1
          ADDD a2
          STOD a2
EndThen:  JUMP Next  /Unnecessary  
Next:     ...

If-then-else

High-level language

     if(a0==a1) a2 = a2 + 1;
     else a2 = a2 + 2;
     //next instructions...

Assembly language

If:       LODD a0
          SUBD a1
          JZER Then
Else:     LOCO 2 
          ADDD a2
          STOD a2
          JUMP Next
Then:     LOCO 1
          ADDD a2
          STOD a2
EndThen:  JUMP Next  /Unnecessary  
Next:     ...

Repetition

High-level language

   //sum first n integers
   int sum=0;
   int i=0;
   while(i<=n){
     sum= sum + i;
     i++;
   }

Assembly Language

init:    LOCO 0
         STOD sum
         STOD i
loop:    LODD n
         SUBD i
         JPOS loopend
loopbody:
         LODD sum  /load sum into accumulator
         ADDD i    /add i to it
         STOD sum  / and don't forget to store the result!
incr:    LOCO 1
         ADDD i
         STOD i
         JUMP loop
loopend: ...
It is straightforward to define deterministic loops, for example,

          for(i = 0; i<n; i++){
            sum= sum + i;
          }

in terms of do-while. Likewise do-until - the continuation test is merely carried out at the end of the loop, instead of at the beginning for do-while.

Machine Code, Memory Maps

Let us see how the If-then-else from the previous section would assemble into machine code. We use Figure 6.1 to translate.

For ease of understanding, we show the assembly language in the table. Assume that, initially, a0 contains 8, a1, 4 and a2, 2.

<-- Assembly language --->  <------- Machine code ------------>
                             Address   Instruction/Data
If:       LODD a0            0100      0500
          SUBD a1            0101      3501
          JZER Then          0102      5107
Else:     LOCO 2             0103      7002
          ADDD a2            0104      2502
          STOD a2            0105      1502
          JUMP Next          0106      610B
Then:     LOCO 1             0107      7001
          ADDD a2            0108      2502
          STOD a2            0109      1502
EndThen:  JUMP Next          010A      610B
Next:     ...                010B      ...
                             ...
a0                           0500      0008
a1                           0501      0004
a2                           0502      0002

Thus, we can identify two separate sections of memory: (a) program, (b) data. Of course, there is nothing really separating these, and there is nothing to stop assembly programmers modifying instructions as data, nor, for that matter, runaway programs attempting to execute data!

Actually, as shown in Figure 6.2, in a C or C++ program, we can identify four sections of memory. Exact implementation detail may differ depending on compiler, machine architecture and operating system. In some contexts, Figure 6.2 is called a memory map.


  
Figure 6.2: Memory Layout of a Program - Memory Map
\begin{figure}\begin{tex2html_preform}\begin{verbatim}Low memory address
0 +---...
...------------+ ---
Top of memory\end{verbatim}\end{tex2html_preform}\end{figure}

The Assembly Process

Assembly is the process of translating the symbolic assembly code into (numeric) machine code.

Example

Assemble (translate) the following assembly program into binary machine code. Assume a0, a1 ... at 500, 501 etc. Start code at 100.

DW a0  /shown here for completeness
DW a1
   ....
DW a10
DW one 1
If:       LODD a0
          SUBD a1
          JZER Then
          JUMP Next
Then:     LODD a10
          ADDD one
EndThen:  JUMP Next  /Unnecessary  
Next:     .....

Two-Pass Assembler

This section describes how a two-pass assembly program works. We said that one assembly language instruction maps to one machine instruction; thus, it would appear natural to run through the list of assembly instructions and translate them using a table such as Figure 6.1.

This works fine until we get to JZER Then; what address is Then?

You don't know until you have assembled up to Then.

Therefore, we must have a symbol table - - which is a list of (symbol, address) pairs. If a symbol has been entered in the table, then you can look up its value. Initially, the table is empty.

Also we must have an operation-code (op-code) table - similar to the symbol table, only it is fixed, and contains a table relating instruction code (LODD, STOD, ...) to the 4-bit op-code part of the instruction (0001, 0010, ...). In general, this should also contain the length of each instruction, or some means of calculating it. Unlike most machines, Mac-1a has a single fixed instruction length of one; most machines have instruction lengths which vary according to the instruction.

Pass One

The main job of pass one is to build the symbol table.

1.
Insert the data variables in the symbol-table, Figure 6.3;


  
Figure 6.3: Symbol Table - variables
\begin{figure}\begin{tex2html_preform}\begin{verbatim}Symbol Value Other Informa...
...
... etc.
a10 50a
one 50b\end{verbatim}\end{tex2html_preform}\par\end{figure}

2.
Next go through the program to be translated, and build the ILC (instruction location counter); start at 100 (we assume that all programs start at 100) and cumulatively add each instruction length. See Figure 6.4. Note: I have abbreviated some label names.


  
Figure 6.4: Partial Translation, pass one
\begin{figure}\begin{tex2html_preform}\begin{verbatim}Assembly Machine
Label Ope...
...essary 1 106* 6
Next: ..... 107*\end{verbatim}\end{tex2html_preform}\end{figure}

3.
Also, in this pass we might as well translate from symbolic op-code to numeric; see right-hand column of Figure 6.4;

4.
Finally, add the label symbols - marked `*' in Figure 6.4 - to the symbol table; see Figure 6.5.


  
Figure 6.5: Symbol Table - at end of pass one - completed
\begin{figure}\begin{tex2html_preform}\begin{verbatim}Symbol Value Other Informa...
...00
Then 104
EndT 106
Next 107\end{verbatim}\end{tex2html_preform}\end{figure}

Pass Two

Now the symbol table is complete and we can complete the translation by filling in the operand fields; see Figure 6.6.


  
Figure 6.6: Completed Assembly, pass two
\begin{figure}\begin{tex2html_preform}\begin{verbatim}Assembly Machine
Label Ope...
...sary 1 106 6 107
Next: ..... 107\end{verbatim}\end{tex2html_preform}\end{figure}

Also, we may require the data area must be initialised - to zero or some appropriate default.

Manual Assembly

Here in a slightly less formal way, is a recipe for assembling manually. We do it in a table, first write down the assembly code, the make two columns, one for address, the other for instruction or data. Then translate using Figure 6.1.

Do it in two passes. Leave Jump addresses blank first time round - you don't know the Jump destination addresses until after you have coded the Jump destination.

Use Hexadecimal for the machine code. If necessary, separate the binary fields in Figure 6.1 into groups of four - one for each Hex. digit, and add another column to Figure 6.1 to contain the Hex. representation of the operation code (op-code).

LABEL   ASS. INSTR.      ADDR.     BINARY INSTR.
------------------------------------------------
If:       LODD a0        100       0500
          SUBD a1        101       3501
          JZER Then      102       5(Then)
          JUMP Next      103       6(Next)

Then:     LODD a10       104       050A
          ADDD one       105       250B       

EndThen:  JUMP Next      106       6(Next)  

Next:     .....          107

Now, we know that Then = 104, Next = 107. So we can complete the assembly:

LABEL   ASS. INSTR.      ADDR.     BINARY INSTR.
------------------------------------------------
If:       LODD a0        100       0500
          SUBD a1        101       3501
          JZER Then      102       5104
          JUMP Next      103       6107

Then:     LODD a10       104       050A
          ADDD one       105       250B       

EndThen:  JUMP Next      106       6107  

Next:     .....          107

Linking and Loading

If the program above was viable to work on their own, it would be possible to load it into the addresses given: 100 to 107 for program and 500 to 50b for data, set the PC to 100, and the program would run.

Unfortunately, programs are usually made up from more than one module, and the results of each module translation - object files - must be linked together to produce an executable program.

Essentially, at the end of pass two, there will be symbols for which you do not have an address; these will refer to subprograms (see Chapter 7) that are contained in other modules. Also, the ILC will need to contain relative addresses, not absolute addresses as above.

In linking and loading the main jobs to be done are:

Refer also to Figure 6.2.

We further discuss linking (along with issues surrounding compilation and interpretation) in Chapter 9.

Exercises

In the following, unless specified, assume the executable code is to start at is at 100Hex, and a0, a1, a2, ... at 500, 501, 502 (Hex), ...

1.
Translate the following (separate) Java like instructions into Mac-1A.

(a)
a10 = a0;

(b)
         if( a1==a0 ) a10 = 1;
         else         a10 = 0;

        if( a1>a0 ) a10 = 1;
        else        a10 = 0;

        if( a1<a0 ) a10 = 1;
        else        a10 = 0;

(c)
a3 = a2 - a1;

(d)
a3 = a2*2;

(e)
a3 = a2*3;

(f)
a3 = a2*4;

(g)
(more difficult) a3 = a2 * a1; (assume a2, a1 both less than 128 ).

(h)
a0 = - a1;

(i)
a0 = - 1;

2.
Translate the following Java like code into Mac-1A.

     int a,b,c,d,eq;

     a = 16;
     b = 32;
     d = 64;
     c = a + b;
     if(c==d) eq = 1;
     else     eq = 0;

3.
Assuming start is at 100Hex, and a0, a1, a2 at 500, 501, 502 (Hex), manually assemble the following:

Start:    LODD a0
          ADDD a1
          STOD a2

4.
Manually assemble the following code.

DW a0  (at 500Hex)
 ...
DW a10 (at 50AHex)
DW x       50B
DW y       50C
DW z       50d

Start:    LODD a0   
          ADDD a1   
          STOD a2   
          JZER Zero
          LOCO 0
          STOD x
          STOD y
          LOCO 10
          STOD z
          JUMP Lab1
Zero:     LOCO FF
          STOD x
          LOCO 1
          STOD y
          LOCO 2
          STOD z
Lab1:     ...

5.
. What does the code in the previous question above do? Translate it into Java.
6.
Manually assemble:

DW one 1 (one is at 50BHex)

If:       LODD a0
          SUBD a1
          JZER Then

Else:     LODD a10 
          ADDD one
          ADDD one    
          JUMP Next

Then:     LODD a10
          ADDD one

EndThen:  JUMP Next  
Next:     .....

7.
Assuming that Mac-1a uses 16-bit twos complement to store signed integers, why is it impossible to 'load constant' (LOCO) minus 1, or indeed, any negative number. Explain how you would, using LOCO and one other instruction, load a negative constant (say -1000 decimal) into the AC register.

8.
Note the restriction on LOCO for positive numbers as well. Explain how, using LOCO and one other instruction, how you would cause the number 1000H to be loaded into the AC register.

9.
Assuming you know that the instruction LOCO FFFH is contained in address 100H,

(a)
Explain a simple way of causing 7FFFH to be loaded into the AC register, (without using an additional constant);

(b)
ditto FFFFH.

(c)
What is your judgment of this sort of programming?

10.
Write any program that will modify continuously its behaviour by modifying its code - i.e. it treats some of its code as 'data'.

11.
Here is a machine language program; usual layout, the program starts at 100; data variables a0, a1, ..., a10 are at 500, 501, 50a, respectively.

     Addr.     Instruction
     -----     -----------
     100       0500
     101       2500
     102       2500
     103       1501
     104       3502
     105       4109
     106       0505
     107       1503
     108       610c
     109       0504
     10a       1503
     10b       610c

(a)
Disassemble it, i.e. translate back into assembly code;

(b)
What does it do? Ans:

          a1 = 3*a0;
          if(a1>a2) a3 = a4;
          else      a3 = a5;
Explain.


next up previous contents
Next: Further Assembly Programming Up: Computer Architecture - CSC Previous: The Central Processing Unit
jc
2000-11-13