Sie sind auf Seite 1von 54

Introduction to Assembly

language
1

USING THE AVR MICROPROCESSOR

M. Neil - Microprocessor Course


Outline
2

— Introduction to Assembly Code


— The AVR Microprocessor
— Binary/Hex Numbers
— Breaking down an example microprocessor program
— AVR instructions overview
— Compiling and downloading assembly code
— Running and debugging assembly code

M. Neil - Microprocessor Course


A really simple program
3
— Some variables (i,j)
int main(void)
— Set variables to initial
{
values char i;
char j;
— A loop
i=0;
¡ Check a condition to see if j=0;
we are finished while (i<10) {
j = j + i;
¡ Do some arithmetic PORTB = j;
¡ Output some information PORTB = i;
i++;
¡ Increment the loop }
counter return 0;
}

M. Neil - Microprocessor Course


Running a program on a microprocessor
4

— When you want to turn your program into something


which runs on a microprocessor you typically compile the
program
— This creates a program in the “machine language” of the
microprocessor
¡ A limited set of low level instructions which can be executed directly
by the microprocessor
— We can also program in this language using an assembler
— We can have a look at the assembly language the c
compiler generates for our simple program to
understand how this works

M. Neil - Microprocessor Course


Translating our program into assembly language
5
int main(void)
{ 000000be <main>:
be: 80 e0 ldi r24, 0x00 ;0
char i; c0: 90 e0 ldi r25, 0x00 ;0
char j; c2: 98 0f add r25, r24
i=0; c4: 92 bb out 0x18, r25 ; 18
c6: 82 bb out 0x18, r24 ; 18
j=0;
c8: 8f 5f subi r24, 0xFF ; 255
while (i<10) { ca: 8a 30 cpi r24, 0x0A ; 10
j = j + i; cc: d1 f7 brne .-12 ; 0xc2 <main+0x4>
PORTB = j; ce: 80 e0 ldi r24, 0x00 ;0
d0: 90 e0 ldi r25, 0x00 ;0
PORTB = i; d2: 08 95 ret
i++;
}
return 0; Location in Opcodes –
The Assembly
program the
} language equivalent
memory program
of the opcodes
(Address) code
M. Neil - Microprocessor Course
AVR Microprocessor architecture
6
Registers
Program
Storage for numbers the
Memory
processer will perform
Your code goes
arithmetic on
here

Arithmetic Logic Unit


(ALU)
The heart of the
processor which
Input/Output handles logic/math
Interface to the
outside world

M. Neil - Microprocessor Course


Numbers on a microprocessor
7

— We’ve seen that our program is converted into a


series of numbers for execution on the
microprocessor
— Numbers are stored in a microprocessor in memory
¡ They can be moved in an out of registers and memory
¡ Calculations can be done
¡ Numbers can be sent to Output devices and read from Input
devices
— The numbers are stored internally in binary
representations.
¡ We will study precisely how this is done shortly, and even build
some simple memory devices.

M. Neil - Microprocessor Course


Binary/Hexadecimal Numbers
8
— A “Bit” can be a 0 or 1 Binary Hex Decimal
¡ The value is set using transistors 0000 0 0
in the hardware
0001 1 1
— Bits are organized into groups to
represent numbers 0010 2 2
¡ 4 Bits is a “Nybble” 0011 3 3
÷ Can store numbers 0-15 0100 4 4
¡ 8 Bits is a “Byte” 0101 5 5
÷ Can store numbers 0-255
0110 6 6
¡ 16 Bits is a word
÷ Can store numbers 0-65535 0111 7 7
— Hexadecimal is a very handy 1000 8 8
representation for binary 1001 9 9
numbers
1010 A 10
¡ Each 4 bits maps onto a HEX
number 1011 B 11
¡ Can quickly convert from HEX to 1100 C 12
binary
1101 D 13
1110 E 14
1111 F 15
M. Neil - Microprocessor Course
Binary Representation
9

• This representation is based on powers of 2. Any


number can be expressed as a string of 0s and 1s

Example: 5 = 1012 = 1* 22 + 0*21 + 1*20

Example: 9 = 10012 = 1* 23 + 0* 22 + 0*21 + 1*20

Exercise: Convert the numbers


19, 38, 58 from decimal to binary.
(use an envelope, a calculator or C program)

M. Neil - Microprocessor Course


Hexadecimal Representation
10

• This representation is based on powers of 16.


