Structures.asm

This page contains a set of macros for the MPLAB IDE Assembler (developed with version 7.21)
For the MicroChip PICmicro range of micro-controllers
They implement the basic structures for rapid development of programs.

The Structures

Here is a summary. I have thought long and hard about whether to include the 'while ... endwhile' and the 'repeat ... until' structures and I decided against it. I amalgamated them into one structure - 'do ... while ... loop'. If you don't like this then get in touch but I think it's better. Note: all the macro names have a following underscore to differentiate them from keywords.
if ... else ... endif do ... while ... loop while ... endwhile repeat ... until
      
if_ <Function>
  ...
 {elseif_ <Function>
  ...}
 {else_
  ...}
endif_
      
do_
  ...
 while_ <Function>
  ...
loop_
      
do_
 while_ <Function>
  ...
loop_
      
do_
  ...
 while_ <Function>
loop_
There is a bit more to it but first this ...

The Functions

Where you see <Function> I have defined several functions for comparison:
  ISequR  OpA, OpB
will evaluates Register OpA = Register OpB.
  ISlesL  OpA, 5
will evaluates Register OpA < Literal 5.
OpB must not be W and OpA must always be a register
I have so far been unable to auto-differentiate Registers from Literals so you'll have to make do with the different basic functions for now.
Here is a complete list
ISlesR, ISleqR, ISequR, ISgrtR, ISgeqR, ISneqR
ISlesL, ISleqL, ISequL, ISgrtL, ISgeqL, ISneqL
There are two others for bit testing which only accept a literal:
ISset  OpA,3
ISclr  OpA,3

Example:

      
if_      ISlesR OpA,OpB  ;if OpA<OpB
  movlw  d'10'           ; mov 10
  movwf  OpA             ; to OpA
endif

A new one: do_ ... while_ ... loop_

While loops (and repeat loops) usually consist of two internal parts: the increment of the loop and the function it performs.
I often find that I require the increment step before the while loop aswell as in it, for example:

get byte
while_ byte!=0
  output byte
  get byte
endw_
When 'get byte' is complex this requires duplication of code or a function call or perhaps a better programmer than myself.
Anyway, I have implemented the 'do_' to overcome this problem. This is the same code as before

do_
  get byte
while_ byte!=0
  output byte
loop_
As I'm sure you can see a normal while loop is just the same but without any code inbetween do_ and while_ and a normal repeat loop is the same but without any code between the while_ and the loop_ (logic revered)
It's a small change to get your head around but I'm sure you'll appreciate the extra fexibility!
And one more point, 'do_ ... loop_' without the while_ will repeat forever,

Logic

After any structure which requires a function i.e. if_, elseif_, while_ you can have further logic.
The logic does not implement different operator presidence but still gives a great boost to flexibility
For example:
 

;if (OpA<OpB || OpA==15) && (PORTA,0)=1

if_      ISlesR OpA,OpB  ;if OpA<OpB
 or_     ISequL OpA,15   ; or OpA=15
 and_    ISset  PORTA,0  ;  and bit PORTA,0 =1
  movlw  d'10'           ; mov 10
  movwf  OpA             ; to OpA
endif_
The implemented logic functions are:
and_, or_

Extensions

It is simple to write your own comparison macro's.
Here is the basic code for ISequL

ISequL macro  OpA,OpB
       movf   OpA,W
       subwf  OpB,W
       btfss  STATUS,Z
       endm
Points to note are:
Only 1 or 2 parameters can be passed to the macro.
and the final instruction must be 'skip next' when true.

Limitations

All structures can be nested as much as you want.
It is only limited by the number of labels the assembler permits.
Don't use labels beinging gt and ending in a number - my macros do.
I have only been using PIC's for about 2 months now but as I learn more, I may improve things.
W and STATUS are corrupted, do not rely on them after macro calls!

Jake Spedding 11/2005

Use as you wish but keep my name in it please!
Visitor Count

Structures.asm

Readers Comments
05/03/2007: Jake
Feel free to add a comment
Add Comment