Micro Session5
Micro Session5
Clara Zaiter
[email protected]
BSF 0x20, 3
Bit 3 in register 0x20 will be set
Daw Instruction
This instruction (decimal adjust WREG) adjusts the 8-bit value in WREG resulting from the earlier addition
of 2 numbers (each in packed BCD format).
▪ BCD (binary coded decimal) numbers represent the decimal digits 0 through 9 with a 4-bit binary equivalent.
▪ Packed BCD numbers concatenate 2 BCD digits in one 8-bit register.
The carry bit is affected by the daw instruction and is used as a carry in for 16-bit packed BCD additions.
For instance, the addition of 0x08 to 0x04 results in 0x0C which is the correct value in binary form.
The decimal adjust instruction converts this result to 0x12. This is done as follows:
Call/recall and return Instructions
Call/recall and return Instructions
Call/recall and return Instructions
The retlw instruction operates like the return instruction.
Besides this, it sends back a constant value via the working register. It is similar to the C
return statement in the following function.
Program memory Rd/Wr Instructions
There are essentially two instructions dealing with program memory: tblrd and tblwt along
with their variants. The registers involved in reading from or writing to PM are TBLPTR and
Table Latch (or TABLAT) .
tblrd Instruction
This allows reading of data bytes stored side by side in program space by:
1. Loading the 21-bit register TBLPTRU:TBLPTRH:TBLPTRL with the address of the memory
location to be read.
The db (define byte) directive is used to store a set of consecutive table entries in PS as in:
In order to let TBLPTR point to address Table declared at address 0x1234 in PS, the following
sequence of instructions will do the job.
tblrd Instruction
2. Executing instruction tblrd which moves the contents of the location pointed to by TBLPTR
into TABLAT.
After execution:
TABLAT =
TBLPTR =
WREG =
tblrd Instruction
Note that the tblrd instruction does not affect the STATUS bits. Therefore, in order to check whether a
table entry is zero, use the following code:
Subroutines
• A subroutine is a separate block of code that can be called from multiple places in
a program. The control jumps to the subroutine, executes the code, and then
returns to the caller.
• It is memory-efficient because the code exists only once in memory. Each call to
the subroutine reuses this code.
• Has some overhead due to jumping to the subroutine and returning, which can
take additional clock cycles.
• Easier to debug and maintain. Changes made to a subroutine affect all calls to it,
and breakpoints can be set to test and step through the code.
Macros
• A macro is a preprocessor directive that allows you to define a block of code with
a name. When the macro is called, the assembler expands it inline by replacing
the macro name with its code, essentially copying the code wherever it’s called.
• Harder to debug because each instance of the macro is expanded inline, making it
challenging to identify where a particular instance may have caused an error.
Subroutines vs Macros
• Use subroutines for larger, reusable code blocks, especially where memory
efficiency matters.
• Use macros for smaller, frequently repeated tasks where execution speed is
paramount.