Difference between revisions of "Introducing the Instruction Set Part 3"

From Intellivision Wiki
Jump to: navigation, search
(Conditional Branches)
(Conditional Branches)
Line 35: Line 35:
  
 
== Signed Comparisons ==
 
== Signed Comparisons ==
 +
 +
The following branches are particularly useful when comparing signed numbers:
 +
 +
<TABLE BORDER=1>
 +
<TR><TH COLSPAN=2>Mnemonic</TH><TH>Branch taken when...</TH><TH COLSPAN=2>Mnemonic</TH><TH>Branch taken when...</TH></TR>
 +
<TR><TH>&nbsp;&nbsp;[[BEQ]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BZE]]&nbsp;&nbsp;</TH><TD><CODE> Z = 1            </CODE></TD><TH>&nbsp;&nbsp;[[BNEQ]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BNZE]]&nbsp;&nbsp;</TH><TD><CODE> Z = 0          </CODE></TD></TR>
 +
<TR><TH>&nbsp;&nbsp;[[BLT]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BNGE]]&nbsp;&nbsp;</TH><TD><CODE> S <> OV          </CODE></TD><TH>&nbsp;&nbsp;[[BGE]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BNLT]]&nbsp;&nbsp;</TH><TD><CODE> S = OV          </CODE></TD></TR>
 +
<TR><TH>&nbsp;&nbsp;[[BLE]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BNGT]]&nbsp;&nbsp;</TH><TD><CODE> Z = 1 OR S <> OV  </CODE></TD><TH>&nbsp;&nbsp;[[BGT]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BNLE]]&nbsp;&nbsp;</TH><TD><CODE> Z = 0 AND S = OV </CODE></TD></TR>
 +
<TR><TH COLSPAN=2>&nbsp;&nbsp;[[BOV]]&nbsp;&nbsp;</TH><TD><CODE> OV = 1            </CODE></TD><TH COLSPAN=2>&nbsp;&nbsp;[[BNOV]]&nbsp;&nbsp;</TH><TD><CODE> OV = 0          </CODE></TD></TR>
 +
</TABLE>
 +
 +
 +
  
 
== Unsigned Comparisons ==
 
== Unsigned Comparisons ==
 +
 +
These branches are useful when comparing unsigned numbers, including pointers to memory:
 +
 +
<TABLE BORDER=1>
 +
<TR><TH COLSPAN=2>Mnemonic</TH><TH>Branch taken when...</TH><TH COLSPAN=2>Mnemonic</TH><TH>Branch taken when...</TH></TR>
 +
<TR><TH COLSPAN=2>&nbsp;&nbsp;[[BC]]&nbsp;&nbsp;</TH><TD><CODE> C = 1            </CODE></TD><TH COLSPAN=2>&nbsp;&nbsp;[[BNC]]&nbsp;&nbsp;</TH><TD><CODE> C = 0          </CODE></TD></TR>
 +
<TR><TH>&nbsp;&nbsp;[[BEQ]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BZE]]&nbsp;&nbsp;</TH><TD><CODE> Z = 1            </CODE></TD><TH>&nbsp;&nbsp;[[BNEQ]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BNZE]]&nbsp;&nbsp;</TH><TD><CODE> Z = 0          </CODE></TD></TR>
 +
</TABLE>
 +
  
 
== Sign/Zero Comparisons ==  
 
== Sign/Zero Comparisons ==  
 +
 +
Common looping instructions, such as [[DECR]] and [[INCR]] only modify the [[Sign Flag|sign]] and [[Zero Flag|zero]] flags without updating the [[Carry Flag|carry]] or [[Overflow Flag|overflow]] flags.  These are best used with the following branches:
 +
 +
 +
<TABLE BORDER=1>
 +
<TR><TH COLSPAN=2>Mnemonic</TH><TH>Branch taken when...</TH><TH COLSPAN=2>Mnemonic</TH><TH>Branch taken when...</TH></TR>
 +
<TR><TH COLSPAN=2>&nbsp;&nbsp;[[BPL]]&nbsp;&nbsp;</TH><TD><CODE> S = 0            </CODE></TD><TH COLSPAN=2>&nbsp;&nbsp;[[BMI]]&nbsp;&nbsp;</TH><TD><CODE> S = 1          </CODE></TD></TR>
 +
