Labels

[PIC]binary to BCD (3 digits)

; 8bit to BCD half-packed (3 digits)
; reference on PICList which is truely a good place for studing PIC assmbling
; course 3 of PIC MCU in YNU
;
; no CopyRight
; Mar,12 2009 edited by Seraph

include "p16f877a.inc"

hundreds equ 0x20
tens equ 0x21
ones equ 0x22


org 0
goto main
org 4
goto int_srv

int_srv:
main:
movlw 0xff
call bin2bcd
bin2bcd:

clrf tens
clrf hundreds
decf hundreds, f
decf tens, f
movwf ones

div10_tens
incf tens
movlw 0xa
subwf ones, f
btfsc STATUS, C
goto div10_tens
movlw 0xa
addwf ones, f

div10_hundreds
incf hundreds
movlw 0xa
subwf tens, f
btfsc STATUS, C
goto div10_hundreds
movlw 0xa
addwf tens, f
;transmit them through 'call put232' or 'call putLCD'
;
ret
end

[PIC]PIC instruction set summary

PIC Instruction Set Summary

Notation:
  • fr: one of the 80 memory ram positions implemented as 8 bit registers (File Register Set or FRS) (0 =< fr <= 4f). The first 12 are special purpose registers (SFR) and the other 68 are general purpose registers(GPR).
  • W: 8 bit accumulator of the Arithmetic Logic Unit (ALU)
  • d: mnemonic for the destination of one operation, which can be 1 ( a file register f) or 0 (the accumulator w)
  • k: an 8 bit literal
  • b: 3 bit literal identifying one bit of a byte (0: least significant bit, 7: most significant bit). Example: f(7)
  • addr: 11 bit literal representing an instruction address
  • C: Carry bit = STATUS(0)
  • Z: Zero bit = STATUS(2)
  • DC: Digit Carry bit = STATUS(1)
  • opr: mnemonic for one of the following binary operations:
    • add - addition
    • sub - subtraction
    • and - logical and
    • ior - logical or
    • xor - exclusive or

     

    1. Copy value from/to file register or literal to/from w
    Mnemonic Description Status Function Obs
    movf fr, d Move file register
    Z
    fr => d see macro: mov f, d
    movwf   fr Move W to file register   w => fr macro: mov w, fr
    movlw   k Move literal to W   k => W macro: movl k, w

     

    2. Logic / arithmetic instructions with a file register and w
    Mnemonic Description Status Function Obs
    oprwf   fr,d logic / arithmetic operation
    with a file register and W
    Z(all),C,DC
    (add,sub)
    fr opr W => d only add, sub affect C,DC
    macro: opr fr, d

     

    3. Logic / arithmetic instructions with a literal and w
    Mnemonic Description Status Function Obs
    oprlw   k logic / arimetic operation
    with a literal and W
    Z(all),C,DC
    (add,sub)
    k opr W => W only add, sub affect C,DC
    macro:oprl k,w

     

    4. One operand logic / arithmetic instructions
    Mnemonic Description Status Function Obs
    clrw   Clear accumulator W
    Z
    0 => W  
    clrf   fr Clear file register fr
    Z
    0 => fr  
    decf  fr,d Decrement file register fr
    Z
    fr - 1 => d  
    incf  fr, d Increment file register fr
    Z
    fr + 1 => d  
    comf fr,d 1's complement file register fr
    Z
    not fr => d  
    rlf  fr, d Rotate file register fr left thru C
    C
    C <= fr(7), fr(i) <= fr(i-1), fr(0) <= C  
    rrf   fr, d Rotate file register fr right thru C
    C
    C => fr(7), fr(i) => fr(i-1), fr(0) => C  
    bcf   fr, b Bit clear on file register fr   0 => fr(b)  
    bsf   fr, b Bit set on file register fr   1 => fr(b)  
    swapf  fr,d swap halves of fr   (fr(0:3) <=> fr(4:7)) => d  
    nop   No operation      

     

    5. Branch, Skip and Call instructions
    Mnemonic Description Status Function Obs
    goto    addr branch to addr   addr => PC(0:10)  
    call    addr call routine at addr   PC => TOS
    addr => PC(0:10)
     
    decfsz fr,d Decrement fr, skip if zero   fr - 1 => d, skip if 0  
    incfsz fr,d Increment fr, skip next instr if zero   fr + 1 => d, skip next instr if 0  
    btfsc fr,b Bit test fr, skip if clear   skip next instr if fr(b) =0  
    btfss fr,b Bit test fr, skip if set   skip next instr if fr(b)=1  
    return   return from subroutine   TOS => PC  
    retlw   k return with literal in w   k =>w,   TOS => PC  
    retfie   return from interrupt   TOS => PC,    1 => GIE  

     

    6. Buit-in macros for commonly used logic / arithmetic operations
    Mnemonic Description Status Function Obs
    addcf fr, d Add carry to fr
    Z
    btfsc   3, 0   incf  f,d  
    subcf fr, d Subtract carry from fr
    Z
    btfsc   3, 0   decf  fr,d  
    negf fr, d Negate file register fr
    Z
    comf   fr, 1   incf  fr,d  
    b   addr Branch to addr   goto    adddr  
    bz   addr Branch on Zero to addr   btfsc   3, 2    goto addr  
    bnz   addr Branch on No Zero to addr   btfss   3, 2    goto addr  
    bc   addr Branch on Carry to addr   btfsc   3, 0    goto addr  
    bnc   addr Branch on No Carry to addr   btfss   3, 0    goto addr  
    skpc Skip on Carry   btfss   3, 0     
    skpnc Skip on No Carry   btfsc   3, 0       
    skpz Skip on Zero   btfss   3, 2     
    skpnz Skip on No Zero   btfsc   3, 2       
    clrz   Clear Zero flag   bcf    3, 2  
    setz   Set Zero flag   bsf    3, 2    
    clrc   Clear Carry flag   bcf    3, 0    
    setc   Set Carry flag   bsf    3, 0      
    tstf   fr Test file register fr
    Z
    movf  fr, f  
    xchg fr1, fr2 Exchange file regs fr1 & fr2
    Z
    movf fr2, 0 xorwf  fr1,1
    xorwf fr1, 0  xorwf  fr1, 1
    movwf fr2
    Macro uses only w
    as temporary variable
    See:mymacros.inc
    decbnz fr,addr Decrement file register,
    if # 0 branch to addr
      decfsz  fr
    goto addr
    In:mymacros.inc

    Copyright © C閘io Guimar鉫s
    Institute of Computing - Unicamp - Brazil

    Last Update: Nov 29, 2001 by celio

[PIC]exchange between two file registers

; exchg.inc
; exchange the contents bettwen two file register


;move from file_register to file_register
_movff macro _movff_src, _movff_dst
movf _movff_src, w
movwf _movff_dst
endm


;exchange
_exchg macro _exchg_a, _exchg_b
_movff _exchg_a, tmp
_movff _exchg_b, _exchg_a
_movff tmp, _exchg_b
endm

[PIC]16-bit counter