Lesson 6: Instruction Set
The following section will cover some of the basic instructions the Pico's SX-28 Processor has to offer. The instructions mentioned in this
section are listed in the the table below in the order that they appear in.
Instruction |
Parameters |
Meaning |
MOV |
dest, src |
Move src to dest (dest = src) |
INC |
dest |
dest++ (dest = dest + 1) |
DEC |
dest |
dest-- (dest = dest - 1) |
JMP |
addr9 |
Jump |
CSB |
|
Compare, Skip if Below |
CJAE |
|
Compare, Jump if Above or Equal to |
JZ |
addr9 |
Jump if zero flag is set |
JNZ |
addr9 |
Jump if zero flag is not set |
ADD |
dest, src |
ADD (dest = dest + src) |
SUB |
dest, src |
SUB (dest = dest - src) |
RL |
dest |
Rotate dest Left w/ Carry |
RR |
dest |
Rotate dest Right w/ Carry |
NOP |
|
No operation |
STC |
|
Set carry flag |
CLC |
|
Clear carry flag |
STZ |
|
Set zero flag |
CLZ |
|
Clear zero flag |
CALL |
addr8 |
CALL subroutine |
RET |
|
Return from a subroutine |
dest = destination, src = source, addr8 = 8-bit address, addr9 = 9-bit address |
Figure 6-1: Basic Instruction List
Move Instruction
The
MOV (Move) instruction is used to set one memory location equal to another memory location or constant. It can be equated
to a single equal sign in higher level languages.
Increment and Decrement Instructions
The
INC (increment) and
DEC (decrement) instructions are commonly used in conjunction with loop structures to count how
many times the loop has been executed. Incrementing and decrementing affects the zero flag and are ideal in use with jump instructions
to create loops and if/else statements.
Jump Instructions and Control Structures
The
JMP (jump) instruction and its variants are very useful instructions. They are most commonly used in conjunction with code labels
which point to a specific location in the program's execution to jump to. If/else and loop structures common to higher level programming
languages can be constructed using a combination assembly instructions. If/else statements can be written with a combination of jump and
comparison instructions and for loops can be written with jump, comparison, and increment/decrement instructions.
C++/Java If/Else |
Assembly #1 |
Assembly #2 |
if (x<y)
{
//stuff
}
else
{
//more stuff
}
|
CSB x,y
JMP else
//stuff
jmp endif
else
//more stuff
endif
|
CJAE x,y,else
//stuff
jmp endif
else
//more stuff
endif
|
Figure 6-2: Conditional examples
The high-level if/else statement above can be written in assembly with relative ease. The
CSB (Compare, Skip if Below) instruction is used
to test the condition. If the condition is false, it will skip the jump instruction pointing to the else code label and will perform the
code in the "stuff" area and then jump to the endif code label. If the condition is true, then the jump instruction will not be skipped
and the "more stuff" area will be reached. The third segment of code is an alternative way to write the loop by using the
CJAE (Compare,
Jump if Above or Equal to) instruction which combines compare and jump instructions.
C++/Java For Loop |
Assembly Loop w/ "for" logic |
Condensed Assembly Loop |
for(int i=0; i<10; i++)
{
//stuff
}
|
MOV i, 0
loop
CMP i,10
JZ endloop
//stuff
INC i
JMP loop
endloop
|
MOV i, 10
loop
//stuff
DEC i
JNZ loop
|
Figure 6-3: loop examples
The three loops above demonstrate how loop logic from a high-level language may be implemented with assembly language. The second loop's logic
is nearly identical to that of the first loop: Initializing i to 0, comparing i with 10 and escaping the loop if the condition isn't met, performing
"stuff", incrementing i, then jumping back to the beginning of the loop again. The second loop is logically identical to the first loop, but by tweaking
it a bit, the third loop can be attained which takes advantage of the zero flag to reduce the number of instructions needed. Looping can be further
optimized with more advanced instructions which combine incrementing and decrementing with jumping instructions.
Arithmetic Instructions
The arithmetic instructions supported by the Pico's processor are limited to
ADD (addition) and
SUB (subtraction). Division is not supported since
the processor is not equipped to handle floating point operations and the result of such operations are often floating point values. Multiplication
is also not supported but is easy to replicate and can be achieved by repeated addition.
MOV answer, 0
loop ; code label
ADD answer, number1
number2--
JNZ loop
|
The purpose of the code segment to the left is to demonstrate multiplication of two numbers by utilizing repeated addition.
Answer is initialized to zero. Number1 and Number2 should already have arbitrary values, where number2 is positive.
|
Figure 6-4: Arithmetic example
The code segment only has the functionality to multiply a positive or negative integer with a positive integer. It could be expanded upon to allow
either operand to be positive or negative. Integer division and modulus operations can be implemented in a similar manner.
Bit Rotation Instructions
Bit rotation instructions have a wide range of uses. Cycling through the bits of a memory location to check for flags is simple with bit rotations.
The SX-28 only supports
RL (left rotation) and
RR (right rotation) which include the carry flag during the rotation. The other shifts such as the
arithmetic shift can be emulated at the expense of a few more clock cycles.
RR w
MOV w.0, w.1
|
CC
RL w
|
To mimic the ASR instruction, the bits are first rotated, then the most significant bit is reinstated. The carry flag is simply ignored.
|
To mimic the ASL instruction, the carry flag is set to zero before the bits are rotated so zero shifts to the least significant bit.
|
Figure 6-5: Bit Rotation examples
Figure 6-6: Rotation Instruction and Arithmetic Shifts
The arithmetic shift is similar to the rotation instructions innate to the SX-28, but the sign bit is preserved during right shifts and 0 is shifted into the
least significant bit during left shifts. Quick division and multiplication by 2 can be achieved with arithmetic shifts.
Additional Instructions
The
NOP instruction can be used to synchronize program execution up with with an external I/O stream.
CLC and
STC can be used to clear and set the carry flag.
CLZ and
STZ can be used to clear and set the zero flag.
CALL and
RET are used to jump to and from subroutines.
A variety of comparison instructions are available for use in addition to the ones mentioned in the control structures portion.