Architecture Test 3

Condition Flags

Four condition flags

  1. Carry flag - C
  2. Overflow flag - V
  3. Zero flag - Z
  4. Negative flag - N

These are assigned new values every time an instruction is performed

Carry Flag - C

 It is said that a carry has occured when the addition of the filnal column would result in the need for an extra column to fully represent the number. The carry flag is assigned a value of 1 when carry has occured, otherwise it is assigned the value 0

Zero Flag - Z

Assigned a value of 1 when the operation results in a value of 0, representing true. Otherwise, the flag is a assigned the value 0, representing false.

Unsigned and Signed Interpretations of Binary Numbers

A computer must represent everything using 1's and 0's. A minus sign cannot be put in front of the binary number in a computer. In order to represent both positive and negative numbers, a different set of weights is used. To represent negative numbers, the weight of the leftmost bit is changed to a negative number.

For an 8-bit binary number, the binary number 10011010 represented the decimal value -102 (-128 + 16 + 8 + 2 = -102)

It is easy to tell if a number is positive or negative by looking at the leftmost bit. If the bit is 1, the number must be negative.

Using the unsigned intreptation, an 8-bit binary number can represent a decimal value in the range of 0 to 255. For the signed interpretation, an 8-bit number can represent a decimal value in the range -128 to 127.

Overflow Flag - V

When two numbers are added, whether or not the result is within the valid range must be determined. If the result is not within the valid range, it is said that overflow has occured, and the overflow flag is assigned the value 1. Otherwise V is assigned the value 0.

It is important to remember the following rule which is used to decide whether overflow has occured when two signed numbers have been added.

If the two numbers are positive and the result is negative, overflow has occured.
If the two numbers are negative and the result is positive, overflow has occured.
If one number is positive and the other negative, it is not possible for there to be overflow.

Negative Flag - N

Assigned a value of 1 when the result using signed interpretation is negative. Otherwise assigned a value of 0.

 

Branch Instructions

Unconditional Branch Instructions

Branch Always - BRA

Regarded as the simplest branch instruction. A label is placed in front of the instruction to which to branch to. It is good practice to choose a meaningful name for the label.

Conditional Branch Instructions

Branch if equal to zero - BEQ

Control branches to the instruction labelled, only if the result of the previous instruction was zero. Otherwise control simply moves on to the next instruction.

Branch if not equal to zero - BNE

Control branches to the instruction labelled, only if the result of the previous instruction was not zero. Otherwise control simply moves on to the next instruction.

Branch if higher than zero - BHI

Control branches to the instruction labelled, only if the result of the previous instruction was higher than zero. Otherwise control simply moves on to the next instruction.

Logical Shift Left - LSL

   Shift to be performed on rightmost byte of data register D2
v
LSL.B #1, D2
^
Number of bits to shift

Branch if Carry Clear - BCC

Looks at the value placed in the carry flag by the previous instruction. If this value is zero the branch is taken, otherwise control simply moves to the next instruction.

Often used with LSL. Control will branch to the instruction labelled if the bit that fell off into the carry flag was zero. Otherwise control moves to the next instruction.

Branch if Carry Set - BCS

Causes control to branch if the value placed in the carry flag by the previous instruction is 1.

Compare Instruction - CMP

SUB.B D1, D2 subtracts the rightmost byte in the data register D1 from the rightmost byte in data register D2, and stores the result in data register D2. The compare instruction CMP.B D1, D2 performs exactly the same subtraction, but does not store the result.

A compare instruction is often used with a conditional branch instruction.

Summary of Conditional Branch Instructions

Instruction Numbers interpreted as signed or unsigned Branch taken if result of previous instruction was...

BEQ

Unsigned equal to zero
BNE Unsigned not equal to zero
BGT Signed greater than zero
BLT Signed less than zero
BGE Signed greater than or equal to zero
BLE Signed less than or equal to zero
BHI Unsigned higher than zero
BLS Unsigned lower than zero or the same

 

Creating a loop

A loop is created using a loop counter and the conditional branch instruction BNE.

 

Shift Instructions

Used to move a group of bits one or more places to the left or right. If the contents of the data register are shifted left or right by one bit, the bit that falls off the edge is transferred to the carry flag. A shift instruction and a branch instruction that depends on the condition of the carry flag are often used together.

The leftmost bit falls off into carry flag C. The rightmost bit always becomes zero.

Logical Shift Left - LSL

   Shift to be performed on rightmost byte of data register D2
v
LSL.B #1, D2
^
Number of bits to shift

Branch if Carry Clear - BCC

Looks at the value placed in the carry flag by the previous instruction. If this value is zero the branch is taken, otherwise control simply moves to the next instruction.

Often used with LSL. Control will branch to the instruction labelled if the bit that fell off into the carry flag was zero. Otherwise control moves to the next instruction.

Branch if Carry Set - BCS

Causes control to branch if the value placed in the carry flag by the previous instruction is 1.

Address Registers

An address register is a box containing the address of a location in main memory. Each address register is entirely seperate from the others. It can be thought of as a pointer pointing to a memory location.

Load Effective Address - LEA

Initially an address is placed in an address register using the LEA instruction.

eg
LEA $2004,A0

places the address $2004 in the address register A0.

Working with Address Regsiters and Pointers

Once LEA has been used to initialise an address register, the pointer can be used to access the memory location. The pointer can also be moved so that it points to a different memory location.

This is done using post increment register indirect addressing, predecrement register indirect addressing and register indirect addressing. 

Post Increment Register Indirect Addressing

MOVE.B (A0)+, D2

A0 is inside round brackets. This means the memory location pointed to by address register A0.

The + sign after the brackets means after performing the instruction, move the pointer forward one place.

Copy a byte from the memory location pointed to by address register A0 to data register D2, finally move the pointer forward one place.


MOVE.W (A0)+, D2

This instruction is working with a word rather than a byte. so the word pointed to by A0 is copied from memory to the data register. After the instruction has been performed the pointer is moved forward one word, that is, two memory locations!


MOVE.L (A0)+, D2

This instruction is working with a longword. After the instruction has been performed the pointer is moved forward one longword, that is, four memory locations!

Pre Decrement Register Indirect Addressing

MOVE.B -(A0), D2

The - sign before the brackets means before performing the instruction, move the pointer back one place.

Firstly move the pointer back one place, then copy a byte from the memory location pointed to by address register A0 to data register D2.


MOVE.W D2, -(A0)

The previous instruction copied fata from memory into a data register. In contrast, this instruction copies a word from data register D2 into memory. The - sign means before performing the instruction, move the pointer back one place.

The instruction says firstly, move the pointer back one place (in this case a word, which is two memory locations), then copy a word from data register D2 to the new memory location pointed to by address register A0.

Register Indirect Addressing

MOVE.B (A0), D2

There is no + or - sign, so the pointer remains unchanged.

Copy a byte from the memory location pointed to by address register A0 to data register D2.