Any number can be expressed in terms of:
0,1,2,…,9,A,B,C,D,E,F (0,1,2,…,9,10,11,12,13,14,15)

Example: 256 = 10016 = 1* 162 + 0*161 + 0*160

Example: 1002 = 3EA16 = 3* 162 + 14*161 + 10*160

Exercise: Convert the numbers


1492, 3481, 558 from decimal to hex.
(use calculator or maths package)

M. Neil - Microprocessor Course


HEX/Binary Conversion
11

— Converting HEX/Binary to Decimal is a bit painful,


but converting HEX/Binary is trivial
— We often use the notations 0x or $ to represent a
HEX number
¡ Example 0x15AB for the HEX number 15AB
¡ In assember language we see the notation $A9 for the HEX
number A9

Exercise: Convert the numbers


0xDEAD 0xBEEF from Hex to binary.
Now convert them to Decimal

M. Neil - Microprocessor Course


8 Bit Microprocessors
12

— The AVR microprocessor we will be using is an “8


bit” processor
— The operations the processor performs work on 8 bit
numbers
¡ Data is copied from memory into the processors internal
“registers” 8 bits at a time
¡ Operations (addition/subtraction/etc..) can be performed on
the 8 bit registers
¡ We can of course do calculation with bigger numbers, but we
will have to do this as a sequence of operations on 8 bit
numbers
÷ We will see how to do this later – using a “carry” bit

M. Neil - Microprocessor Course


Operations
13

— The processor can perform several operations


¡ Including “Boolean” algebra

Operation Register A Register B Result


Add A,B 00001111 00000001 00010000
Not A 01010101 10101010
OR A,B 00010101 00101010 00111111
ShiftL A 00001111 00011110
ShiftR A 00001111 00000111
AND A,B 01010101 10101010 00000000

Exercise: (1) Find NOT(AAA)


(2) Find OR(AAA; 555) Why is shift important ?
(3) Find AND (AEB123; FFF000) Try ShiftR(011) ShiftL(011)
(what do SHIFTR and ShiftL do in
base 10?)
M. Neil - Microprocessor Course
What About Subtraction: Negative Numbers
14

— With 4 bits, you can Integer Sign


Magnitude
1’s
complement
2’s
complement

represent +7 0111 0111 0111

¡ 0 to +15 +6
+5
0110
0101
0110
0101
0110
0101

¡ -8 to + 7 +4 0100 0100 0100

÷ There are three ways to +3 0011 0011 0011


+2 0010 0010 0010
represent these negative +1 0001 0001 0001
numbers 0 0000 0000 0000
-1 1001 1110 1111
-2 1010 1101 1110
— Sign/Magnitude: Set the top bit to 1 -3 1011 1100 1101
— 1’s complement : Take the complement -4 1100 1011 1100
of the number -5 1101 1010 1011
— 2’s complement : Take the complement -6 1110 1001 1010
of the number and then add 1 -7 1111 1000 1001
-8 1000 (-0) 0111(??) 1000

M. Neil - Microprocessor Course


What About Subtraction: Negative Numbers
15

— There is a good reason to use a 2’s complement


representation
¡ Binary addition of a two’s complement numbers “just works”
whether the numbers are positive or negative

3+4 3 + -4 -3 + 4 -3 + -4
0011 0011 1101 1101
+ 0100 + 1100 + 0100 + 1100
= 0111 = 1111 = 0001 = 1001
(7) (-1) (+1) (-7)

Exercise: Fill out the same table using sign magnitude numbers.
Do you understand now why 2’s complement is a useful
representation!
M. Neil - Microprocessor Course
8 Bit representations
16

— 8 bits can be used to represent any 256 different symbols


— The numbers 0-255
— The numbers -128 to + 127
— Part of a longer number
— A “Character”
¡ Hence “char” in c
¡ This is a bit out of date
¡ Unicode uses 16 bits

M. Neil - Microprocessor Course


Back to our program
17
• The program is stored in program
000000be <main>:
memory
be: 80 e0 ldi r24, 0x00 ;0
• There are 64Kilobytes of program
c0: 90 e0 ldi r25, 0x00 ;0
c2: 98 0f add r25, r24
memory (2^16 bytes)
c4: 92 bb out 0x18, r25 ; 18 • Each location stores an 8 bit number
c6: 82 bb out 0x18, r24 ; 18 • The location is specified with a 16 bit
c8: 8f 5f subi r24, 0xFF ; 255 Address
ca: 8a 30 cpi r24, 0x0A ; 10 • (0x0000-0xFFFF)
cc: d1 f7 brne .-12 ; 0xc2 <main+0x4>
ce: 80 e0 ldi r24, 0x00 ;0
Address Value
d0: 90 e0 ldi r25, 0x00 ;0 00BE 80
d2: 08 95 ret
00BF E0
00C0 90
00C1 E0
00C2 98
00C3 0F
M. Neil - Microprocessor Course 00C4 92
18

