You are on page 1of 12

Tutorial 1:

Simple Coding Tasks with


AVR Assembly

1
• Copy contents of R1 to R6
R6 ← R1
MOV R6, R1
• Put zero in R10
R10 ← 0
CLR R10
• Subtract 100 from R20
R20 ← R20 - 100
SUBI R20, 100
• Subtract 100 from R2
R2 ← R2 - 100
LDI
SUBI R16, 100
R2, 100
SUB R2, R16
2
• Add 24 to R25
R25 ← R25 + 24
LDI R16, 24
ADD R25, R16
• Add 24 to pointer X
R27:R26 ← R27:R26 + 24
ADIW R27:R26, 24
• Set upper four bits of R17 without affecting the lower bits
R17 ← R17 ˅ 11110000
ORI R17, $F0
• Flip all bits of R3
R3 ← R3 ́
COM R3
3
• Divide by 2 the signed number stored in R5
R5 ← R5 ÷ 2
ASR R5
• Multiply Z pointer by 2
R31:R30 ← R31:R30 * 2
LSL R30
ROL R31
• Flip the two upper bits of R3 and set the two lower bits
R0 ← ((R3 ⊕ 1100 0000b) ˅ 0000 0011b )
LDI R16, $C0
EOR R3, R16
LDI R16, $3
AND R3, R16
4
• Store contents of R8 into location 150H of SRAM
SRAM [150h] ← R8
STS $150, R8
• Load R17 with contents of memory location pointed by Y
R17 ← SRAM [Y]
LD R17, Y
• Increment the byte stored in memory location 100H
[100h] ← [100h] +1
LDS R0, $100
INC R0
STS $100, R0

5
• Store "70H" into SRAM memory location where R1:R0
points
[R1:R0] ← 70H
MOVW ZH:ZL, R1:R0
LDI R16, $70
ST Z, R16
• Add to pointer X the value of pointer Y
X←X+Y
ADD R26, R28
ADC R27, R29
• Store bit3 of R10 into T bit of SREG
T ← R10[3]
BST R10, 3

6
• Get into R0 a byte from port C
R0 ← PINC
IN R0, $13 ; 13H is the address of PINC
• Output 55H on port B
PORTB ← 55H
LDI R17, $55
OUT $18, R17 ; 18H is the address of PortB
• Multiply R0 by 3
R0 ← 3*R0
MOV R10, R0
ADD R0, R0
ADD R0, R10

7
• Configure all pins of port A as input pins.
CLR R0
OUT $1A, R0 ; 1AH is the address of DDRA
• Configure upper pins of port A (PA7-PA4) as inputs and
lower pins as outputs
LDI R16, $0F
OUT $1A, R0
• Disable all interrupts
CLI
• Configure INT4 to trigger interrupt on rising edge
(without affecting other INTx sources)
IN R16, $3A ; 3AH is the address of EICRB
ORI R16, $03
OUT $3A, R16 8
• Load program counter with 400H
JMP $400 ; PC ← 400H
• Add 60 to program counter
RJMP 60 ; PC = PC + 60H
• Go to address "THERE" if R5 ≥ 0.
TST R5 ; R5 = R5 ˅ R5 (to update SREG)
BRGE THERE ; if R5 ≥ 0 (S = 0) then jump to THERE
• Clear R18 if it is greater than or equal 20
CPI R18, 20
BRLT NEXT
CLR R18
NEXT:

9
• Continually check the status of EEWE bit in EECR
register until it becomes zero, then clear R10
WAIT: SBIC $1C, 2 ; skip one instruction if bit is cleared
RJMP WAIT
CLR R10
• If (R1 < 0) then set all bits of R2
TST R0
BRGE NEXT
SER R1
NEXT:
• Continually read port A as long as the read value is an
even number. Once an odd value is read, output it the
same value on port B.
WAIT: SBIC $10, 0
RJMP WAIT
IN R0, $10
10
OUT $15, R0
• Continually read and check the byte on port A. If it is
even input a new value, otherwise output the value on
port B. Use only data space only.
WAIT: SBIC $10, 0
RJMP WAIT
IN R0, $10
OUT $15, R0
• Repeat the previous problem using only data space.
WAIT: LDS R16, $30
ANDI R16, $01
BREQ WAIT
OUT $35, R16

11
Assignment
• Give equivalent instruction(s) to "PUSH R1“

• Load R0 with the byte on location $100 in


EEPROM

• Compare between problem 32 and problem 33


in terms of
– Time needed to execute the code (# of cycles)
– Size of executable code (# of words)

12

You might also like