You are on page 1of 17

AVR

ATMEL
AVR
Assembler

.
ATMEL AVR Studio, ATMEL AVR In-Circuit
Emulator. ,
AVR.
, .
Microsoft Windows 3.11, Microsoft Windows95 Microsoft Windows NT.
MS-DOS.
AVR ,

.

, , .
, , .
120 .
,
. , ,
.
:
[:] [] []
[:] [] []


:
; []
. (;)
. , .
:
label: .EQU var1=100 ; var1 100 ( )
.EQU var2=200 ; var2 200

test: rjmp test ; ( )


;

;
, ,
.
AVR
AVR, AVR
Data Book.



ADD Rd,Rr Rd = Rd + Rr Z,C,N,V,H,S 1
ADC Rd,Rr Rd = Rd + Rr + C Z,C,N,V,H,S 1
SUB Rd,Rr Rd = Rd - Rr Z,C,N,V,H,S 1
SUBI Rd,K8 Rd = Rd - K8 Z,C,N,V,H,S 1
SBC Rd,Rr Rd = Rd - Rr - C Z,C,N,V,H,S 1
SBCI Rd,K8 Rd = Rd - K8 - C Z,C,N,V,H,S 1
AND Rd,Rr Rd = Rd Rr Z,N,V,S 1
ANDI Rd,K8 Rd = Rd K8 Z,N,V,S 1
OR Rd,Rr Rd = Rd V Rr Z,N,V,S 1
ORI Rd,K8 Rd = Rd V K8 Z,N,V,S 1
EOR Rd,Rr Rd = Rd EOR Rr Z,N,V,S 1
COM Rd Rd = $FF - Rd Z,C,N,V,S 1
NEG Rd (. ) Rd = $00 - Rd Z,C,N,V,H,S 1
SBR Rd,K8 () Rd = Rd V K8 Z,C,N,V,S 1
CBR Rd,K8 () Rd = Rd ($FF - K8) Z,C,N,V,S 1
INC Rd Rd = Rd + 1 Z,N,V,S 1
DEC Rd Rd = Rd -1 Z,N,V,S 1
TST Rd Rd = Rd Rd Z,C,N,V,S 1
CLR Rd Rd = 0 Z,C,N,V,S 1
SER Rd Rd = $FF None 1
ADIW Rdl,K6 Rdh:Rdl = Rdh:Rdl + Z,C,N,V,S 2
K6
SBIW Rdl,K6 Rdh:Rdl = Rdh:Rdl - Z,C,N,V,S 2
K6
MUL Rd,Rr R1:R0 = Rd * Rr Z,C 2
MULS Rd,Rr R1:R0 = Rd * Rr Z,C 2
MULSU Rd,Rr R1:R0 = Rd * Rr Z,C 2
FMUL Rd,Rr R1:R0 = (Rd * Rr) << Z,C 2
1
FMULS Rd,Rr R1:R0 = (Rd *Rr) << Z,C 2
1
FMULSU Rd,Rr R1:R0 = (Rd * Rr) << Z,C 2
1



RJMP k PC = PC + k +1 None 2
IJMP (Z) PC = Z None 2
EIJMP STACK = PC+1, PC(15:0) = Z, None 2
(Z) PC(21:16) = EIND
JMP k PC = k None 3
RCALL k STACK = PC+1, PC = PC + k + 1 None 3/4*

ICALL (Z) STACK = PC+1, PC = Z None 3/4*
EICALL (Z) STACK = PC+1, PC(15:0) = Z, None 4*
PC(21:16) =EIND
CALL k STACK = PC+2, PC = k None 4/5*
RET PC = STACK None 4/5*
RETI PC = STACK I 4/5*
CPSE Rd,Rr , if (Rd ==Rr) PC = PC 2 or 3 None 1/2/3
CP Rd,Rr Rd -Rr Z,C,N,V,H,S 1
CPC Rd,Rr Rd - Rr - C Z,C,N,V,H,S 1
CPI Rd,K8 Rd - K Z,C,N,V,H,S 1
SBRC Rr,b if(Rr(b)==0) PC = PC + 2 or 3 None 1/2/3

SBRS Rr,b if(Rr(b)==1) PC = PC + 2 or 3 None 1/2/3

SBIC P,b if(I/O(P,b)==0) PC = PC + 2 or 3 None 1/2/3

SBIS P,b if(I/O(P,b)==1) PC = PC + 2 or 3 None 1/2/3

BRBC s,k SREG if(SREG(s)==0) PC = PC + k + 1 None 1/2
BRBS s,k SREG if(SREG(s)==1) PC = PC + k + 1 None 1/2