<TR><TH>&nbsp;&nbsp;[[BEQ]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BZE]]&nbsp;&nbsp;</TH><TD><CODE> Z = 1            </CODE></TD><TH>&nbsp;&nbsp;[[BNEQ]]&nbsp;&nbsp;</TH><TH>&nbsp;&nbsp;[[BNZE]]&nbsp;&nbsp;</TH><TD><CODE> Z = 0          </CODE></TD></TR>
 +
</TABLE>
  
 
== If-Then and If-Then-Else ==
 
== If-Then and If-Then-Else ==

Revision as of 05:34, 12 October 2007

This segment of the tutorial introduces branches, particularly conditional branches and function calls. This is Part 4 of a series. If you haven't yes, you may wish to review at least Part 1 and Part 2.

Unconditional Branches and Jumps

Unconditional branches are branches that are always taken. Jump instructions do essentially the same thing. The following table lists the instructions:

MnemonicDescription Cycles Size
B Branch to label 92 words
J Jump to label 123 words
JD Jump to label while disabling interrupts 123 words
JE Jump to label while enabling interrupts 123 words


As you can see, the primary difference between branches and jumps is that branches are smaller and faster. Branches encode their "target address," the address being jumped to, as a relative displacement to the current address. Jumps, on the other hand, store the actual address of the target. In most cases, especially in a 16-bit ROM, there are few reasons to use a J instruction, although the combination instructions, JD and JE can be useful.

There is also a pseudo-instruction, JR, that allows "jumping to a location held in a register." It is really a pseudonym for "MOVR Rx, R7". Because it is a MOVR instruction, it will modify the Sign Flag and Zero Flag, which may be confusing if you're not expecting it. Otherwise, it is an efficient method for jumping to an address held in an register, such as when returning from a CALL.

Conditional Branches

The CP1610 has a rich set of conditional branch instructions. These branches work in concert with instructions that modify the CPU's flags in order to implement various constructs, such as if-then-else, loops, and so on. The following table summarizes the conditional branches.

MnemonicBranch taken when...MnemonicBranch taken when...
  BC   C = 1   BNC   C = 0
  BOV   OV = 1   BNOV   OV = 0
  BPL   S = 0   BMI   S = 1
  BEQ    BZE   Z = 1   BNEQ    BNZE   Z = 0
  BLT    BNGE   S <> OV   BGE    BNLT   S = OV
  BLE    BNGT   Z = 1 OR S <> OV   BGT    BNLE   Z = 0 AND S = OV
  BUSC   S <> C   BESC   S = C


Signed Comparisons

The following branches are particularly useful when comparing signed numbers:

MnemonicBranch taken when...MnemonicBranch taken when...
  BEQ    BZE   Z = 1   BNEQ    BNZE   Z = 0
  BLT    BNGE   S <> OV   BGE    BNLT   S = OV
  BLE    BNGT   Z = 1 OR S <> OV   BGT    BNLE   Z = 0 AND S = OV
  BOV   OV = 1   BNOV   OV = 0



Unsigned Comparisons

These branches are useful when comparing unsigned numbers, including pointers to memory:

MnemonicBranch taken when...MnemonicBranch taken when...
  BC   C = 1   BNC   C = 0
  BEQ    BZE   Z = 1   BNEQ    BNZE   Z = 0


Sign/Zero Comparisons

Common looping instructions, such as DECR and INCR only modify the sign and zero flags without updating the carry or overflow flags. These are best used with the following branches:


MnemonicBranch taken when...MnemonicBranch taken when...
  BPL   S = 0   BMI   S = 1
  BEQ    BZE   Z = 1   BNEQ    BNZE   Z = 0

If-Then and If-Then-Else

Looping

Function Calls

Simple Call/Return

Nested Call/Return

Passing Arguments via Return Address

Indirect Branches and Jump Tables

"It was a 'Jump to Conclusions' mat. You see, it would be this mat that you would put on the floor... and would have different conclusions written on it that you could jump to." -- Tom Smykowski, Office Space

Indirect Branching: "Jump Vectors"

Simple Jump Tables

Adding to the Program Counter

Moving On

At this point, you may wish to move along to the last part, or review earlier parts of this tutorial:

Or, you can return to the Programming Tutorials index.