Section 13.4
Computer Architecture
13.5 Assembly Language
Each processor type has its own binary (machine code) langauge. Assembly language
is short-hand for this.
13.5.1 Data Transfer Instructions
These move (copy) a value from register or location to another.
MOVE <source>, <destination>
Example Program
MOVE #100, R0
MOVE #112, R1
RTS
13.5.2 Memory Mapped Output
Certain memory locations map directly to an output device.
In the ASM Tutor memory locations 5 to 14 map to the mapped memory window.
MOVE #'H', 5
13.5.3 Address Mode
These modes specify how the data in an instruction is to be acquired.
Immediate Mode
In this mode the operand is a literal value.
MOVE #35, R0 - Move the value 3510
Direct (Absolute) Mode
Here the operand holds the address of the data to be used in the instruction.
MOVE 129, R0 - Move the data in the location 12910 into R0
Direct addressing is slower than immediate addressing because:
- The data must subsequently be fetched from its location
- High memory locations (E.g. Addresses above 128K in a 16-bit machine)
will need to be loaded into the CPU in several FE cycles.
Indirect Mode
In this mode the operand holds a register/memory location that in turn holds
the address of the data to be used.
MOVE (R1), R0 - Moves the contents of the memory location that is referenced
by register R1 into register R0.
Indexed Mode
The operand address is calculated by adding a base address to a value in a
register (often called the index register).
MOVE 200(R0), 129 - Move the value from the address stored in R0 + 200 to
address 129.
Relative Mode
This mode is used in branch instructions.
13.5.4 The Keyboard Buffer
In the ASM tutor, memory address 4 is reserved as the keyboard buffer.
The contents are changed by clicking on the appropriate key in the interrupt
window.
13.5.5 Unconditional Flow Control
Normally instructions execute in sequence as the program counter is
incremented.
Jumping
Jumping causes the program to branch to a specified address.
JUMP <address>
For example
JMP 128 - Jumps to address 128
JMP (R3) - Jumps to address in R3
Sub-routines
Unconditional flow control can be acheived by the use of a sub-routine.
JSR <address>
- Jump to an effective address
RTS
- Return to the next instruction after the JSR
that
invoked the sub-rotuine.
Most OSs provide a number of sub-routines.
E.g.
- to access a file
- to display a window
- to get input from the keyboard
ASM Tutor emulates this with addresses 86 to 89.
To Output A String
- Make sure that the string is terminated with a 0.
- Place the start address of the string into R0.
JSR 86
To Input A String
- Place the start address of the buffer to receive a string into R0.
- Place the size of the buffer into R0.
JSR 88
13.5.7 Arithmetic Instructionn
Addition
ADD <source>, <destination>
The source
is added to the destination
. The sum is
placed in the destination
.
Subtraction
SUB <source>, <destination>
The source
is subtracted from the destination
and the result is
left in the destination
.
13.5.8 Branch Instructions
Conditional flow control can be acheived by comparing two values and branching,
depending on the result.
First use:
CMP
- to compare two values and set the status register
Then a BXX
instruction:
BEQ
- Branch if equal
BNE
- Branch if not equal
BGE
- Branch if greater than or equal
BGT
- Branch if greater than
BLE
- Branch if less than or equal to
BLT
- Branch if less than
13.5.9 The Status Register
After an arithmetic operation the result may be negative or an overflow may occur.
Normally the status register contains the following bits:
8 (23)
|
4 (22)
|
2 (21)
|
1 (20)
|
Carry
|
Overflow
|
Zero
|
Negative
|
13.5.10 Logical Operators
These operations are used to manipulate the individal bits in memory locations.
The AND Operator
The AND operator takes two inputs and gives one output.
The output will be only be 1 if both the first and the second inputs are 1.
Input 1
|
Input 2
|
Output
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
0
|
0
|
1
|
1
|
1
|
When several logical operators are applied to values represented by several bits
the answer is calculated in a bitwise manner.
E.g. 1010 AND 1001 gives 1000
An Application of AND - Masking
E.g.
|
1010 1110 1010 0001
|
AND
|
0000 0000 1111 1111
|
gives
|
0000 0000 1010 0001
|
In order to find the lower byte in a 16-bit word the word can be masked with
0000 0000 1111 1111.
The OR Operator
Input 1
|
Input 2
|
Output
|
0
|
0
|
0
|
0
|
1
|
1
|
1
|
0
|
1
|
1
|
1
|
1
|
The output will be 1 if either the first, second or both inputs are 1.
E.g. 1010 OR 1001 gives 1011
An Application of OR - Converting Binary Numbers To ASCII
The ASCII code for character '3' is:
0011 0011
This can be generated from the binary code for 310 by:
0011 0000
The NOT Operator
NOT only takes one input.
An Application of NOT - Two's Complement
In an 8 bit system:
310 = 0000 00112
-310 = NOT 0000 0011 ADD 1
-310 = 1111 1100 ADD 1
-310 = 1111 1101
ASM Syntax
AND and OR take a source and a destination which are treated as input 1 and input 2.
The results are then replaced in the destination.
AND <source>, <destination>
OR <source>, <destination>
Immediate mode binary numbers are prefixed with a '%'.
E.g. MOVE 1011 0111, 123
NOT takes a single address or register and swaps the 1s and 0s.
Most assemblers support three types of shift instruction:
Logical Shift
A logical shift causes the m.s.b. to be shifted to the carry bit (in the status
register) and zero moves to the l.s.b.
ASM Syntax
LSL #1, <address>
- Logical shift left (one place)
LSR #1, <address>
- Logical shift right (one place)
Arithmetic Shift
This results in multiplying or dividing the value by 2. Arithmetic shifting takes
account of two's complement negative numbers.
ASM Syntax
ASL #1, <address>
- Arithmetic shift left (one place)
ASR #1, <address>
- Aritmetic shift right (one place)
Rotational Shift
Here the bits in an address are are physically rotated left or right. The bit that
jumps from one end to the other is replicated in the carry bit.
ASM Syntax
ROL #1, <address>
- Rotate left (one place)
ROR #1, <address>
- Rotate right (one place)
13.5.12 Iteration In Assembly Language
It is useful to be able to work out the equivalent assembly language code for
high-level constructs.
FOR K = 1 TO 10
DO N to N + 1
END FOR