More
on Picoblaze assembly programming
1. Single bit manipulation


The following
code segment shows how to set, clear, and toggle the second LSB of the
SO register:


We can also
apply the concept of the and mask to the test instruction to check a
single bit. For example, the following code segment tests
the MSB of the SO register and branches to a proper routine accordingly:

A single bit can
be extracted by applying the
previous code. For example, the following code segment extracts the MSB
of the s0 register and stores it in the sl register:
It is equivalent
to "if MSB is zero, don't do anything because s1 is already loaded as
0, if it is not zero (1) then load 0x01 to s1".

2.
Multiple-byte manipulation
Assume that x
and y are 24-bit data and that each occupies three registers. The
following code segment illustrates the use of carry in multiple-byte
addition:
The first
instruction performs normal addition of the least significant bytes and
stores the carry-out bit into the carry flag. The second instruction
then includes the carry flag when adding the middle bytes. Similarly,
the third instruction uses the carry flag from the previous addition to
obtain the result for the most significant bytes.

The incrementing
and subtraction of multiple bytes can be achieved in a similar fashion:



Multiple-byte
data can be shifted by including the carry flag in the individual shift
instruction. For example, the sla instruction shifts data left one
position and shifts the carry flag into LSB. The code for shifting a
3-byte data left can be written as



3.
Control structure
A high-level
programming language usually contains various control constructs to
alter the execution sequence. These include the if-then-else, case, and
for-loop statements. On the other hand, PicoBlaze provides only simple
conditional and unconditional jump instructions. Despite its
simplicity, we can use them with a test or compare instruction to
implement the high-level control constructs. The following examples
illustrate the construction of the if-then-else, case, and for-loop
statements. Let us first consider the if-then-else statement:


The code uses
the compare instruction to check the sO==sl condition and to set the
zero flag. The following jump instruction examines the flag and jumps
to the else branch if the flag is not set. The case statement can be
considered as a multiway jump, in which execution is transferred
according to the value of the selection expression. The following
statement uses the SO variable as the selection expression and jumps to
the corresponding branch:

The multiway
jump can be implemented by a hardware feature known as "index address
mode" in some processors. However, since PicoBlaze does not support
this feature, the case statement has to be constructed as a sequence of
if-then-else statements. In other words, the previous case statement is
treated as

The
corresponding assembly code segment becomes:

The for-loop
statement executes a segment of the code repetitively. The loop
statement can be implemented by using a counter to keep track of the
iteration number. For example, consider the following:

The assembly
code segment is:

4.
Subroutine development
PicoBlaze uses
the call and return instructions to implement the subroutine. The call
instruction saves the current content ofthe program counter and
transfers program execution to the starting address of a subroutine. A
subroutine ends with a return instruction, which restores the saved
program counter and resumes the previous execution.
Because of the
primitive nature of the assembly language, thorough documentation is
instrumental. A subroutine should include a descriptive header and
detailed comments. A representative header is shown below:

