Você está na página 1de 40

Instrues em assembler p/ microcontroladores PIC

famlias 12, 14 e 16 bits


Sintaxe Descrio Microchip Operao equivalente
ADDLW k Add Literal and W W = W + k
ADDWF f,d Add W and f d = W + f (onde d pode ser W ou f)
ANDLW k AND Literal with W W = W AND k
ANDWF f,d AND W with f d = W AND f (onde d pode ser W ou f)
BCF f,b Bit Clear f f(b) = 0
BSF f,b Bit Set f f(b) = 1
BTFSC f,b Bit Test f, Skip if Clear f(b) = 0 ? Se , salta uma instruo
BTFSS f,b Bit Test f, skip if Set f(b) = 1 ? Se , salta uma instruo
CALL k Subroutine Call Chamada a uma subrotina no endereo k
CLRF f Clear f f = 0
CLRW Clear W Register W = 0
CLRWDT Clear Watchdog Timer Watchdog timer = 0
COMF f,d Complement f d = not f (onde d pode ser W ou f)
DECF f,d Decrement f d = f -1 (onde d pode ser W ou f)
DECFSZ f,d Decrement f, Skip if 0 d = f -1 (onde d pode ser W ou f) se d = 0 salta
Sintaxe Descrio Microchip Operao equivalente
GOTO k Go to address salta para o endereo k
INCF f,d Increment f d = f +1 (onde d pode ser W ou f)
INCFSZ f,d Increment f, Skip if 0 d = f +1 (onde d pode ser W ou f) se d = 0 salta
IORLW k Inclusive OR Literal with W W = W OR k
IORWF f,d Inclusive OR W with f d = f OR W (onde d pode ser W ou f)
MOVLW k Move literal to W W = k
MOVF f,d Move f d = f (onde d pode ser W ou f)
MOVWF f Move W to f f = W
NOP No Operation Nenhuma operao
OPTION Load Option Register OPTION = W
RETFIE Return from Interrupt Retorna de uma interrupt handler
RETLW k Return Literal to W Retorna de uma subrotina com W = k
RETURN Return from Subroutine Retorna de uma subrotina
RLF f,d Rotale Left f through Carry d = f << 1 (onde d pode ser W ou f)
RRF f,d Rotale Right f through Carry d = f >> 1 (onde d pode ser W o f)
Sintaxe Descrio Microchip Operao equivalente
SLEEP Go into Standby Mode Coloca o PIC em standby
SUBLW k Subtract W from Literal W = k - W
SUBWF f,d Subtract W from f d = f - W (onde d pode ser W ou f)
SWAPF f Swap f f = Swap do bit 0123 com 4567 de f
TRIS f Load TRIS Register TRIS di f = W
XORLW k Exclusive OR Literal with W W = W XOR k
XORWF f,d Exclusive OR W with f d = f XOR W (onde d pode ser W ou f)
3
Instrues - MOVLW
MOVLW Carregar literal k em W

Sintaxe: MOVLW k
Operandos: 0 k 255
Operao: (k) ==> (W)
Flags afectadas: nenhum
Descrio: O literal k passa ao registo W.

Exemplo: MOVLW 0x5A


4
Instrues - ADDLW
ADDLW Soma um literal a W

Sintaxe: [label] ADDLW k
Operandos: 0 k 255
Operao: : (W) + (k)==> (W)
Flags afectadas: C, DC, Z (flags do registro STATUS)
Descrio: Soma o contedo do registo W e k, guardando o resultado em W.


Exemplo:
Antes: W = .5 (Equivale a 10 em decimal)

ADDLW .10

Depois: W = .15


5
Instrues - ADDWF
Sintaxe: ADDWF f,d
Operao: (W) + (f) ==> (salva resultado no destino)
Flags afectadas: C, DC, Z
Descrio: Soma o contedo do registo W com o registo f. Se d 0, o
resultado armazena-se em W, Se d 1 armazena-se em f.

Exemplo:
Antes: W = .5, REG = .10

ADDWF REG,1

Depois: W = .5, REG = .15
6
Instrues - ANDLW
Sintaxe: ANDLW k
Operandos: 0 k 255
Operao: : (W) AND (k)==> (W)
Flags afectadas: Z

Descrio: Realiza a operao lgica AND entre o contedo do registo W e k,
guardando o resultado em W.

