EE234 - Lec - 03
EE234 - Lec - 03
Lecture 3
02/25/2025 1
Example 3.1 Add the 16-bit numbers stored in data memory at
0x2000~0x2001 & 0x2010~0x2011 and store the sum in
0x2020~0x2021.
Solution:
Sol 1: Use address pointer Sol 2: Use Data Direct mode
ldi YL, 0 lds r0, 0x2000
ldi YH, 0x20 lds r1, 0x2010
ld r0, Y add r0, r1
ldd r1, Y+0x10 sts 0x2020, r0
add r0, r1 lds r0,0x2001
st Y+0x20, r0 lds r1,0x2011
ld r0, Y+1 adc r0, r1
ldd r1, Y+0x11 sts 0x2021, r0
adc r1, r0
st Y+0x21, r1
02/25/2025 2
Example 3.2 Add 55 to the 16-bit value stored in 0x2000~0x2001
Solution:
lds r24, 0x2000
lds r25, 0x2001
adiw r24, 55
sts 0x2000, r24
sts 0x2001, r25
02/25/2025 3
Example 3.3 Write an instruction sequence to add the 16-bit
number stored in program memory at 0x1050~0x1051 to the 16-bit
value stored in data memory at 0x2000~0x2001 and leave the sum
in data memory at 0x2000~0x2001.
Solution:
ldi ZL,0x50
ldi ZH,0x10
lpm r0, Z+
lpm r1, Z
lds r2,0x2000
add r2,r0
sts 0x2000,r2
lds r2,0x2001
adc r2,r1
sts 0x2001, r2
02/25/2025 4
Example 3.4 Write an instruction sequence to subtract 40 from the
16-bit value stored in data memory at 0x2010~0x2011.
Solution:
lds r24,0x2010 lds r16, 0x2010
lds r25,0x2011 subi r16, 40
sbiwr24, 40 sts 0x2010, r16
sts 0x2010, r24 lds r16, 0x2011
sts 0x2011, r25 sbci r16, 0
sts 0x2011, r16
02/25/2025 5
Types of Statements in an Assembly Program
Assembler directive
Instruction
Comment
Empty line
Components in an Assembly Program Statement
Label—optional
Opcode or assembler directive
Operands or Arguments (of directive)
Comment (optional)
02/25/202 6
5
Label Field
Start with a letter, ?, or _
Followed by 0 or more letters, digits, ?, $, or _
Terminated by a colon character “:”
Represent a location in data or program memory
loop: …
start: …
02/25/2025 7
Operation Field—Directive or Assembly Instruction Mnemonic
.def lpCnt = r19
.equ N = 40
add r20, r22
sub r0, r1
Operand Field—Directive Argument or Instruction Operands
.db 10, 11, 12, 13, 14, 15, 16, … ; argument to a
directive
adiw r24, 59
lds r0, 0x2050
02/25/2025 8
Comment Field
Single line—start with “;” or “//”
Multiple lines—start with “/*” and terminated with “*/”
Expression
Used to form a constant
low(expression) ldi r16, low(RAMEND)
high(expression) ldi r17, high(RAMEND)
out CPU_SPL, r16
out CPU_SPH, r17
02/25/2025 9
Format of Constants
Binary Constants
1. Add Prefix: B’01011001’, or b’01011001’ – also include single
quotes
2. Add Suffix: 1010B, 0101001b
ldi r17, b’11001011’
Octal Constants
3. Add Prefix: Q’123456’, q’1345’ – also include single quotes
4. Add Suffix: 1234q, 4567Q
02/25/2025 10
Decimal Constants
1. Add prefix: D’123456’, d’23456’
2. No suffix, no prefix: 1234, -10
Hexadecimal Constants
3. Add prefix: 0x12AB, h’3344’, H’DCBA’
4. Add suffix: 0136h, 04433H ; must be preceded by a 0.
02/25/2025 11
Assembler Directives
.byte expression
.byte 10 ; reserve 10 bytes in data memory
.cseg ; start a new code segment
.db expression ; define constant bytes in program memory
.db 1,2,3,4,5,6,7,8,9,10
.def symbol = reg ; use symbol to refer to a register
.def sumL = r20
.def sumH = r21
.def lpCnt = r22
02/25/2025 12
.equ symbol = expression ; use symbol to represent a
constant
.equ NN = 30
.equ ULIMIT = 110
.device device_name ; tell the assembler which device to
generate
; the machine code
.device atxmega128A1U
.dseg ; declare a data segment
.dw expression ; define 16-bit constant
.dw case1, case2, case3, case4
.include “filename” ; tell the assembler to read a file
02/25/2025 13
.eseg ; start a new EEPROM segment
.exit ; tells the assembler to stop assembling
the file
Macro and ENDMACRO Directives
A name assigned to a group of instructions or directives
Used in cases the same instruction sequence must be
included in several places
These sequences of instructions may operate on different
parameters
The parameters are referred to as @0, @1, @2, …
By using the macro mechanism, the user only needs to type
02/25/2025 14
.macro macro_name
…
…
.endmacro
Example 3.5
.macro sub16I
subi @0, low(@2)
sbci @1, high(@2)
.endmacro
To invoke this macro
sub16I r16,r17,0x3456
02/25/2025 15
Example 3.6 Write a macro to load an address into a specified
pointer.
Solution:
.macrosetPointer
ldi @0, low(@2)
ldi @1, high(@2)
.endmacro
How to invoke
setPointer YL, YH, array
02/25/2025 16
AVR Assembly Program Template
.include <atxmega128A1def.inc>
.cseg ; declare a new code segment
.org 0x00 ; reset vector
jmp start
.org 0xFA
start: ldi r16,low(RAMEND) ; set up stack pointer
out CPU_SPL, r16 ; “
ldi r16, high(RAMEND) ; “
out CPU_SPH, r16 ; “
…
02/25/2025 17
Software Development Procedure
Step 1
Obtain problem definition—via interview to get the programmer
(or software developer) and user to agree upon what need to be
done.
Step 2
Develop program algorithm and represent it using pseudo code or
flowchart.
Step 3
Convert algorithm or flowchart into program code.
Step 4
Test the program.
Step 5
Maintain the program—good documentation is important.
02/25/2025 18
Format of Algorithm
Step 1
…
Step 2
…
Step 3
…
02/25/2025 19
Algorithm for Generating 1 kHz Square
Waveform using PE0 pin
Step 1
Pull PE0 pin high.
Step 2
Wait for 0.5 ms.
Step 3
Pull PE0 pin low.
Step 4
Wait for 0.5 ms.
Step 5
02/25/2025 Go to Step 1. 20
Example 3.7 Write a program to add the 16-bit value stored at data
memory location at 0x2000~0x2001 and program memory at
0x1090~0x1091 and store the sum at data memory
0x2020~0x2021.
Algorithm
Step 1
Use Z to point to 0x1090 at program memory.
Step 2
Fetch the 16-bit value from program memory at 0x1090~0x1091 and leave the
value in r0~r1.
Step 3
Fetch the 16-bit value from data memory at location 0x2000~0x2001 and leave it
in r2~r3.
Step 4
Add r2 to r0;
Step 5
Add r3 and C flag to r1.
Step 6
Save r0&r1 in data memory at 0x2020~0x2021.
02/25/2025 21
Solution:
.include<atxmega128A1def.inc>
.cseg
.org 0x00
jmp start
.org 0xFA
start: ldi ZL, 0x90
ldi ZH, 0x10
lpm r0, Z+
lpm r1, Z
lds r2, 0x2000 ; fetch second operand from
lds r3, 0x2001 ; data memory
add r0, r2 ; add the low bytes
adc r1, r3 ; add the high byte
sts 0x2020, r0 ; save the low byte of the sum
sts 0x2021, r1 ; save the high byte of the sum
again:jmp again
02/25/2025 22
Example 3.8 Write a program to add 20 to data memory locations
0x2000, 0x2001, 0x2002, respectively.
Solution:
.include <atxmega128A1def.inc>
.cseg
.org 0x00
jmp start
.org 0xFA
start: ldi r16, 20 ; place 20 in r16
lds r0, 0x2000
add r0, r16
sts 0x2000, r0
lds r0, 0x2001
add r0, r16
sts 0x2001, r0
lds r0, 0x2002
add r0, r16
sts 0x2002, r0
02/25/2025 23
Multi-precision Addition
Example 3.9 Write a program to add two 32-bit numbers stored at
num1 and num2 at program memory and save the sum at data
memory 0x2000~0x2003.
Solution:
.include <atxmega128A1Udef.inc>
.cseg
.org 0x00
jmp start
.org 0xFA
start: ldi ZL, low(num1 << 1)
ldi ZH, high(num1 << 1)
lpm r0, Z+
lpm r1, Z+
lpm r2, Z+
lpm r3, Z
ldi ZL, low(num2 << 1)
ldi ZH, high(num2 << 1)
02/25/2025 24
lpm r4, Z+
lpm r5, Z+
lpm r6, Z+
lpm r7, Z
add r0, r4 ; add the least significant bytes
sts 0x2000, r0 ; save the sum of the least significant bytes
adc r1, r5
sts 0x2001, r1
adc r2, r6
sts 0x2002, r2
adc r3, r7
sts 0x2003, r3
here: rjmp here
num2: .dw 0x5432, 0x9876 ; 0x98765432
num1: .dw 0x4321, 0x6543 ; 0x65434321
02/25/2025 25
Multi-precision Subtraction
Example 3.10 Write a program to subtract the 32-bit numbers
stored at program memory starting at num1 from the 32-bit number
stored at program memory starting at num2 and save the difference
at data memory 0x2000~0x2003.
Solution:
.include <atxmega128A1Udef.inc>
.cseg
.org 0x00
jmp start
.org 0xFA
start: ldi ZL, low(num1 << 1)
ldi ZH, high(num1 << 1)
lpm r0, Z+
lpm r1, Z+
lpm r2, Z+
lpm r3, Z
ldi ZL, low(num2 << 1)
02/25/2025
ldi ZH, high(num2 << 1) 26
lpm r4, Z+
lpm r5, Z+
lpm r6, Z+
lpm r7, Z
sub r4, r0
sts 0x2000, r4
sbc r5, r1
sts 0x2001, r5
sbc r6, r2
sts 0x2002, r6
sbc r7, r3
sts 0x2003, r7
forever: rjmp forever
02/25/2025 27
Multiplication
mul rd, rs ; rd & rs can be r0 to r31 (unsigned
multiplication)
muls rd, rs ; rd & rs can be r16 t0 r31 (signed
multiplication)
mulsu rd, rs ; rd & rs can be r16 to r31
The low and high bytes of the product are placed in r0 & r1.
02/25/2025 28
Infinite Program Loop
forever: …
…
jmp forever
02/25/2025 29
For … Loop—(format 1) (format 2)
for (j = n1; j <= n2; j++) for (j = n2; j >= n1; j--)
.def j = r19 .def j = r19
.equ n1 = 1; .equ n1 = 1
.equ n2 = 40 .equ n2 = 40
… …
ldi j, n1 ldi j, n2 ; initialize j
floop: cpi j, n2+1 floop: cpi j, n1-1 ; loop termination
test
breq done breq done
… …
inc j dec j ; loop index
update
jmp floop jmp floop
done: … done: …
02/25/2025 30
#include <atxmega128A1def.inc>
Write a
.def sumL = r19
program to
.def sumH = r20
add integers
.def j = r18
from 20 until
.def zero = r21
120.
.equ n1 = 20
.equ n2 = 120
.cseg
.org 0x00
jmp start
.org 0xF6
start: ldi r16, low(RAMEND)
out CPU_SPL, r16
ldi r16, high(RAMEND)
out CPU_SPH, r16
02/25/2025 31
clr sumL ; initialize SUM to 0
clr sumH ; “
clr zero ; force r21 to 0
ldi j, n1
floop: cpi j, n2+1
breq done
add sumL, j
adc sumH, zero ; add 0 and C flag to sumH
inc j
jmp floop
done: rjmp done
02/25/2025 32
Compare & Conditional Branch Instructions
Program loop implementation requires compare instruction for
setting up loop termination condition
Program loop also needs conditional branch instruction (brcc
02/25/2025 33
Table 3.6 AVR conditional branch instructions
Mnemonics Description Operation
sbrc Rr, b Skip if bit in register cleared If (Rr(b) == 0) PC ¬ [PC] + 2 or 3
sbrs Rr, b Skip if bit in register set If (Rr(b) == 1) PC ¬ [PC] + 2 or 3
sbic A, b Skip if bit in I/O register cleared If (I/O(A,b) == 0) PC ¬ [PC] + 2 or 3
sbis A,b Skip if bit in I/O register set If (I/O(A,b) == 1) PC ¬ [PC] + 2 or 3
brbs s, k Branch if status flag set If (SREG(s) == 1) then PC ¬ [PC] + k + 1
brbc s, k Branch if status flag cleared If (SREG(s) == 0) then PC ¬ [PC] + k + 1
breq k Branch if equal If (Z == 1) then PC ¬ [PC] + k + 1
brne k Branch if not equal If (Z == 0) then PC ¬ [PC] + k + 1
brcs k Branch if carry set If (C == 1) then PC ¬ [PC] + k + 1
brcc k Branch if carry cleared If (C == 0) then PC ¬ [PC] + k + 1
brsh k Branch if same or higher If (C == 0) then PC ¬ [PC] + k + 1
brlo k Branch if lower If (C == 1) then PC ¬ [PC] + k + 1
brmi k Branch if minus If (N == 1) then PC ¬ [PC] + k + 1
brpl k Branch if plus If (N == 0) then PC ¬ [PC] + k + 1
brge k Branch if greater or equal, signed If (N Å V == 0) then PC ¬ [PC] + k + 1
brlt k Branch if less than, signed If (N Å V == 1) then PC ¬ [PC] + k + 1
brhs k Branch if half carry set If (H == 1) then PC ¬ [PC] + k + 1
brhc k Branch if half carry cleared If (H == 0) then PC ¬ [PC] + k + 1
brts k Branch if T flag set If (T == 1) then PC ¬ [PC] + k + 1
brtc k Branch if T flag cleared If (T == 0) then PC ¬ [PC] + k + 1
brvs k Branch if V flag set If (V == 1) then PC ¬ [PC] + k + 1
brvc k Branch if V flag cleared If (V == 0) then PC ¬ [PC] + k + 1
brie k Branch if interrupt enabled If (I == 1) then PC ¬ [PC] + k + 1
brid k Branch if interrupt disabled If (I == 0) then PC ¬ [PC] + k + 1
02/25/2025 34
Example 3.11 Write an assembly program to compute the sum
of an array of 8-bit elements and save the sum at
0x2000~0x2001 using for-loop.
Solution:
Algorithm:
Step 1
Let sumH, sumL, ptr, and lpCnt represent the high byte of sum, low byte of
sum, array pointer, and loop count.
Step 2
sumH = 0; sumL = 0; ptr array; lpCnt = n1.
Step 3
If (lpCnt == (n2+1)) then save the sum in data memory and stop.
Step 4
Fetch one element from array pointed to by ptr and then increment ptr by 1.
Step 5
Add the fetched element to sumL and then add 0 and carry to the sumH.
Step 6
Increment lpCnt and Go to Step 3.
02/25/2025 35
.include <atxmega128A1def.inc>
.equ n1 = 1
.equ n2 = 40
.def lpCnt = r19
.def sumH,r21
.def sumL,r20
.def zero,r1
.dseg
.org 0x2000
sum: .byte 2
.cseg
.org 0x00
jmp start
.org 0xFA
start: ldi ZL,low(array << 1)
ldi ZH,high(array << 1)
ldi lpCnt, n1
clr sumL
clr sumH
02/25/2025 36
clr zero
loop: cpi lpCnt,n2+1 ; loop termination check
breq done ; “
lpm r0, Z+ ; fetch the next number
add sumL, r0 ; add to the lower byte of
SUM
adc sumH, zero
inc lpCnt
rjmp loop
done: sts sum, sumL
sts sum+1,sumH
Here: rjmp here
array: .db 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
.db 11,12,13,14,15,16,17,18,19,20
.db 21,22,23,24,25,26,27,28,29,30
.db 31,32,33,34,35,36,37,38,39,40
02/25/2025 37
The While Loop
while C do S
while loop tests loop termination at the beginning of the loop
Perform loop operation
Need to update the loop termination condition after an iteration
Need to jump to the start of the loop
02/25/2025 38
Example 3.12 Write an assembly program to find the number of
array elements that are either less than 25 or greater than 110.
Solution:
.include <atxmega128A1def.inc>
.def lpCnt= r20
.def cnt = r21
.equ NN = 40
.equ LO = 25
.equ HI = 110
.cseg
.org 0x00
jmp start
.org 0xFA
start: ldi ZL,low(array << 1)
ldi ZH,high(array << 1)
ldi lpCnt, 0
ldi cnt, 0
02/25/2025 39
loop: cpi lpCnt,NN
breq done
lpm r16, Z+ ; fetch one array element
cpi r16, LO ; Is it lower than 25?
brlo yes
cpi r16, HI+1 ; Is it higher than 110?
brsh yes
rjmp next
yes: inc cnt
next: inc lpCnt
jmp loop
done: jmp done
array: .db 11,23,10,14,15,16,19,30,24,31
.db 32,39,45,36,20,25,38,41,42,43
.db 70,111,112,113,114,115,118,119,120,121
.db 1,2,3,4,5,6,7,8,9,10
02/25/2025 40
Repeat Until Loop
Perform initialization before loop
Perform loop operation at the start of loop
Update loop index
Test loop termination at the end of the loop
.equ N = 20 .equ N = 20
.def lpCnt = r16 .def lpCnt = r16
… …
ldi lpCNt, 0 ldi lpCnt, N
loop: … loop: …
… …
inc lpCnt dec lpCnt
cpi lpCnt, N brne loop
brne loop …
02/25/2025 41
Example 3.13 Write a program to find the total elements of an
array of 8-bit elements that have bits 2, 3, and 4 set to 1. Use the
repeat—until looping construct. Assume that the array is stored in
program memory.
Solution:
Step 1
Let ptr, lpCnt, cnt represent the array pointer, loop count, and the count
of elements with bit 2, 3, 4 set to 1.
Step 2
Initialize ptr, lpCnt, cnt.
Step 3
Read one element from program memory, move pointer.
Step 4
If the bit 4, 3, & 2 are all 1s, increment cnt by 1.
Step 5
Decrement lpCnt by 1.
Step 6
If lpCnt == 0, stop; else go to Step 3.
02/25/2025 42
.include <atxmega128A1def.inc>
.dseg
.org 0x2000
count: .byte 2
.equ NN = 30
.def lpCnt = r19
.def cnt = r20
.cseg
.org 0x00
jmp start
.org 0xFA
start: ldi ZL, low(array <<1)
ldi ZH, high (array << 1)
ldi cnt, 0
ldi lpCnt, NN
02/25/2025 43
loop: lpm r16, Z+
andi r16, 0x1C
cpi r16, 0x1C
brne next
inc cnt
next: dec lpCnt
brne loop
here: rjmp here
array: .db
0x21,0x2C,0x37,0x42,0x3D,0x59,0x11,0x22,0x97,0x3E
.db
0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A
.db
0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F
02/25/2025 44
Boolean Instructions
02/25/2025 45