Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing an Assembly-language program Atmel assembly language CS-280 Dr. Mark L. Hornick 1.

Similar presentations


Presentation on theme: "Writing an Assembly-language program Atmel assembly language CS-280 Dr. Mark L. Hornick 1."— Presentation transcript:

1 Writing an Assembly-language program Atmel assembly language CS-280 Dr. Mark L. Hornick 1

2 CS-280 Dr. Mark L. Hornick 2 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

3 CS-280 Dr. Mark L. Hornick 3 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 “;”

4 CS-280 Dr. Mark L. Hornick 4 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

5 CS-280 Dr. Mark L. Hornick 5 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)

6 CS-280 Dr. Mark L. Hornick 6 Some assembler directives are used to define the location of the program instructions in Flash memory: $3C00 NNN bytes configurable Reset and interrupt vector section 42 words (84 bytes) $002A Your program goes here!.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 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

7 CS-280 Dr. Mark L. Hornick 7 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 $3C00 1024 words (2048 bytes) Reset and interrupt vector section 42 words (84 bytes) $002A Your program goes here

8 CS-280 Dr. Mark L. Hornick 8 Some Assembler directives can be used to define variable-like symbols.DEF =R 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 to undefine a symbol.EQU = 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 = Same as.EQU, but variables defined with.SET can be changed later

9 CS-280 Dr. Mark L. Hornick 9 File-related Assembler Directives.LIST (.NOLIST) Enable (disable) list file generation during assembly List files have the.lst file extension) On by default.INCLUDE Include the contents of another file Ex:.INCLUDE “m32def.inc” Includes a file that contains numerous convenient.EQU and.DEF directives

10 CS-280 Dr. Mark L. Hornick 10 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

11 CS-280 Dr. Mark L. Hornick 11 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

12 CS-280 Dr. Mark L. Hornick 12 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


Download ppt "Writing an Assembly-language program Atmel assembly language CS-280 Dr. Mark L. Hornick 1."

Similar presentations


Ads by Google