# Lecture 9

## Assembly Language

Machine code is very hard to follow because it uses binary code to represent the instructions. To provide a more human friendly version of machine code, assembly language is used.

An assembly language statement is a line of text that translates into a single machine instruction.

Assembly Language is expressed in a more human readable form than the binary instructions and names are allowed for memory locations, registers, operations etc.

For example:

Example: translate the following C statement to assembly language and machine code.

x=y*(y+z);

Assume x,y and z are stored in memory locations 0,1 and 2 and there are general purpose registers called A,B,C...etc

```Assembly         Machine
Language         Code```
```MOV A,[1]        3e 00 01 ; A=y
MOV B,[2]        3f 00 02 ; B=z
ADD A,B          8c       ; A=A+B;
MULT A,B         9f       ; A=A*B
MOV [0],A        4e 00 00 ; x=A```
The 3e,3f,8c,9f and 4e are binary codes that contain the operation and registers for the instruction. Note that I have just made these up and they have no real meaning. (See chapter 2 and appendix C of Brookshear for an example of a simple instruction set)

Example:

if (x==3) x++;
y++;

```Assembly           Machine
Language           Code```
```MOV A,[0]          3e 00 00     ; A=x
SUB A,#3           58 03        ; A=A-3
JMP NZ,noadd       Fe 04        ; if (A==0)
ADD [0],#1         41 00 00 01  ;   x=x+1
ADD [1],#1         41 00 01 01  ; y=y+1```
Example:

while (x--<4) {
y+=x;
}

```Assembly         Machine
Language         Code```
```start:
MOV A,[0]        3e 00 00
SUB A,#4         58 04
JMP NC,endloop   fb 0e
MOV A,[0]        3e 00 00
SUB A,#1         58 01
MOV [0],A        4e 00 00
ADD A,[1]        31 00 01
MOV [1],A        4e 00 01
JMP start        fc e9
endloop:```
What about arrays and pointers?

A pointer is an address that can be stored in a register - it may point to the start of an array.

Example:

for(i=0;i!=100;i++) {
score[i]=0;
}

```Assembly
Language```
```MOV C,#0           ; i=0
MOV B,[score]
startloop:
CMP C,#100        ; if (i == 100)
JMP Z,endofloop   ;     goto endofloop
MOV A,#0
MOV [B+C],A       ; *(score + i)=0
ADD C,1           ; i++
JMP startloop     ; goto startloop
endofloop:```
Here is a small program that recompiles itself and generates an Assembly Language listing.
```#include <stdio.h>
#include <stdlib.h>
int function() {
int i,sum;
sum=0;
for(i=0;i!=100;i++)
sum=sum+i;
return sum;
}

void main(int argc[],char *argv[]) {
char s[256];

sprintf(s,"gcc -O %s.c -Wa,-aldn",argv[0]);
system(s);
} ```

The first column is a line number, next is a byte offset, next is the machine code, then labels and assembler directives and finally the assembly language itself. Try changing the program to see what code it produces.

Click here to see the program that is run by the cpu in the last lecture.

Where is Assembly Language compared with other programming languages.

________________________
Specification Languages
Z, VDM
________________________
Declarative Languages
Prolog
________________________
Functional Languages
Lisp, ML
_______________________
High Level Imperative Languages