— AVR Registers – where the


Registers on numerical work is done in a
the AVR program
There are 32 General
purpose registers on the
AVR microprocessor
(R0-R31)

Each of these can hold


an 8 bit number

R26:R27 is also a 16 bit


register called X

R28:R29 is Y

R30:R31 is Z

You can perform


calculations on these
registers very quickly

M. Neil - Microprocessor Course


Back to our program: Loading a register
19
• The first instruction is loading a
000000be <main>:
value of 0x00 into register r24
be: 80 e0 ldi r24, 0x00 ;0
• This instruction is encoded in the 16
c0: 90 e0 ldi r25, 0x00 ;0
c2: 98 0f add r25, r24
bit opcode stored at address 00BE
c4: 92 bb out 0x18, r25 ; 18 • The Assembler code is
c6: 82 bb out 0x18, r24 ; 18 • ldi r24,0x00
c8: 8f 5f subi r24, 0xFF ; 255 • LoaD Immediate r24 with 0x00
ca: 8a 30 cpi r24, 0x0A ; 10
cc: d1 f7 brne .-12 ; 0xc2 <main+0x4>
ce: 80 e0 ldi r24, 0x00 ;0
Address Value
d0: 90 e0 ldi r25, 0x00 ;0 00BE 80
d2: 08 95 ret
00BF E0
00C0 90
00C1 E0
00C2 98
00C3 0F
M. Neil - Microprocessor Course 00C4 92
Decoding an Opcode
20

Words are stored “little endian”


Read into the processor as E080
E080=1110 0000 1000 0000
dddd: 1000=8 -> 16+8 = r24
KKKKKKKK=0x00
be: 80 e0 ldi r24, 0x00
Exercise: what is the opcode for
ldi r19, 0x3F
M. Neil - Microprocessor Course
Back to our program: Loading a register
21

000000be <main>: The second instruction is loading a


be: 80 e0 ldi r24, 0x00 ;0 value of 0x00 into register r25
c0: 90 e0 ldi r25, 0x00 ;0
c2: 98 0f add r25, r24 This adds r24 to r25 and stores the
c4: 92 bb out 0x18, r25 ; 18 result into register r25
c6: 82 bb out 0x18, r24 ; 18
c8: 8f 5f subi r24, 0xFF ; 255 The next instructions output r24
ca: 8a 30 cpi r24, 0x0A ; 10 and r25 (we’ll learn where later)
cc: d1 f7 brne .-12 ; 0xc2 <main+0x4>
ce: 80 e0 ldi r24, 0x00 ;0 This is subtracting a value of 0xFF
d0: 90 e0 ldi r25, 0x00 ;0 from register r24
d2: 08 95 ret This is the compiler cleverly adding 1

This instruction is comparing the


value 0x0A to register r24

If they are not equal, the next instruction will branch back to location 0xC2
that is loop back if they are equal it continues on (and returns from main)
M. Neil - Microprocessor Course
Compare assembly language to c
22
int main(void) 000000be <main>:
{ be: 80 e0 ldi r24, 0x00 ;0
c0: 90 e0 ldi r25, 0x00 ;0
char i;
c2: 98 0f add r25, r24
char j; c4: 92 bb out 0x18, r25 ; 18
i=0; c6: 82 bb out 0x18, r24 ; 18
c8: 8f 5f subi r24, 0xFF ; 255
j=0;
ca: 8a 30 cpi r24, 0x0A ; 10
while (i<10) { cc: d1 f7 brne .-12 ; 0xc2 <main+0x4>
j = j + i; ce: 80 e0 ldi r24, 0x00 ;0
PORTB = j; d0: 90 e0 ldi r25, 0x00 ;0
d2: 08 95 ret
PORTB = i;
i++; • Here the variables are stored in registers r24, r25
} • Pretty easy to see how this program is translated
return 0; • More complex programs quickly become very
complicated to understand in assembly language
}