BREQ k if(Z==1) PC = PC + k + 1 None 1/2
BRNE k if(Z==0) PC = PC + k + 1 None 1/2
BRCS k if(C==1) PC = PC + k + 1 None 1/2
BRCC k if(C==0) PC = PC + k + 1 None 1/2
BRSH k if(C==0) PC = PC + k + 1 None 1/2
BRLO k if(C==1) PC = PC + k + 1 None 1/2
BRMI k if(N==1) PC = PC + k + 1 None 1/2
BRPL k if(N==0) PC = PC + k + 1 None 1/2
BRGE k ( if(S==0) PC = PC + k + 1 None 1/2
)
BRLT k ( ) if(S==1) PC = PC + k + 1 None 1/2
BRHS k if(H==1) PC = PC + k + 1 None 1/2

BRHC k if(H==0) PC = PC + k + 1 None 1/2

BRTS k T if(T==1) PC = PC + k + 1 None 1/2
BRTC k T if(T==0) PC = PC + k + 1 None 1/2
BRVS k if(V==1) PC = PC + k + 1 None 1/2

BRVC k if(V==0) PC = PC + k + 1 None 1/2

BRIE k if(I==1) PC = PC + k + 1 None 1/2

BRID k if(I==0) PC = PC + k + 1 None 1/2

*
, . CALL, ICALL, EICALL,
RCALL, RET RETI,
PC 16 (128KB ).
128KB , .


MOV Rd,Rr Rd = Rr None 1
MOVW Rd,Rr Rd+1:Rd = Rr+1:Rr, r,d None 1
even
LDI Rd,K8 Rd = K None 1
LDS Rd,k Rd = (k) None 2*
LD Rd,X Rd = (X) None 2*
LD Rd,X+ - Rd = (X), X=X+1 None 2*
LD Rd,-X - X=X-1, Rd = (X) None 2*
LD Rd,Y Rd = (Y) None 2*
LD Rd,Y+ - Rd = (Y), Y=Y+1 None 2*
LD Rd,-Y - Y=Y-1, Rd = (Y) None 2*
LDD Rd,Y+q Rd = (Y+q) None 2*
LD Rd,Z Rd = (Z) None 2*
LD Rd,Z+ - Rd = (Z), Z=Z+1 None 2*
LD Rd,-Z - Z=Z-1, Rd = (Z) None 2*
LDD Rd,Z+q Rd = (Z+q) None 2*
STS k,Rr (k) = Rr None 2*
ST X,Rr (X) = Rr None 2*
ST X+,Rr - (X) = Rr, X=X+1 None 2*
ST -X,Rr - X=X-1, (X)=Rr None 2*
ST Y,Rr (Y) = Rr None 2*
ST Y+,Rr - (Y) = Rr, Y=Y+1 None 2
ST -Y,Rr - Y=Y-1, (Y) = Rr None 2
ST Y+q,Rr (Y+q) = Rr None 2
ST Z,Rr (Z) = Rr None 2
ST Z+,Rr - (Z) = Rr, Z=Z+1 None 2
ST -Z,Rr - Z=Z-1, (Z) = Rr None 2
ST Z+q,Rr (Z+q) = Rr None 2
LPM R0 = (Z) None 3
LPM Rd,Z Rd = (Z) None 3
LPM Rd,Z+ - Rd = (Z), Z=Z+1 None 3

ELPM R0 = (RAMPZ:Z) None 3
ELPM Rd,Z Rd = (RAMPZ:Z) None 3
ELPM Rd,Z+ Rd = (RAMPZ:Z), Z = None 3
- Z+1
SPM (Z) = R1:R0 None -
ESPM (RAMPZ:Z) = R1:R0 None -
IN Rd,P Rd = P None 1
OUT P,Rr P = Rr None 1
PUSH Rr STACK = Rr None 2
POP Rd Rd = STACK None 2
*
, . LD, ST, LDD, STD, LDS,
STS, PUSH POP, .


LSL Rd Rd(n+1)=Rd(n), Rd(0)=0, C=Rd(7) Z,C,N,V,H,S 1
LSR Rd Rd(n)=Rd(n+1), Rd(7)=0, C=Rd(0) Z,C,N,V,S 1
ROL Rd C Rd(0)=C, Rd(n+1)=Rd(n), C=Rd(7) Z,C,N,V,H,S 1
ROR Rd C Rd(7)=C, Rd(n)=Rd(n+1), C=Rd(0) Z,C,N,V,S 1
ASR Rd Rd(n)=Rd(n+1), n=0,...,6 Z,C,N,V,S 1
SWAP Rd Rd(3..0) = Rd(7..4), Rd(7..4) = None 1
Rd(3..0)
BSET s SREG(s) = 1 SREG(s) 1
BCLR s SREG(s) = 0 SREG(s) 1
SBI P,b I/O(P,b) = 1 None 2
CBI P,b I/O(P,b) = 0 None 2
BST Rr,b T T = Rr(b) T 1
BLD Rd,b T Rd(b) = T None 1
SEC C =1 C 1
CLC C=0 C 1
SEN N=1 N 1

