## Jupyter at Bryn Mawr College

Public notebooks: /services/public/dblank / CS240 Computer Organization / 2015-Fall / Notes

# Examples of Branching¶

Whenever you issue one of the condition-code setting instructions, it sets the N, P, and Z registers depending on whether the number in that register is negative, positive, or zero.

For example, if you use the ADD, AND, or NOT instruction and end up with 0000000000000000 in R0, then these registers would have the following:

• N is 0
• P is 0
• Z is 1

Exactly one of these will be 1 at any one time.

The BR instruction is made up of:

• opcode, 0000
• n, z, and p bits to match
• an offset to add to the PC

The BR statement will look at any of the N, Z, or P registers, and if there is a match with any of the bits in the instruction, then it will set the PC to PC + offset.

For example, this will always jump over the next instruction:

0000 111 000000001

This will always do nothing (eg, just go to next instruction), regardless of what ?s are because it won't match (n, z, and p are all 0s):

0000 000 ?????????

To demonstrate how branching works, we need to put something in a register, test it, and then branch to a new location.

In this first example, we will:

1. put 0 in R0
2. branch to the next statement if that is zero
5. HALT

Here it is in machine language:

In [3]:
.ORIG x3000
0101 000 000 1 00000 ; R0 <- R0 and 0
0000 111 000000000   ; BR NOOP
0001 000 000 1 00001 ; R0 <- R0 + 1
0001 000 000 1 00010 ; R0 <- R0 + 2
1111 0000 00100101   ; HALT
.END

============================================================
Memory dump:
============================================================
x3000: x5020
x3001: x1021
x3002: x1022
x3003: xF025
x3004: xF025

============================================================
Registers:
============================================================
PC: x3004
N: 0 Z: 1 P: 0
R0: x0000 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x0000

In [4]:
%exe

============================================================
Computation completed
============================================================
Instructions: 4
Cycles: 27 (0.000013 milliseconds)

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1
R0: x0003 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x3004

In [1]:
.ORIG x3000
0101 000 000 1 00000 ; R0 <- R0 and 0
0000 010 000000000   ; BR if zero to next statement
0001 000 000 1 00001 ; R0 <- R0 + 1
0001 000 000 1 00010 ; R0 <- R0 + 2
1111 0000 00100101   ; HALT
.END

============================================================
Memory dump:
============================================================
x3000: x5020
x3001: x0400
x3002: x1021
x3003: x1022
x3004: xF025

============================================================
Registers:
============================================================
PC: x3005
N: 0 Z: 1 P: 0
R0: x0000 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x0000

In [2]:
%exe

============================================================
Computation completed
============================================================
Instructions: 5
Cycles: 33 (0.000017 milliseconds)

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1
R0: x0003 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x3005


Indeed, we see that R0 is 3, so it was created by executing the Add 1 and Add 2 instructions.

In the next example, we will:

1. put 0 in R0
2. branch to the next, next statement if that is zero
5. HALT
In [5]:
.ORIG x3000
0101 000 000 1 00000 ; R0 <- R0 and 0
0000 010 000000001   ; BR if zero to next, next statement
0001 000 000 1 00001 ; R0 <- R0 + 1
0001 000 000 1 00010 ; R0 <- R0 + 2
1111 0000 00100101   ; HALT
.END

============================================================
Memory dump:
============================================================
x3000: x5020
x3001: x0401
x3002: x1021
x3003: x1022
x3004: xF025

============================================================
Registers:
============================================================
PC: x3005
N: 0 Z: 1 P: 0
R0: x0000 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x0000

In [6]:
%exe

============================================================
Computation completed
============================================================
Instructions: 4
Cycles: 27 (0.000013 milliseconds)

============================================================
Registers:
============================================================
PC: x048E
N: 0 Z: 0 P: 1
R0: x0002 R1: x0000 R2: x0000 R3: x0000
R4: x0000 R5: x0000 R6: x0000 R7: x3005


And indeed we see that R0 is 2, so that it skipped over the R0 + 1 instruction.

You can use the BR to do conditional code flow, like if, for, and while. Note that the offset can be negative.