Lecture Overview • Assembly program structure Microprocessors & Interfacing – Assembler directives – Assembler expressions – Macros AVR Programming (II) • Memory access • Assembly process – First pass – Second pass Lecturer : Dr. Annie Guo S2, 2008 COMP9032 Week3 1 S2, 2008 COMP9032 Week3 2 Assembly Program Structure Assembly Program Structure (cont.) • An assembly program basically consists of • The label for an instruction is associated with – Assembler directives the memory location address of that • E.g. .def temp = r15 instruction. – Executable instructions • All instructions are not case sensitive • E.g. add r1, r2 – “add” is same as “ADD” • An input line in an assembly program takes one of the following forms : – “.DEF” is same as “.def” – [label:] directive [operands] [Comment] – [label:] instruction [operands] [Comment] – Comment – Empty line S2, 2008 COMP9032 Week3 3 S2, 2008 COMP9032 Week3 4
Example Comments • A comment has the following form: ; The program performs Two comment lines – ;[Text] ; 2-byte addition: a+b; – Items within the brackets are optional Empty line .def a_high = r2; • The text between the comment-delimiter(;) .def a_low = r1; .def b_high = r4; Six assembler directives and the end of line (EOL) is ignored by the .def b_low = r3; .def sum_high = r6; assembler. .def sum_low = r5; mov sum_low, r1 mov sum_high, r3 Four executable instructions add sum_low, r2 adc sum_high, r3 S2, 2008 COMP9032 Week3 5 S2, 2008 COMP9032 Week3 6 Assembly Directives • Assembly directives are instructions to the assembler. They are created for a number of purposes: Summary of – For symbol definitions AVR Assembler • For readability and maintainability directives • All symbols used in a program will be replaced by the real values during assembling • E.g. .def, .set – For program and data organization • E.g. .org, .cseg, .dseg – For data/variable memory allocation • E.g. .DB – For others NOTE: All directives must be preceded by a period S2, 2008 COMP9032 Week3 7 S2, 2008 COMP9032 Week3 8
Directives for Symbol Definitions Directives for Symbol Definitions (cont.) • .DEF • .EQU – Define symbols on registers – Define symbols on values .EQU symbol = expression .DEF symbol = register – E.g. • Un-redefinable. The symbol cannot be redefined for other value in the program .def temp=r17 – E.g. • Symbol temp can be used for r17 elsewhere in the program after the definition .EQU length=2 • Symbol length with value 2 can be used elsewhere in the program after the definition S2, 2008 COMP9032 Week3 9 S2, 2008 COMP9032 Week3 10 Directives for Symbol Definitions Program/Data Memory (cont.) Organization • .SET • AVR has three different memories – Define symbols on values – Data memory – Program memory .SET symbol = expression – EPROM memory • redefinable . The symbol can represent other value • The three memories are corresponding to later. three memory segments to the assembler: – E.g. – Data segment .set input=5 • Symbol input with value 5 can be used elsewhere in the – Program segment (or Code segment) program after this definition and before its redefinition. – EEPROM segment S2, 2008 COMP9032 Week3 11 S2, 2008 COMP9032 Week3 12
Program/Data Memory Example Organization Directives • Memory segment directives specify which .DSEG ; Start data segment memory segment to use .ORG 0x100 ; from address 0x100, ; default start location is 0x0060 – .DSEG • Data segment vartab: .BYTE 4 ; Reserve 4 bytes in SRAM ; from address 0x100 – .CSEG • Code segment .CSEG ; Start code segment – .ESEG ; default start location is 0x0000 • EPROM segment const: .DW 10, 0x10, 0b10, -1 • The .ORG directive specifies the start ; Write 10, 16, 2, -1 in program address to store the related program/data. ; memory, each value takes ; 2 bytes. mov r1,r0 ; Do something S2, 2008 COMP9032 Week3 13 S2, 2008 COMP9032 Week3 14 Directives for Constants Data/Variable Memory Allocation Directives • Store data in program/EEPROM memory – .DB • Specify the memory locations/sizes for • Store byte constants in program/EEPROM memory – Constants Label: .DB expr1, expr2, … • In program/EEPROM memory – expr* is a byte constant value – Variables – .DW • In data memory • Store word constants in program/EEPROM memory • All directives must start with a label so that • little endian scheme is used the related data/variable can be accessed later. Label: .DW expr1, expr2, … – expr* is a word constant value S2, 2008 COMP9032 Week3 15 S2, 2008 COMP9032 Week3 16
Directives for Variables Directives for Others • Reserve bytes in data memory • Include a file – .BYTE – .INCLUDE “m64def.inc” • Reserve a number of bytes for a variable • Stop processing the assembly file – .EXIT Label: .BYTE expr • Begin and end macro definition – .MACRO • expr is the number of bytes to be reserved. – .ENDMACRO – Will be discussed in detail later S2, 2008 COMP9032 Week3 17 S2, 2008 COMP9032 Week3 18 Implement Data/Variables Sample C Program // global variables: • With those directives, you can const char g_course[ ] = "COMP"; char* g_inputCourse = "COMP"; implement/translate data/variables into char g_a; static char g_b; machine level descriptions int main(void){ • An example of translation by WINAVR is // local variables: const char course[ ] = "COMP9032"; given in the next slide. char* inputCourse = "COMP9031"; char a; static char b; char i; char isCOMP9032 = 1; for(i=0; i<9; i++){ if (inputCourse[i] != course[i]){ isCOMP9032 = 0; i = 9; } } return 0; S2, 2008 COMP9032 Week3 19 S2, 2008 COMP9032 Week3 20
Memory mapping after execution Memory mapping after build and run S2, 2008 COMP9032 Week3 21 S2, 2008 COMP9032 Week3 22 Memory Mapping Diagram Remarks Static data • Data have scope and duration in the program 0x0100 • Data have types and structures g_course Constants 0x0104 • Those features determine where and how to 0x0105 g_inputCourse store data in memory. 0x0109 pointer (g_inputCourse) 0x010A 0x10F2 • Constants are usually stored in the non- 0x010B course volatile memory and variables are allocated in constants 0x0115 0x10FA SRAM memory. inputCourse 0x10FAB 0x011D pointer (inputCourse) 0x10FAC b 0x011E • In this lecture, we will only take a look at how g_b 0x011F a 0x10FD to implement basic data type. g_a 0x0120 0x10FE i – Advanced data/variable implementation will be RAMEND isCOMP9032 covered later. Dynamic data S2, 2008 COMP9032 Week3 23 S2, 2008 COMP9032 Week3 24
Example 1 Example 1: Solution • Translate the following variables. Assume • Translate the following C variables. Assume each integer takes four bytes. each integer takes four bytes. .dseg ; in data memory .org 0x100 ; start from address 0x100 int a; unsigned int b; a: .byte 4 ; 4 byte integer char c; b: .byte 4 ; 4 byte unsigned integer char* d; c: .byte 1 ; 1 character d: .byte 2 ; address pointing to the string – All variables are allocated in SRAM – Labels are given the same name as the variable for convenience. S2, 2008 COMP9032 Week3 25 S2, 2008 COMP9032 Week3 26 Example 2 Example 2 (cont.) • Translate the following C constants and • An insight into the memory mapping variables. – In program memory, data are packed in words. If int a; only a single byte left, that byte is stored in high C code: const char b[ ]=“COMP9032”; byte and the low byte is filled with 0. const int c=9032; Hex values .dseg .org 0x100 0x0000 43 4F ‘C’ ‘O’ a: .byte 4 Assembly 0x0001 4D 50 ‘M’ ‘P’ code: 0x0002 39 30 ‘9’ ‘0’ .cseg 33 32 0x0003 ‘3’ ‘2’ b: .DB ‘C’, ‘O’, ‘M’, ‘P’, ‘9’, ‘0’, ‘3’, ‘2’, 0 0x0004 0 0 0 0 C: .DW 9032 48 23 0x0005 9032 – All variables are in SRAM and constants are in FLASH S2, 2008 COMP9032 Week3 27 S2, 2008 COMP9032 Week3 28
Example 3 Example 3 : Solution • Translate data structures • Translate data structures struct { .set student_ID=0 int student_ID; .set name = student_ID+4 char name[20]; .set WAM = name + 20 char WAM; .set STUDENT_RECORD_SIZE = WAM + 1 } STUDENT_RECORD; .dseg typedef struct STUDENT_RECORD *student; s1: .BYTE STUDENT_RECORD_SIZE s2: .BYTE STUDENT_RECORD_SIZE student s1; student s2; S2, 2008 COMP9032 Week3 29 S2, 2008 COMP9032 Week3 30 Example 4 Example 4: Solution • Translate data structures • Translate data structures .set student_ID=0 – with initialization .set name = student_ID+4 .set WAM = name + 20 struct .set STUDENT_RECORD_SIZE = WAM + 1 { int student_ID; .cseg char name[20]; s1_value: .DW HWRD(123456) char WAM; .DW LWRD(123456) } STUDENT_RECORD; .DB “John Smith” .DB 75 .dseg typedef struct STUDENT_RECORD *student; s1: .BYTE STUDENT_RECORD_SIZE s2: .BYTE STUDENT_RECORD_SIZE student s1 = {123456, “John Smith”, 75}; student s2; ; copy the data from instruction memory to s1 … S2, 2008 COMP9032 Week3 31 S2, 2008 COMP9032 Week3 32
Recommend
More recommend