![]() |
Jupyter at Bryn Mawr College |
|
|
Public notebooks: /services/public/dblank / CS240 Computer Organization / 2015-Fall / Notes |
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:
Exactly one of these will be 1 at any one time.
The BR
instruction is made up of:
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:
Here it is in machine language:
.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
%exe
.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
%exe
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:
.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
%exe
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.