CLN N=0 N 1

SEZ Z=1 Z 1
CLZ Z=0 Z 1
SEI I=1 I 1
CLI I=0 I 1
SES S=1 S 1
CLN S=0 S 1
SEV V=1 V 1
CLV V=0 V 1
SET T T=1 T 1
CLT T T=0 T 1
SEH H=1 H 1

CLH H=0 H 1

NOP None 1
SLEEP ( None 1
)
WDR None 1
.
:
Rd: ( )
Rr:
b: (3 ),
s: (3 ),
P: (5-6 ),
K6; (6 ),
K8: (8 ),
k: ( ),
q: (6 ),
Rdl: R24, R26, R28, R30. ADIW SBIW
X,Y,Z: (X=R27:R26, Y=R29:R28, Z=R31:R30)

. .
,
, .. .


BYTE
CSEG
DB EEPROM
DEF
DEVICE
DSEG
DW EEPROM
ENDM, ENDMACRO
EQU
ESEG EEPROM
EXIT
INCLUDE
LIST
LISTMAC
MACRO
NOLIST
ORG
SET
.
BYTE -
BYTE .
, BYTE .
, .
( CSEG DSEG).
.
:
: .BYTE

:
.DSEG
var1: .BYTE 1 ; 1 var1
table: .BYTE tab_size ; tab_size

.CSEG
ldi r30,low(var1) ; Z
ldi r31,high(var1) ; Z
ld r1,Z ; VAR1 1

CSEG -
CSEG .
,
. .
, .
ORG .
CSEG .
:
.CSEG
:
.DSEG ;
vartab: .BYTE 4 ; 4

.CSEG ;
const: .DW 2 ; 0x0002
mov r1,r0 ;

DB - EEPROM
DB EEPROM.
, DB
. DB .
(CSEG) EEPROM (ESEG).
- .
(-128..255),
, ,
.
,
( - ),
,
, DB.
:
: .DB _

:
.CSEG
consts: .DB 0, 255, 0b01010101, -128, 0xaa

.ESEG
const2: .DB 1,2,3

DEF -
DEF .
.
.
.
:
.DEF _ =

:
.DEF temp=R16
.DEF ior=R0

.CSEG
ldi temp,0xf0 ; 0xf0 temp (R16)
in ior,0x3f ; SREG ior (R0)
eor temp,ior ; temp ior

DEVICE -
DEVICE .
,
, .
, , EEPROM
. ,
.
:
.DEVICE AT90S1200 |AT90S2313 | AT90S2323 | AT90S2333 | AT90S2343 | AT90S4414 | AT90S4433
| AT90S4434 | AT90S8515 | AT90S8534 | AT90S8535 | ATtiny11 | ATtiny12 | ATtiny22 |
ATmega603 | ATmega103
:
.DEVICE AT90S1200 ; AT90S1200

.CSEG
push r30 ;
; AT90S1200

DSEG -
DSEG .
, .
BYTE .
. ORG
. .
:
.DSEG

:
.DSEG ;
var1: .BYTE 1 ; 1 var1
table: .BYTE tab_size ; tab_size .

.CSEG
ldi r30,low(var1) ; Z
ldi r31,high(var1) ; Z
ld r1,Z ; var1 r1

DW - EEPROM
DW EEPROM.
, DW
. DW .
(CSEG) EEPROM (ESEG).
- .
(-32768..65535),
, ,
.
:
: .DW expressionlist

:
.CSEG
varlist: .DW 0, 0xffff, 0b1001110001010101, -32768, 65535

.ESEG
eevarlst: .DW 0,0xffff,10
ENDMACRO -
, .
MACRO.
:
.ENDMACRO

:
.MACRO SUBI16 ;
subi r16,low(@0) ;
sbci r17,high(@0) ;
.ENDMACRO

EQU -
EQU . .

.
:
.EQU =

:
.EQU io_offset = 0x23
.EQU porta = io_offset + 2

.CSEG ;
clr r2 ; r2
out porta,r2 ; A

ESEG - EEPROM
ESEG EEPROM.
EEPROM, .
EEPROM DB, DW . EEPROM
. ORG
EEPROM. .
:
.ESEG

:
.DSEG ;
var1: .BYTE 1 ; 1 var1
table: .BYTE tab_size ; tab_size .

.ESEG
eevar1: .DW 0xffff ; 1 EEPROM

EXIT -
EXIT .
(. INCLUDE),
INCLUDE. ,
.
:
.EXIT

:
.EXIT ;
INCLUDE -
INCLUDE ,
EXIT,
INCLUDE.
INCLUDE.
:
.INCLUDE "_"

