120 likes | 231 Views
Writing an Assembly-language program. Atmel assembly language. The AVR assembler. …is a modern, “single pass” assembler Converts mnemonics to machine instructions (opcodes + operands) Ex: add r20, r5 is assembled to 0x 0d45 Case is irrelevant; alternately: ADD R20, R5
E N D
Writing an Assembly-language program Atmel assembly language CS-280 Dr. Mark L. Hornick
The AVR assembler …is a modern, “single pass” assembler • Converts mnemonics to machine instructions (opcodes + operands) • Ex: add r20, r5 is assembled to 0x0d45 • 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/C++ compiler) • You’ll use the C compiler in CE2810 CS-280 Dr. Mark L. Hornick
Comments 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 “;” CS-280 Dr. Mark L. Hornick
Directives 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/operands • They only direct the Assembler how to interpret subsequent assembly language instructions or generate output • All directives are documented in the online help of AVRStudio Directives usually are placed at the beginning of a program, but may appear anywhere CS-280 Dr. Mark L. Hornick
Number 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) CS-280 Dr. Mark L. Hornick
Some assembler directives are used to define the location of the program instructions in Flash memory: .CSEG • Alerts the Assembler that subsequent assembly statements are intended to generate instructions for the Code Segment • ie, where executable machine code is placed .ORG <n> • Directs the Assembler where to begin placing subsequent instructions in memory • Example:.CSEG ;the default segment .ORG 0x2a directs the subsequent machine instructions 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 CS-280 Dr. Mark L. Hornick
The 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 .CSEG;the default segment .ORG 0x0 RJMP 0x2A ; jump to app section .ORG 0x2A ; your program’s 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 Your program goes here $3C00 1024 words (2048 bytes) CS-280 Dr. Mark L. Hornick
Some Assembler directives can be used to define variable-like symbols • .DEF <symbol>=R<n> • Define a symbol to refer to a specific register • .DEF Counter=R10 • Counter can be used anywhere in the program in place of R10 • Ex: ADD R20, Counter • Case does not matter • Placement of .DEF does not matter, but should precede first usage • Symbols can be redefined • Use .UNDEF <symbol> to undefine a symbol • .EQU <constant>=<expression> • Define a constant to be used in place of a constant value • .EQU START= 0x42 • Ex: .ORG START • .EQU ZERO = 0 • Ex: LDI R20, ZERO • constants cannot be redefined or undefined • .SET <variable>=<expression> • Same as .EQU, but variables defined with .SET can be changed later CS-280 Dr. Mark L. Hornick
File-related Assembler Directives • .LIST (.NOLIST) • Enable (disable) list file generation during assembly • List files have the .lst file extension) • On by default • .INCLUDE <“file”> • Include the contents of another file • Ex: .INCLUDE “m32def.inc” • Includes a file that contains numerous convenient .EQU and .DEF directives CS-280 Dr. Mark L. Hornick
AVR 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 CS-280 Dr. Mark L. Hornick
Labels 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 • The assembler automatically figures out what address to assign to a label CS-280 Dr. Mark L. Hornick
Every input line can be preceded by a label Example: .CSEG .ORG 0x0 rjmp Start_of_program ;go to beginning .ORG 0x2A Start_of_program:add r1, r2 Here: rjmp Here ;repeat forever • Labels are alphanumeric strings terminated by a colon(:) • Labels are given the value of the location counter at the place they appear • Labels can be used as jump targets in program instructions • The label Start_of_program: is assigned the value of the address of beginning of the program (0x2A); Here: is assigned 0x2B • The assembler automatically figures out what address to assign to a label CS-280 Dr. Mark L. Hornick