M. Neil - Microprocessor Course


Programming in this course
23

— We will be programming exclusively in assembler


¡ Allows us to understand precisely what the microprocessor is
going to do and how long it will take to do so
÷ important for time critical applications
¡ Full access to all functions of the microprocessor
¡ With care can make very efficient use of resources
÷ Sometimes very important for small microprocessors
— Programming in assembler requires some discipline
¡ Code can be very difficult to understand
÷ The code is very low level
÷ Line by line comments very important

M. Neil - Microprocessor Course


The AVR instruction set
24

— We’ve seen a few sample instructions which cover


most of the basic type of operations
— Arithmetic and Logic instructions
¡ (ADD, SUB, AND, OR, EOR, COM, INC, DEC, …)
— Branch Instructions
¡ Jump to a different location depending on a test

— Data transfer instructions


¡ Move data to/from Registers and memory

— Bit setting and testing operations


¡ Manipulate and test bits in registers

M. Neil - Microprocessor Course


Arithmetic and Logic Instructions
25

— Addition — Increment
add r20,r21 inc r20
R20 ß r20+r21 R20 ß r20+1

— Subtraction
subi r20,$22 — Logic
R20 ß r20-$22
and r20,r24
sub r20,r21
R20 ß r20-r21 R20 ß AND(r20,r24)

Many instructions work either with two


registers, or with “immediate” data values
(stored in the opcode)

M. Neil - Microprocessor Course


Arithmetic and Logic Instructions – The full set
26

M. Neil - Microprocessor Course


The Status Register
27
— Every time the processor
performs an operation it sets bits
in the Status Register (SREG)
¡ Example cpi r01,$77
¡ This register is in the I/O region
— SREG can be examined/set with
the in and out instructions
¡ in r17,SREG
¡ out SREG,r22
— The status bits are also tested by
branch instructions to decide
whether or not to jump to a
different location

M. Neil - Microprocessor Course


Branch Instructions
28

— Branch
breq label1
Branch if equal to location label1
brlo label2
Branch if lower to location label2
If the Branch test fails – the next line of code is executed
If the Branch test is successful, the program jumps to the location specified
— Jump
jmp label3
Jump to label3 – no information about where we jumped from is
saved. This is a one way trip
— Call, Return
rcall mysub
ret
Call subroutine mysub. The program saves information about where it currently is
executing and then jumps to the code at mysub. When the subroutine is finished it
calls ret, which then returns to where the rcall was made.

M. Neil - Microprocessor Course


All Branch Instructions
29

M. Neil - Microprocessor Course


Data Transfer Instructions
30

— Load — Copy Register


ldi r20,$73
mov r20,r21
R20 ß $73
ld r20,X R20 ß r21
R20 ß (X)

— Input — Output
in r20,PIND out PORTD,r24
R20 ß PIND PORTD ß r24

There are a few quirks about loading and storing to


memory. We will cover this in detail soon.

M. Neil - Microprocessor Course


Data transfer reference
31

M. Neil - Microprocessor Course


32

The
Atmega128
The microprocessor you
will be using has several
input and output ports

Some of these are


already connected to
switches or LEDs on the
boards we are using

Others will be available


to you to connect to
various devices.

It is time to make some


lights blink!

M. Neil - Microprocessor Course


Setting up the Input/Output ports:
33

• For the ports we are ; ******* Port B Setup Code ****


using we set the Data ldi r16, $FF ; all bits out
Direction Register out DDRB , r16 ; Port B Direction Register
(DDR) which has a bit ldi r16, $FF ; Init value
for each bit of I/O (1 out PORTB, r16 ; Port B value
for input, 0 for output)
• We can then set the ; ******* Port D Setup Code ****
initial value of the data ldi r16, $00 ; all bits in
bits at the I/O port out DDRD, r16 ; Port D Direction Register
• PortB is connected to ldi r16, $FF ; Init value
LEDs on our board out PORTD, r16 ; Port D value
• PortD is connected to
the blue switches

M. Neil - Microprocessor Course


The ATmega128 Microprocessor
34

— In this course you will be using the


ATmega128 processor mounted on an
ATMEL programming board (STK300)

M. Neil - Microprocessor Course


Where the I/O ports are connected
35

PORTD Switches

PORTB LEDs

PORT connectors

M. Neil - Microprocessor Course


Setting up a code directory
36

— Create a directory on your H: drive where you will