:
; iodefs.asm:
.EQU sreg = 0x3f ;
.EQU sphigh = 0x3e ;
.EQU splow = 0x3d ;

; incdemo.asm
.INCLUDE iodefs.asm ;
in r0,sreg ;

LIST -
LIST .
, .
, NOLIST
.
:
.LIST

:
.NOLIST ;
.INCLUDE "macro.inc" ;
.INCLUDE "const.def" ;
.LIST ;

LISTMAC -
LISTMAC .
.
:
.LISTMAC

:
.MACRO MACX ;
add r0,@0 ;
eor r1,@1
.ENDMACRO ;

.LISTMAC ;
MACX r2,r1 ; ( )

MACRO -

MACRO .
. ,
. 10 , @0-
@9. .
ENDMACRO.
,
LISTMAC. +.

:
.MACRO

:
.MACRO SUBI16 ;
subi @1,low(@0) ; 0 1
sbci @2,high(@0) ; 0 2
.ENDMACRO ;

.CSEG ;
SUBI16 0x1234,r16,r17 ; 0x1234 r17:r16

NOLIST -
NOLIST .
, .
, .
LIST

:
.NOLIST

:
.NOLIST ;
.INCLUDE "macro.inc" ;
.INCLUDE "const.def" ;
.LIST ;

ORG -
ORG ,
. SRAM (),
, EEPROM EEPROM.
( )
. EEPROM
, 32 ( 0-31 ).
EEPROM - .
:
.ORG

:
.DSEG ;

.ORG 0x37 ; SRAM 0x37


variable: .BYTE 1 ; 0x37H

.CSEG
.ORG 0x10 ; 0x10
mov r0,r1 ; 0x10

SET -
SET .
. EQU
SET.
:
.SET =
:
.SET io_offset = 0x23
.SET porta = io_offset + 2

.CSEG ;
clr r2 ; 2
out porta,r2 ; A


,
. 32-.

:
( ).

SET

EQU

:
o ( ): 10, 255

o ( ): 0x0a, $0a, 0xff, $ff

o : 0b00001010, 0b11111111

o ( ): 010, 077

PC - (Programm Counter)


(
, ). ,
.

14 !
14 ~
14 -
13 *
13 /
12 +
12 -
11 <<
11 >>
10 <
10 <=
10 >
10 >=
9 ==
9 !=
8 &
7 ^
6 |
5 &&
4 ||

: !
: 1 0,
: 14
: ldi r16, !0xf0 ; r16 0x00

: ~
:
: 14
: ldi r16, ~0xf0 ; r16 0x0f

: -
:
: 14
: ldi r16,-2 ; -2(0xfe) r16

: *
:
: 13
: ldi r30, label*2

: /
:
: 13
: ldi r30, label/2

: +
:
: 12
: ldi r30, c1+c2

: -
:
: 12
: ldi r17, c1-c2

: <<
:
: 11
: ldi r17, 1<<bitmask ; r17 1 bitmask

: >>
:
: 11
: ldi r17, c1>>c2 ; r17 c1 c2

: <
: 1 ( ), 0

: 10
: ori r18, bitmask*(c1<c2)+1

: <=
: 1 (
), 0
: 10
: ori r18, bitmask*(c1<=c2)+1

: >
: 1 ( ), 0

: 10
: ori r18, bitmask*(c1>c2)+1

: >=
: 1 (
), 0
: 10
: ori r18, bitmask*(c1>=c2)+1

: ==
: 1 ( ), 0

: 9
: andi r19, bitmask*(c1==c2)+1

: !=
: 1 ( ), 0

: 9
: .SET flag = (c1!=c2) ; flag 1 0

: &
:
: 8
: ldi r18, High(c1&c2)

: ^
:
: 7
: ldi r18, Low(c1^c2)

: |
:
: 6
: ldi r18, Low(c1|c2)

: &&
: 1 , 0
: 5
: ldi r18, Low(c1&&c2)

: ||
: 1 , 0
: 4
: ldi r18, Low(c1||c2)


:
LOW()
HIGH()
BYTE2()
HIGHBYTE3()
BYTE4()
LWRD() 0-15
HWRD() 16-31
PAGE() 16-21
EXP2() 2 ()
LOG2() log2()


WAVRASM .
, 28 (
MS-Windows). ,
. .

.
. , ,
, . ,
.
,
, .
( ) .
, ( ),
.

Options.

"List-file extension"
, "Output-file extension"
. "Output file format" (
). ( AVR
Studio), , OBJ.
EEPROM EEP.
, ,
.
"Wrap relative jumps" "" .
4 (8 ),
(rjmp) (rcall) .
"Save before assemble"
( ) .
, ,
"Close all windows before exit".

Atmel, AVR Atmel Corporation

, ruslansh@i.com.ua