Practical Applications

Practical applications

Comparing two registers (if they are equal)

Let’s say we need comparing two register and look if they are equal or not. It is C code is like below

if (a==b) {…}

To implement same structure in PIC microcontroller assembly, we use STATUS register’s zero flag.

MOVF a,W ;Move content of ‘a’ to ‘W’(accumulator)
SUBWF b,W ;Subtract ‘W’(contains ‘a’s content) from ‘b’
BTFSC STATUS,Z ;If zero flag is reset(0) skip next ins.
… ;STATUS.Z is set. Two registers are equal.
— ;Continue (This instruction will be implemented in both cases)

The zero flag will be set(1), if the result of the SUBWF is zero. By using this method, we can find out if two registers are equal.

Some microcontrollers have direct instruction for comparing. See instruction set for this instruction(CPFSEQ).

Comparing two registers (the greater one)

In this case, we need to find out if one register is greater than the other. It is C code is like below

if (a>b) {…}

Now, we need to use carry/borrow’ flag of the STATUS register.

MOVF a,W ;Move content of ‘a’ to W(accumulator)
SUBWF b,W ;Subtract ‘W’(contains ‘a’s content) from ‘b’
BTFSS STATUS,C ;If C flag is set(1), skip next instruction.
… ;STATUS.C is reset(0). a is bigger than b.
— ;Continue (This instruction will be implemented in both cases)

The carry/borrow’ flag (here we consider it borrow) will be reset(0) if the subtract operation needs borrow. In other words, SUBWF b,W will subtract W from b. Therefore, if W is bigger than b, b will need borrow. This will reset(0) Carry/borrow’ bit.

Some microcontrollers have direct instruction for comparing. See instruction set for this instruction (CPFSGT/CPFSLT).

BFTSS/BTFSC Usage (if conditions)

BTFSS or BTFSC will check a bit if it is set or reset then it will skip or not next instruction. This is if condition of assembly language. What if we need more instructions or else condition?

Moving a value to a register according to a bit

Let’s say we need to move ’00′ to RESULT(user defined) register if the result of the last calculation is ’0′, move ‘FF’ if the result of the above calculation is other than ’0′.

SUBLW h’15′ ;Any calculation. We used SUBLW, which subtracts W from literal (h’15′)
MOVLW h’00′ ;We put ’00′ to W (accumulator). ’00′ data is prepared
BTFSS STATUS,Z ;If the zero flag is set, skip next instruction.
MOVLW h’FF’ ;STATUS.Z is reset(0). That means the result of subtraction is other than zero.
;So, modify W with ‘FF’.
MOVWF RESULT ;Move W to RESULT register. W contains the value of last MOVLW command.

The above code is like if else statement. C demonstration of the code is like below

W = W – 15;
if (W == 0) {
W = 0×00;
} else {
W = 0xFF;

If else condition with multiple instructions in assembly

BTFSC/BTFSS instruction has the ability only skip one instruction. If we need more instruction according to the result of if condition, we can use GOTO instruction.

BTFSS PORTB,5 ;Any bit of any register
GOTO Label0 ;Jump to state 0
… ;instructions that belong
… ;to set(1) state

GOTO Continue ;Jump to continue
… ;instructions that belong
… ;to reset(0) state

… ;instructions after
… ;if condition

Labels can be put anywhere in the code. Labels are not instructions. They only point the address of the memory for compiling. GOTO instruction will branch labels and it takes two instruction cycle to be implemented.

The C demonstration is like below

if (PORTB.5==1) {
} else {
…state0 (Label0)

Toggling bits of a register

To toggle bits, we use XOR operation. The truth table of XOR :

0 0 0
0 1 1
1 0 1
0 0 0

That means if A and B are different, result will be 1, otherwise 0.

If we XOR two numbers like 11010011 and 00001111.

1101 1100

Here, second number is the mask for XOR operation. Only, the bits which are ’1′ in the masking number, will be toggled in the first number.

MOVLW b’00001111′ ;Masking number
XORWF PORTB,F ;First four bits of PORTB will be toggled

I will add more applications later. These were the general Microchip PIC assembly equivalents of higher-level languages.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Reddit
  • Add to favorites
  • Email
  • RSS

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>