store your code
— Download the file Simple.asm onto your computer
¡ from the course web page (Assembler Codes):

M. Neil - Microprocessor Course


Getting Started with STUDIO 7:
37

Go to Start à Programs à ATMEL Studio 7.0


à ATMEL Studio 7.0
Select advanced mode

Select New
Project

M. Neil - Microprocessor Course


Getting Started with STUDIO 7:
38

You should now see the window:

Click this and navigate


Pick Assembler to your code directory.
Put on H: drive!

Create new
folder

Pick a name for your At the


project end
M. Neil - Microprocessor Course
Getting Started with STUDIO 4:
39

You should now see the window:

Select ATmega 128

At the
end
M. Neil - Microprocessor Course
Getting started with Studio 7:
40

Navigate to and open


downloaded Simple.asm

Your file

Main edit window

M. Neil- Microprocessor Course


Getting started with Studio 7:
41

Paste into your file


main.asm

Close
Simple.asm

Copy code from


Simple.asm

M. Neil- Microprocessor Course


Building and Running your Program :
42
Click to open project
properties

Save then
close

Select
Simulator

M. Neil - Microprocessor Course


Building and Running your Program :
43
Click on
Build

Output window
appears showing a
successful build or
other wise!
M. Neil - Microprocessor Course
Building and Running your Program :
44
Some new files have
been generated

M. Neil - Microprocessor Course


Building and Running your Program :
45
Hit “Start debugging
and break” button

Current point in code Click to open other


monitoring windows

Memory window

M. Neil - Microprocessor Course


Open monitoring Windows:
46

IO window shows
many others

Processor Status
window shows
many processor
resources

Open as
required for
detailed view

M. Neil - Microprocessor Course


Stepping in the simulator
More comands
47
available through
Debug menu

Current point
in program
Here to reset
Here to pause
Here to run
Click this to
without
step through
display
programme

Arrange monitoring windows in


M. Neil- Microprocessor Course main window as you desire
Exercising the ATmega128 commands:
48

— Download the program Simple.asm, assemble it


and run it in the simulator
— Step through the program and make sure you
understand what is happening after each
instruction
— Try changing the number of times the loop is
executed and make sure you understand the
outputs on PORTB

M. Neil - Microprocessor Course


Programming the board
49

Power connector
ISP interface:
and switch
Programming only

Connect “gold box”


ICE programmer
from computer USB
to JTAG interface

JTAG interface:
Programming
and debugging

M. Neil - Microprocessor Course


What is the In Circuit Emulator/Debugger doing?
50

— JTAG (Joint Test Action Group) connector provides


an interface to your microprocessor that lets you
¡ Download from main computer to microprocessor device
memory (programme and data)
¡ Control execution of code in microprocessor (start/stop/step)
¡ Read device memory and register contents back to computer
— ICE is a useful tool not just for its ability to
programme your microprocessor but also to let you
see how your programme is working on the device in
its particular application environment

M. Neil - Microprocessor Course


Running the in circuit programmer/debugger
51

Save then
close

Click to open project


properties

Select Atmel-
ICE and JTAG
interface

M. Neil- Microprocessor Course


Stepping through with the ICE
52

Current point
in program
Here to reset
Here to pause
Here to run Click here to
Click this to change bits
without
step through in regiisters
display
programme

You are now running on the


M. Neil- Microprocessor Course microprocessor!
Programs to write I:
53

— Download the program simple.asm, assemble it


download it to the board and run it.
¡ What do you see on the LEDs?
¡ Do you know why (note that a dark LED è 1)?
÷ change the code so that output on the LEDs has a lit LED for a 1
(think about using the com instruction)
— Modify the program so that it changes the number of
times the loop is run depending on which switch
button is pushed
¡ Use the instruction in Rxx,PIND to get the state of the buttons
¡ Make sure that the LED output makes sense when you push
the different buttons

M. Neil - Microprocessor Course


Programs to write II:
54

— Make a counter from 0 – FF, output the values to


PORTB and look with your scope probe at the LSB
(PB0). How long does it take to make an addition?
Why does the B0 bit toggle with a frequency that is
twice that of B1? (check this using two scope
probes; one on B0 and another on B1)
— In the documentation you will find how many clock
counts are required to perform an instruction in your
program. The ATmega128 has an 8 MHz clock.
Predict the time it takes to do an addition and
compare with your measurement using the scope.

M. Neil - Microprocessor Course

Das könnte Ihnen auch gefallen