Title: Writing an Assembly-language program
1Writing an Assembly-language program
2The AVR assembler
- is a modern, single pass assembler
- Converts mnemonics to machine code(s)
- Ex add r20, r5 is assembled to 0d45
- Case is irrelevant alternately ADD R20, R5
- Reports syntactical errors
- Ex ADE R20, R5 (ADE no such instruction)
- Can create listing (.lst) and map files
- Generates object (.hex) files that can be
executed on a simulator or on real hardware - Intel defined the .hex file format
- Used automatically by gcc (C compiler)
- Youll use the C compiler in CE2810
3Comments in Assembler
- In addition to the style of commenting the
AVR Assembler permits C-style comments - Block comments starting with / and ending with
/ - // comments where the remainder of the line is
ignored - These C-style comments are unusual for Assemblers
- Typical Assemblers only permit
4Directives are used within an assembly language
program to control certain aspects of the
operation of the Assembler
- Directives begin with a period (.)
- Directive often take operands, but not always
- Ex .ORG 0x2a
- Case does not matter
- .org 0x2A is equivalent
- Directives are not instructions they are not
translated to opcodes - They only direct the Assembler how to interpret
subsequent assembly language input or generate
output - All directives are documented in the online help
of AVRStudio - Directives usually are placed at the beginning of
a program
5Number representation in AVR assembly language
- All values are decimal unless specified
- .ORG 42 decimal 42
- .ORG 0x2A hexadecimal
- .ORG 052 octal
- .ORG 0b00101010 binary
- Radix prefixes
- 0b, 0B binary (0b101010 or 0B101010)
- 0 octal
- 0x, 0X hexadecimal (0x002a or 0x2A)
6Assembler directives are used to define the
location of the program instructions in Flash
memory
- .CSEG
- Defines the start of the Code Segment
- ie, where executable instructions are placed by
the assembler - .ORG ltngt
- Directs the Assembler where to begin placing
instructions in memory - Example.CSEG the default segment .ORG 0x2a
directs the opcodes to be placed after the first
42 (0x2a) reserved words
Reset and interrupt vector section42 words (84
bytes)
002A
Your program goes here!
3C00
NNN bytes configurable
7The processor begins executing instructions at
flash memory address 0
- But the first 42 words should be reserved for
special instructions known as Reset and Interrupt
Vectors - Unless you explicitly put an instruction at
address 0, the (invalid) opcode 0xFFFF is placed
there - The processor skips the invalid opcode and
moves onto the next address location - To avoid this, use the .CSEG directive to place
an instruction at address 0 that forces the
processor to jump to a location where valid
instructions exist - .CSEGthe default segment
- .ORG 0x0 RJMP 0x2A jump to app section
- .ORG 0x2A your programs instructions
- The first reserved word at address 0 is the Reset
Vector that is designed to contain the
instruction that gets executed whenever the CPU
is reset - i.e., a jump to where the actual program begins
- The operand of RJMP is the address to jump to
Reset and interrupt vector section42 words (84
bytes)
002A
3C00
1024 words (2048 bytes)
8Some Assembler directives can be used to define
variable-like symbols
- .DEF ltsymbolgtRltngt
- Define a symbol to refer to a specific register
- .DEF CounterR10
- Counter can be used anywhere in the program in
place of R10 - Case does not matter
- Placement of .DEF does not matter, but should
precede first usage - Symbols can be redefined
- Use .UNDEF ltsymbolgt to undefine a symbol
- .EQU ltconstantgtltexpressiongt
- Define a constant to be used in place of a
constant value - .EQU START 0x42
- .EQU ZERO 0
- constants cannot be redefined or undefined
- .SET ltvariablegtltexpressiongt
- Same as .EQU, but variables defined with .SET can
be changed later
9File-related Assembler Directives
- .LIST (.NOLIST)
- Enable (disable) list file generation during
assembly - List files have the .lst file extension)
- On by default
- .INCLUDE ltfilegt
- Include the contents of another file
- Ex .INCLUDE m32def.inc
- Includes a file that contains numerous .EQU and
.DEF directives
10AVR Debugger/Simulator
- Simulates execution of the compiled program
- Start, stop, single-step
- Run to breakpoint
- Can view contents of memory, registers
- Tracks time required to execute
- Can see IO port status
11Labels can be used in place of actual addresses
- Every input line can be preceded by a label
- an alphanumeric string terminated by a colon ()
- Labels are used as
- targets for jump and branch instructions
- as a variable name of data in data memory (SRAM
or EEPROM). - The assembler automatically figures out what
address to assign to a label
12Labels used as jump targets in program
instructions
- Syntax label instruction operands
Comment - labels are given the value of the location
counter at the place they appear - Example
- .CSEG
- .ORG 0x0
- rjmp Start_of_program go to beginning
- .ORG 0x2A
- Start_of_programadd r1, r2 whitespace is
ignored - rjmp Start_of_program go to beginning
- The label (in this case) is assigned the value
of the address of beginning of the program (0x2A)
13Ports are channels from the CPU to external
hardware and software
- Atmega 32 has
- Digital I/O ports,
- Analog-to-Digital port,
- Serial port,
- The number of ports depends on processor type
- Each port has a specific fixed address in the I/O
address space - 0x00 through 0x3F (0-63)
- This address space is separate from the Program
and Data address spaces
14Atmega32 has four digital I/O ports
- PortA, PortB, PortC, PortD
- each have 8 pins
- Each of these ports is associated with three
special-purpose I/O registers - The register at 0x17 controls whether the pins on
PortB function as input or output - DDRB - Data Direction Register B
- Each pin can be configured independently
- Writing 0xff to DDRB configures all pins to
behave as output - Writing 0x0f configures pins 0-3 as output and
pins 4-7 as input
15Writing to a port
- The OUT instruction is used to write a value from
a Register to a port - OUT 0x18, r25 writes the value of r25 to port at
address 0x18 (PortB) - Each bit in the 8-bit value corresponds to a pin
of the port - Only the pins configured as output will actually
be written - The SBI instruction is used to set a single bit
within a port - SBI 0x18, 0 sets the value of bit 0 to 1
- The CBI instruction is used to reset a single bit
within a port - CBI 0x18, 0 sets the value of bit 0 to 0
16Some Assembler directives can be used to define
variable-like symbols
- .DEF ltsymbolgtRltngt
- Define a symbol to refer to a specific register
- .DEF CounterR10
- Counter can be used anywhere in the program in
place of R10 - Case does not matter
- Placement of .DEF does not matter, but should
precede first usage - Symbols can be redefined
- Use .UNDEF ltsymbolgt to undefine a symbol
- .EQU ltconstantgtltexpressiongt
- Define a constant to be used in place of a
constant value - .EQU DDRB 0x17
- .EQU PORTB DDRB1
- constants cannot be redefined or undefined
- .SET ltvariablegtltexpressiongt
- Same as .EQU, but variables defined with .SET can
be changed later
17PortB (0x18) is wired to the on-board LEDs
- Writing a value of 1 to a bit turns the LED on
- The associated pin must be configured as an output
18Reading from a port
- If DDRB specifies that some pins of a port are to
be configured for input, - The IN instruction is used to read a value from a
port configured for input - IN r0, 0x16 reads the value of the port at
address 0x16 (PortB input, or PinB) to r0 - PinB can only be read, not written
- PortB and DDRB can be both read and written