Exemplo: (FICA MAIS FCIL VIZUALIZAR A OPERAO COM OS VALORES EM BINRIO.

Antes: W = B00011001
ANDLW B11110011
Depois: W = B00010001
7
Instrues - BCF
Sintaxe: BCF f,b
Operandos: 0 f 127, 0 b 7
Flags afectadas: nenhum

Descrio: Zera o bit b do registo f

Exemplo:

Antes: REG = B11011001
BCF REG,7
Depois: REG = B01011001
8
Instrues - BSF
Sintaxe: BSF f,b
Operandos: 0 f 127, 0 b 7
Flags afectadas: nenhum

Descrio: Seta em 1 o bit b do registo f

Exemplo:

Antes: REG = B01011001
BCF REG,7
Depois: REG = B11011001
9
Instrues - BTFSC
BTFSC Testa o bit e salta se for 0

Sintaxe: BTFSC f,d
Operandos: d [0,1], 0 f 127
Operao: Salto Se (f<b>) = 0
Flags afectadas: nenhum

Descrio: Se o bit b do registo f 0, salta uma instruo e continua com a
execuo. Em caso de salto, ocupar dois ciclos de relgio.

Exemplo1:
Antes: REG = B11011001
W = .5

BTFSC REG,6
ADDLW .10
.
.
.

Depois: W = .15
____________________________________________

Exemplo2:
Antes: REG = B10011001
W = .5

BTFSC REG,6
ADDLW .10
.
.
.

Depois: W = .5
10
Instrues - BTFSS
BTFSS Testa o bit e salta se for 1

Sintaxe: BTFSS f,d
Operandos: d [0,1], 0 f 127
Operao: Salto Se (f<b>) = 1
Flags afectadas: nenhum

Descrio: Se o bit b do registo f 1, salta uma instruo e continua com a
execuo. Em caso de salto, ocupar dois ciclos de relgio.

Exemplo1:
Antes: REG = B10011001
W = .5

BTFSS REG,6
ADDLW .10
.
.
.

Depois: W = .15
____________________________________________

Exemplo2:
Antes: REG = B11011001
W = .5

BTFSS REG,6
ADDLW .10
.
.
.

Depois: W = .5
11
Instrues - CALL
CALL Salta para subrotina
Sintaxe: CALL k
Operandos: 0 k 2047
Operao: PC ==> pilha; k ==> PC
Flags afectadas: nenhum
Descrio: Salta para uma subrotina. Retorna da subrotina p/ instruo seguinte
chamada CALL quando na subrotina for encontrada a instruo RETURN.

Exemplo:
.
.
.
CALL SUBROT1
.
.
.
SUBROT1
.
.
.
RETURN
12
Instrues - CLRF
CLRF Apaga um registo
Flags afectadas: Z
Descrio: O registo f carregado com 0x00. A flag Z setado em 1.
Exemplo:
Antes: REG = 0x5A

CLRF REG

Depois: REG = 0x00,
bit Z = 1
Relembrando:
13
Flags:
Zero (Z) Fica a 1 quando o resultado 0.
Carry (C) Fica a 1 quando o resultado da operao maior do que 255.
Digit Carry (DC) Fica a 1 quando os 4 primeiros bits (menos significativo) maior do
que 15 aps uma operao aritmtica (adio ou subtrao)
Instrues - CLRW
CLRW Apaga o registo W

Sintaxe: CLRW
Operandos: nenhum
Operao: 0x00 ==> W, 1 ==> Z
Flags afectadas: Z
Descrio: O registo de trabalho W carregado com 0x00. A flag Z
activada.

Exemplo: CLRW

Antes: W = 0x5A
Depois: W = 0x00, Z = 1
14
Instrues - CLRWDT
CLRWDT Apaga o registro WDT
Sintaxe: CLRWDT
Operandos: nenhum
Operao: 0x00 ==> WDT, 1 ==> /TO
1 ==> /PD
Flags afectadas: /TO, /PD
Descrio: Esta instruo apaga tanto o WDT como o seu preescaler. Os bits
/TO e /PD do registo de estado so colocados a 1.
Exemplo:
CLRWDT
Depois: Contador WDT = 0,
Preescales WDT = 0,
/TO = 1, /PD = 1
Relembrando:


15
Time-Out (TO) Setado em 1 quando instruo CLRWDT executada, em 0
quando o registro WDT estoura (fica > 255).
Power-Down (PD) Setado em 1 quando a instruo CLRWDT executada ou
quando o PIC ligado. Fica setado em 0 quando comando SLEEP executado.
Instrues - COMF
COMF Complemento de f

Sintaxe: COMF f,d
Operandos: d [0,1], 0 f 127
Flags afectadas: Z
Descrio: O registo f complementado. A flag Z activada se o resultado
0. Se d 0 o resultado armazenado em W. Se d 1 armazenado em f.

Exemplo:
Antes: REG = B11111001

COMF REG,1

Depois: REG = B11111001

16
Instrues - DECF
DECF Decremento de f

Sintaxe: DECF f,d
Operandos: d [0,1], 0 f 127
Flags afectadas: Z
Descrio: Decrementa contedo de f. Se d 0, o resultado armazenado em
W, Se d 1 armazenado em f.

Exemplo:
Antes: CONT = 0x01, Z = 0

DECF CONT,1

Depois: CONT = 0x00, Z = 1

17
Instrues - DECFSZ
DECFSZ Decremento e salta se 0

Sintaxe: DECFSZ f,d
Operandos: d [0,1], 0 f 127
Operao: (f) -1 ==> d; Salto Se resultado for 0
Flags afectadas: nenhum
Descrio: Decrementa o contedo do registo f. Se d 0, o resultado
armazenado em W. Se d 1 armazenado em f. Se o resultado 0 salta
uma instruo e ocuparia 2 ciclos.
Exemplo1:
Antes: REG = .2 W = .5 Z = 0
DECFSZ REG,1
ADDLW .10

Depois: REG = .1 W = .15 Z = 0
__________________________________________________
Exemplo2:
Antes: REG = .1 W = .5 Z = 0

DECFSZ REG,1
ADDLW .10

Depois: REG = .0 W = .5 Z = 1
18
Instrues - GOTO
GOTO Salto incondicional
Sintaxe: GOTO k
Operandos: 0 k 2047
Flags afectadas: nenhum
Descrio: Trata-se de um salto incondicional. Desvia a execuo para o
local do k. Ocupa 2 ciclos de relgio.
Exemplo:
INICIO
.
GOTO LOCAL1
.
.
LOCAL1
.
GOTO LOCAL2
.
.
LOCAL2
.
GOTO INICIO
.
.

19
Instrues - INCF
INCF Incremento de f
Sintaxe: INCF f,d
Operandos: d [0,1], 0 f 127
Operao: : (f ) + 1 ==> (dest)
Flags afectadas: Z

Descrio: Incrementa o contedo de f. Se d 0, o resultado armazenado
em W. Se d 1 armazenado em f.

Exemplo:
Antes: CONT = .255 Z = 0

INCF CONT,1

Depois: CONT = .0 Z = 1

20
Instrues - INCFSZ
INCREMENTA e salta se 0

Sintaxe: INCFSZ f,d
Operandos: d [0,1], 0 f 127
Operao: (f) +1 ==> d; Salto Se resultado for 0
Flags afectadas: nenhum
Descrio: Incrementa o contedo do registo f. Se d 0, o resultado
armazenado em W. Se d 1 armazenado em f. Se o resultado 0 salta
uma instruo, se isto ocorrer levar 2 ciclos a instruo.
Exemplo1:
Antes: REG = .254 W = .5 Z = 0
INCFSZ REG,1
ADDLW .10

Depois: REG = .255 W = .15 Z = 0
___________________________________________________________
Exemplo2:
Antes: REG = .255 W = .5 Z = 0

INCFSZ REG,1
ADDLW .10

Depois: REG = .0 W = .5 Z = 1
21
Instrues - IORLW
22
Sintaxe: IORLW k
Operandos: 0 k 255
Operao: : (W) OR (k)==> (W)
Flags afectadas: Z

Descrio: Realiza a operao lgica OR entre o contedo do registo W e k,
guardando o resultado em W.

Exemplo: (FICA MAIS FCIL VIZUALIZAR A OPERAO COM OS VALORES EM BINRIO.

Antes: W = B00011001
IORLW B11110011
Depois: W = B11111011
Instrues - IORWF
23
Sintaxe: IORWF f
Operandos: 0 f 127
Operao: : (W) OR (f)==> d
Flags afectadas: Z

Descrio: Realiza a operao lgica OR entre o contedo do registo W e f.
Se d 0, o resultado armazena-se em W, Se d 1 armazena-se em f.

Exemplo: (FICA MAIS FCIL VIZUALIZAR A OPERAO COM OS VALORES EM BINRIO.

Antes: W = B00011001
REG = B11110011

IORLW REG,1

Depois: W = B00011001
REG = B11111011
Instrues - MOVF
MOVF Mover f para W
Sintaxe: MOVF f,d
Operandos: d [0,1], 0 f 127
Operao: (f) ==> d
Flags afectadas: Z
Descrio: O contedo do registo f movido para o destino dependendo de d.
Se d 0, o resultado armazenado em W. Se d 1 armazenado em f.
Quando d 1 permite verificar o registo, j que afeta O bit Z.

Exemplo1:
Antes: REG = .254 Z = 0

MOVF REG,0

Depois: REG = .254 W = .254 Z = 0
____________________________________________________
Exemplo2:
Antes: REG = .0 Z = 0

MOVF REG,1

Depois: REG = .0 Z = 1

24
Instrues - MOVWF
MOVWF Mover W para f

Sintaxe: MOVWF f
Operandos: 0 f 127
Operao: W ==> (f)
Flags afectadas: nenhum
Descrio: O contedo do registo W passa para o registo f.
Exemplo:
Antes: REG = 0xFF, W = 0x4F

MOVWF REG

Depois: REG = 0x4F, W = 0x4F
25
Instrues - NOP
NOP No operao
Sintaxe: NOP
Operandos: nenhum
Operao: No operar
Flags afectadas: nenhum
Descrio: No realiza nenhuma operao. Na realidade consome um ciclo de
instruo sem fazer nada.

Exemplo: NOP

26
Instrues - RETFIE
RETFIE regresso de interrupo

Sintaxe: RETFIE
Operandos: nenhum
Operao: 1 ==> GIE;
Flags afectadas: nenhum

Descrio: o PC carregado com o contedo de cima da pilha (TOS): direco
de regresso. Consome 2 ciclos. As interrupes voltam a ser habilitadas.
Exemplo: RETFIE

Depois: GIE = 1 (BIT 7 Do registro INTCON)
ESTE BIT SETADO EM 1, LIGA AS INTERRUPES HABILITADAS
27
Instrues - RETLW
RETLW regresso de uma subrotina, com o valor em W

Sintaxe: RETLW k
Operandos: 0 k 255
Operao: : (k)==> (W)
Flags afectadas: nenhum

Descrio: O registo W carregado com a constante k. Consome 2 ciclos.
Exemplo:
RETLW 0x37

Depois: Regressa para a instruo seguinte a chamada CALL desta subrotina
com W = 0x37.


28
Instrues - RETURN
RETURN regresso de rotina

Sintaxe: RETURN
Operandos: nenhum
Flags afectadas: nenhum
Descrio: O PC carregado com o contedo de cima da pilha (TOS): direco
de regresso. Consome 2 ciclos.

Exemplo: RETURN

Depois: Regressa para a instruo seguinte a chamada CALL desta subrotina

29
Instrues - RLF
RLF roda os bits de f para a esquerda

Sintaxe: RLF f,d
Operandos: d [0,1], 0 f 127
Operao: Rotao a esquerda
Flags afectadas: C

Descrio: O contedo de f rodado para a esquerda. O bit de maior peso de
f passa para o carry (C), e o carry colocado no de menor peso. Se d
0, o resultado armazenado em W. Se d 1 armazenado em f.

Exemplo:
Antes: REG = 1110 0110, C = 0

RLF REG,0

Depois: REG = 1110 0110,
W = 1100 1100, C = 1

30
Instrues - RRF
RLF roda os bits de f para a direita

Sintaxe: RRF f,d
Operandos: d [0,1], 0 f 127
Operao: Rotao a direita
Flags afectadas: C

Descrio: O contedo de f rodado para a direita. O bit de menor peso de
f passa para o carry (C), e o carry colocado no de maior peso. Se d
0, o resultado armazenado em W. Se d 1 armazenado em f.

Exemplo:
Antes: REG = 1110 0101 C = 0

RRF REG,0

Depois: REG = 1110 0101
W = 0111 0010 C = 1

31
Instrues - SLEEP

SLEEP Modo baixo consumo
Sintaxe: [label] SLEEP
Operandos: nenhum
Operao: 0x00==>WDT, 1 ==> / TO
0 ==> WDT Preescaler, 0 ==> / PD
Flags afectadas: / PD, / TO
Descrio: O bit de energia colocado a 0, e a 1 o de descanso. O WDT e o
seu preescaler so apagados. o micro pra o oscilador, ficando
adormecido.

Exemplo: SLEEP

Depois:
Preescales WDT = 0, /TO = 1, /PD = 1

32
Instrues - SUBLW
SUBLW Subtrai W ao literal

Sintaxe: SUBLW k
Operandos: 0 k 255
Operao: ( k ) - (W) ==> (W)
Flags afectadas: Z, C, DC
Descrio: Mediante o mtodo do complemento para dois o contedo de W
subtrado ao literal. O resultado armazenado em W.

Exemplos:

Antes:W=.1, C=?. SUBLW .2 Depois: W=1, C=1
Antes:W=.2, C=?. SUBLW .2 Depois: W=0, C=1
Antes:W=.3, C=?. SUBLW .2 Depois:W=.255,C=0 (o resultado negativo)

33
Instrues - SUBWF
SUBWF Subtrai W ao f
Sintaxe: SUBWF f,d
Operandos: d [0,1], 0 f 127
Operao: ( f ) - (W )==> (d)
Flags afectadas: C, DC, Z
Descrio: Mediante o mtodo do complemento para dois o contedo de W
subtrado ao de f. Se d 0, o resultado armazenado em W. Se d 1
armazenado em f.

Exemplos:

Antes: REG = .3, W = .2, C = ?
SUBWF REG,1
Depois:REG = .1, W = .2, C=1


Antes: REG = .2, W = .2, C = ?
SUBWF REG,1
Depois:REG = .0, W = .2, C= 1


Antes: REG = .1, W= .2, C= ?
SUBWF REG,1
Depois:REG = .255 W= .2, C= 0 (Resultado negativo)

34
Instrues - SWAPF
SWAPF Troca nibbles de f

Sintaxe: SWAPF f,d
Operandos: d [0,1], 0 f 127
Flags afectadas: nenhum

Descrio: Os 4 bits de maior peso e os 4 de menor so trocados. Se d 0,
o resultado armazenado em W. Se d 1 armazenado em f.

Exemplo:

Antes: REG = 1110 0101

SWAPF REG,0

Depois: REG = 1110 0101

W = 0101 1110
35
Instrues - XORLW
XORLW W XOR literal
Sintaxe: XORLW k
Operandos: 0 k 255
Operao: (W) XOR (k)==> (W)
Flags afectadas: Z
Descrio: Realiza a operao lgica XOR entre o contedo do registo W e k,
guardando o resultado em W.

Exemplo: XORLW 0xAF

Antes: W = 0xB5
Depois: W = 0x1A
36
Instrues - XORLW
XORWF W XOR F

Sintaxe: [label] XORWF f,d
Operandos: d [0,1], 0 f 127
Operao: (W) XOR (f) ==> (dest)
Flags afectadas: Z
Descrio: Realiza a operao lgica XOR entre os registos W e f. Se d 0,
o resultado armazenado em W. Se d 1 armazenado em f.

Exemplo: : XORWF REG,0

Antes: W = 0xB5, REG = 0xAF
Depois: W = 0xB5, REG = 0x1A

37
Exerccio:
38
1) Qual o tipo de oscilador usado e qual o valor de
freqncia o circuito oscilador abaixo est oferecendo
ao PIC utilizar?
Resposta:

;* * * * * * * * * * * * * * * * * * * * * * *
;* EX2 titulo do Firmware *
;*------------------------------------------*
#INCLUDE <P16F628A.INC> ;ARQUIVO PADRO MICROCHIP PARA 16F628A

__CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_on & _RC_OSC_CLKOUT

;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;* VARIVEIS *
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
CBLOCK 0x20 ;ENDEREO INICIAL DA MEMRIA DE USUARIO
V1 ; VARIAVEL UTILIZADA NA(EM)
V2 ; VARIAVEL UTILIZADA NA(EM)
N ; VARIAVEL UTILIZADA NA(EM)

ENDC ;FIM DO BLOCO DE MEMRIA
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
ORG 0x00 ;ENDEREO INICIAL DE PROCESSAMENTO
GOTO INICIO

ORG 0x04 ;ENDEREO INICIAL DA INTERRUPO
RETFIE ;RETORNA DA INTERRUPO

;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;* INICIO DO PROGRAMA *
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
INICIO

39
2) Se o firmware abaixo for executado no circuito do exerccio 1,
qual ser o resultado?
__________________________________________________

__________________________________________________
CLRF PORTB ;
BSF STATUS,RP0 ;
CLRF TRISB ;
MOVLW 0AH
MOVWF OPTREG ;
BCF STATUS,RP0 ;
INCF PORTB,F ;
BCF STATUS,CARRY ;
LEFT SLEEP ;
RLF PORTB,F ;
BTFSS PORTB,MSB ;
GOTO LEFT ;
RIGHT SLEEP ;
RRF PORTB,F ;
BTFSS PORTB,0 ;
GOTO RIGHT ;
GOTO LEFT ;
;
END
40

Você também pode gostar