54
Chapter 7 – MSP430 Assembler / Linker

Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

Embed Size (px)

Citation preview

Page 1: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

Chapter 7 – MSP430Assembler / Linker

Page 2: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 2

Concepts to Learn…

MSP430 Assembler High Level vs. Assembly Assembly Code Assembly Process MSP430 Assembler Assembly Directives Assembly Sections Linker Libraries Code Composer Essentials/Studio Systematic Decomposition Device: LED’s

Page 3: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 3

Moving Up Levels of Abstraction

Problems

Algorithms

Language

Machine (ISA) Architecture

Micro-architecture

Circuits

Devices Transistors

Logic gates, multiplexers, memory, etc.

LC-3 Architecture

Machine code

Assembly code

Page 4: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 4

AssemblerAn assembler outputs an object file as input

to a linker program

Assembler

An assembly program is a text file

An assembler translates a program

into machine code

Page 5: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 5

High Level vs. Assembly

High Level Languages More programmer friendly More ISA independent Each high-level statement translates to several

instructions in the ISA of the computer Assembly Languages

Lower level, closer to ISA Very ISA-dependent Each instruction specifies a single ISA instruction Makes low level programming more user friendly More efficient code

High Level vs. Assembly

Page 6: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 6

Why Assembly Code?

Allows us to work at a slightly higher level than machine language.

Allows us to use symbolic names for opcodes Allows us to use symbolic names for memory

locations - SUM, PRODUCT Don’t need to know every address of every storage

location. Calculates addresses for us – really a big deal! Helps to allocate memory locations. Provides additional error checking

High Level vs. Assembly

Page 7: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 7

Assembly Code Example;*******************************************************************************; CS/ECEn 124 Lab 4 - morse.asm: Student Code;*******************************************************************************

.cdecls C,LIST, "msp430x22x4.h“ ; include C header

COUNT .equ 2000 ;------------------------------------------------------------------------------ .data ; data

.bss cnt,2 ; ISR counter ;------------------------------------------------------------------------------ .text ; Program resetRESET: mov.w #0x0280,SP ; Initialize stack pointer

mov.w #WDT_MDLY_0_5,&WDTCTL ; Set Watchdog interval to ~0.5msmov.b #WDTIE,&IE1 ; Enable WDT interruptbis.b #0x01,&P1DIR ; P1.0 outputbis.b #0x20,&P4DIR ; P4.0 outputmov.w #COUNT,&cnt ; initialize counterbis.w #LPM0+GIE,SR ; Enter LPM0 w/ interruptjmp $ ; Loop forever; interrupts do all

; Watchdog Timer interrupt service routine;WDT_ISR: xor.b #0x20,&P4OUT ; pulse buzzer

dec.w &cnt ; decrement counter jne WDT_exitmov.w #COUNT,&cnt ; initialize counterxor.b #0x01,&P1OUT ; toggle P1.0

WDT_exit: reti ; return from interrupt

.sect ".int10" ; MSP430 RESET Vector .word WDT_ISR ; Watchdog ISR

.sect ".reset" ; MSP430 RESET Vector

.word RESET ; Power Up ISR

.end

Assembly Code

Labels

Instructions

Comments

Directives

Page 8: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 8

Assembly Syntax

The MSP430 assembler reads up to 200 characters per line. Any characters beyond 200 are truncated.

Follow these guidelines: All statements must begin with a label, a blank, an asterisk, or a

semicolon Labels are optional; if used, they must begin in column 1 One or more blanks must separate each field. Tab and space

characters are blanks. Comments are optional. Comments that begin in column 1 can

begin with an asterisk or a semicolon (* or ;), but comments that begin in any other column must begin with a semicolon.

A mnemonic cannot begin in column 1 or it will be interpreted as a label

Assembly Code

[label[:]]mnemonic [operand list][;comment]

Page 9: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 9

Assembly Syntax

Label Field Labels are optional for all assembly language instructions and

for most (but not all) assembler directives When used, a label must begin in column 1 A label can contain up to 128 alphanumeric characters (A-Z, a-

z, 0-9, _, and $) Labels are case sensitive and the first character cannot be a

number A label can be followed by a colon (:). (If you do not use a label,

the first character position must contain a blank, a semicolon, or an asterisk)

The value of a label is the current value of the Location Counter A label on a line by itself is a valid statement If you do not use a label, the character in column 1 must be a

blank, an asterisk, or a semicolon

Assembly Code

Page 10: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 10

Assembly Syntax

Mnemonic Field The mnemonic field follows the label field. The mnemonic field cannot start in column 1; if it does, it is interpreted as a label. The mnemonic field can begin with one of the following items:

Machine-instruction mnemonic (such as ADD, MOV, JMP) Assembler directive (such as .data, .list, .equ) Macro directive (such as .macro, .var, .mexit) Macro call

Assembly Code

Page 11: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 11

Assembly Syntax

Operand Field The operand field follows the mnemonic field and contains one

or more operands. The operand field is not required for all instructions or directives. An operand consists of the following items:

Symbols Constants Expressions (combination of constants and symbols)

You must separate operands with commas You use immediate values as operands primarily with

instructions Use the # prefix to define an immediate value

Assembly Code

Page 12: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 12

Assembly Syntax

Constants The assembler maintains constants internally as a 32-bit

quantities Constants are not sign extended (ie, 00FFh is equal to 00FF

(base 16) or 255 (base 10); however, it does not equal -1 unless a byte directive)

Types of constants: Decimal: string of decimal digits ranging from (-2147483648 to

4294967295 (ie, 1000, -32768) Hexadecimal: string of up to 8 hexadecimal digits followed by suffix

‘H’ (or ‘h’) or preceded by ‘0x’ (ie, 78h, 0x78) Binary: string of upt to 32 binary digits followed by suffix B (or b) (ie.

0000b, 11110000B)

Assembly Code

Page 13: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 13

Assembly Syntax

Expressions An expression is a constant, a symbol, or a series of constants and

symbols separated by arithmetic operators. The 32-bit ranges of valid expression values are -2147 483 648 to

2147 483 647 for signed values, and 0 to 4 294 967 295 for unsigned values.

Three main factors influence the order of expression evaluation: Parentheses Expressions enclosed in parentheses are always

evaluated first. (You cannot substitute braces ( { } ) or brackets ( [ ] ) for parentheses)

8 / (4 / 2) = 4, but 8 / 4 / 2 = 1 Precedence groups Operators: when parentheses do not determine the

order of expression evaluation, the highest precedence operation is evaluated first.

8 + 4 / 2 = 10 (4 / 2 is evaluated first) Left-to-right evaluation: when parentheses and precedence groups do

not determine the order of expression evaluation, the expressions are evaluated from left to right

Assembly Code

Page 14: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 14

Assembly Syntax

Operators can be used in expressions and are evaluated according to precedence group

Assembly Code

Group Operator Description1 +, -, ~, ! Unary plus, minus, 1’s complement,

Logical NOT2 *, /, % Multiplication, Division, Modulo3 +, - Addition, Subtraction4 <<, >> Shift left, Shift right5 <, <=, >, >= Less than, Less than or equal to, Greater

than, Greater than or equal to6 =[=], != Equal to, Not equal to7 & Bitwise AND8 ^ Bitwise exclusive OR (XOR)9 | Bitwise OR

Page 15: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 15

Assembler Directives

Assembly directives are used to specify: Starting addresses for programs Starting values for memory locations Specify the end of program text.

;*******************************************************************************; CS/ECEn 124 Example Code;*******************************************************************************

.cdecls C,LIST, "msp430x22x4.h“ ; include C header

COUNT .equ 2000 ;------------------------------------------------------------------------------ .data ; data

.bss cnt,2 ; ISR counter ;------------------------------------------------------------------------------ .text ; Program resetRESET: mov.w #0x0280,SP ; Initialize stack pointer

mov.w #WDT_MDLY_0_5,&WDTCTL ; Set Watchdog interval to ~0.5msbis.w #LPM0+GIE,SR ; Enter LPM0 w/ interruptjmp $ ; Loop forever; interrupts do all

.sect ".reset" ; MSP430 RESET Vector

.word RESET ; Power Up ISR

.end

Directives

Assembly Code

Page 16: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 16

Assembly Process

The assembler translates assembly language programs (.asm) into the machine language of the ISA (.obj).

There is a 1-to-1 correspondence between assembly language instructions and instructions in the final machine language.First Pass: find all labels and their corresponding addresses

this information is stored in the symbol table

Second Pass: convert instructions to machine language, using

information from symbol table

Assembly Process

Page 17: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 17

1st Pass: Construct Symbol Table

1. Find the .text statement, which tells us the address of the first instructiona. Initialize location counter (LC)b. Incremented for each new instruction

2. For each non-empty line in the program:a. If line contains a label, add label and LC to symbol

table.b. Increment LC (according to instruction length.)

1. All instructions are 1, 2, or 3 words in length2. Some directives like .bss or .string increment LC by the size of the operand.

3. Stop when .end statement is reached.

Assembly Process

Page 18: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 18

2nd Pass: Generate Machine Language

1. Reset location counter (LC)

2. For each executable assembly language statement, generate the corresponding machine language instruction.a. resolve labels referenced in instructions using the

symbol tableb. increment LC for each instruction as on pass 1c. output resulting machine code to output files

3. Stop when .end statement is reached.

Assembly Process

Page 19: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 19

Assembly Style Guidelines

Provide a program header, with author’s name, date, etc.,and purpose of program.

Start labels, opcode, operands, and comments in same column for each line. (Unless entire line is a comment.)

Use comments to explain what each register does. Remember, the assembler is case sensitive. Use meaningful symbolic names.

Mixed upper and lower case for readability. ASCIItoBinary, InputRoutine, SaveR1

Provide comments between program sections. Each line must fit on the page -- no wraparound or

truncations. Long statements split in aesthetically pleasing manner.

Assembly Process

Page 20: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 20

MSP430 Assembler

The MSP430 assembler translates your source code into machine language

Source files may contain the following elements: Assembly directives Macro directives Assembly language instructions

For more detailed information on each element, please refer to the MSP430 Assembly Language Tools User’s Guide (slau131b.pdf)

Some of the most relevant aspects of the assembly language for the MSP430 will now be introduced

MSP430 Assembler

Page 21: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 21

Stoplight ExampleMSP430 Assembler

;*******************************************************************************; CS/ECEn 124 Lab 4 - stoplight.asm: Software Toggle P1.0;******************************************************************************* .cdecls C,LIST, "msp430x22x4.h" ; MSP430F2274

;------------------------------------------------------------------------------ .text ; beginning of executable code;------------------------------------------------------------------------------RESET: mov.w #0x0280,SP ; init stack pointer mov.w #WDTPW+WDTHOLD,&WDTCTL ; stop WDT bis.b #0x01,&P1DIR ; set P1.0 as output

mainloop: xor.b #0x01,&P1OUT ; toggle P1.0 mov.w #0,r15 ; use R15 as delay counter

delayloop: dec.w r15 ; delay over? jnz delayloop ; n jmp mainloop ; y, toggle led

;------------------------------------------------------------------------------; Interrupt Vectors;------------------------------------------------------------------------------ .sect ".reset" ; MSP430 RESET Vector .short RESET ; start address .end

Page 22: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 22

MSP430 Assembler

The assembly programming tool processes the source code, producing an object file, and a descriptive listing of the entire process

This process is completely controlled by macros, allowing conditional execution

The MPS430 source code programs contains sequences of statements that have: Assembly directives Assembly instructions Macros, and Comments.

MSP430 Assembler

Page 23: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 23

Assembler Syntax

A line can have four fields label, mnemonic, operand list, and comment

The general syntax is:

{label[:]} mnemonic {operand list} {;comment}

Some line examples are:

.sect ".sysmem“ ; Data Spacevar1 .word 2 ; variable var1 declaration

.text ; Program SpaceLabel1: mov r4,r5 ; move R4 contents to R5

.end

MSP430 Assembler

Page 24: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 24

Coding Guidelines

The general coding guidelines: All statements must begin with a label, a blank, an

asterisk, or a semicolon Labels are optional. If used, they must begin in

column 1 One or more blanks or tabs must separate each field Comments are optional Comments that begin in column 1 can begin with an

asterisk or a semicolon (* or ;), but comments that begin in any other column must begin with a semicolon

A mnemonic cannot begin in column 1 or it will be interpreted as a label.

MSP430 Assembler

Page 25: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 25

Constants

The assembler supports several formats for constants:

Binary integer: 11110000b 0xf0 Octal integer: 226q 0x96 Decimal integer: 25 0x19 Hexadecimal integer: 078h 0x78 Character: a 'a' Assembly time: value1 value1 .set

3

MSP430 Assembler

Page 26: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 26

Symbols

Symbols are used as labels, constants, and substitution symbols

A symbol name is a string of up to 200 alphanumeric characters (A-Z, a-z, 0-9, $, and _)

A symbol cannot contain embedded blanks and the first character cannot be a number

Symbols are case sensitive Symbols used as labels become symbolic

addresses that are associated with locations in the program

Labels used locally within a file must be unique.

MSP430 Assembler

Page 27: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 27

Assembler directives supply data to the program and control the assembly process

Assembler directives enable you to: Assemble code and data into specified sections Reserve space in memory for uninitialized variables Control the appearance of listings Initialize memory Assemble conditional blocks Define global variables Specify libraries from which the assembler can obtain

macros Examine symbolic debugging information.

Assembler DirectivesAssembly Directives

Page 28: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 28

Section Assembler Directives

Assembler directives that define sections:

Assembler Directives

Page 29: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 29

Assembler Directives

Assembler directives that initialize constants (data and memory):

Assembler Directives

Page 30: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 30

Assembler Directives

Assembler directives that perform alignment and reserve space:

Assembler Directives

Page 31: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 31

Assembler Directives

Assembler directives that format the output listing:

Assembler Directives

Page 32: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 32

Assembly List File

A line in a listing file has four fields: Field 1: contains

the source code line counter

Field 2: contains the section program counter

Field 3: contains the object code

Field 4: contains the original source statement.

Assembler Directives

Page 33: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 33

Assembler Directives

Assembler directives that reference other files:

Assembler Directives

Page 34: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 34

Assembler Directives

Assembler directives that enable conditional assembly:

Assembler Directives

Page 35: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 35

Assembler Directives

Assembler directives that define structures:

Directives that define symbols at assembly time:

Assembler Directives

Page 36: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 36

Assembler Directives

Assembler directives that perform miscellaneous functions:

Assembler Directives

Page 37: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 37

Assembler Sections

The smallest unit of an object file is called a section

A section is a block of code or data that occupies contiguous space in the memory map

Each section of an object file is separate and distinct.

Object files contain three default sections: .text section contains executable code .data section contains initialized data .bss section reserves space for uninitialized variables.

Assembler Sections

Page 38: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 38

Linker

Example: process of linking two files together:

Linker

Page 39: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 39

Library Routines

Library A set of routines for a specific domain application. Example: math, graphics, GUI, etc. Defined outside a program.

Library routine invocation Labels for the routines are defined as .def Each library routine contains its own symbol table. A linker resolves the external addresses before

creating the executable image.

Libraries

Page 40: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 40

Linking multiple files

Libraries

Page 41: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 41

Code Composer

Based on Eclipse framework Open source enabling rapid innovation

Allows integration with other compilers, plugins One stop shop with TI

MSP430 + FET Programmer + CCE Support

Direct from TI Eclipse Community Third Parties

Low cost solution (Free!)

Code Composer

Page 42: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 42

CCE Window – C/C++ PerspectiveIndependent debugging and Programming view

1-click project debug

Project Outline • Shortcut to all parts of the project

ConsoleBuild information

Problems View • Information• Warnings• Errors

Project View• List of all Projects

Code Window • Real-time breakpoints• Syntax highlighting

Code Composer

Page 43: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 43

CCE Window – Debug PerspectiveIndependent debugging and Programming view

1-click project debug

Real-time, in-system MSP430 information• Register access• Flash, RAM, Info segment access• Disassembly view

Easy to viewprogram size info

Highly configurablewindow layout • User preferences• Plugin support

Target control• Start, stop, halt• Single stepping• Stack trace

CPU Cycle counter

Code Window • Real-time breakpoints• Syntax highlighting

Code Composer

Page 44: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 44

The MSP430 Assembler

To create a new Assembly language project: In File -> New Project choose Managed Make C/ASM

Project (Recommended) Assign a name to the project in Project Name, (e.g.,

stoplight) Choose Project Type: MSP430 Executable In Additional Project Setting, do not choose any

connection with other projects In the Device Selection page, select the target device

MSPF2013 or MSP430F2274 Select configuration option: Assembly only Project

At the end of this sequence of operations, a project named stoplight is opened in the work environment

Code Composer

Page 45: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 45

The MSP430 Assembler

Assembly project (continued): Assign a new source code file to the project. In the

option Menu > File > Source File and create a new file called stoplight.asm

In the project properties menu, set the entry point as identified by the label start

This option is found on page Build C/C++ build > MSP430 Linker V3.0 > Symbol Management > Specify the program entry point for the output model (-entry point).

Code Composer

Page 46: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 46

The MSP430 Assembly Listing

To generate an assembly listing: Project -> Properties C/C++ Build -> Tool Settings -> MSP430 Compiler v3.x Assembler Options: Check Generate listing file (--asm_listing)

To define program entry point: Project -> Properties C/C++ Build -> Tool Settings -> MSP430 Linker v3.x Symbol Management: Specify the program entry point for the output model (-- entry point) Enter the start label

Code Composer

Page 47: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 47

Systematic Decomposition

Finiteness Must terminate.

Definiteness Each step is precisely stated.

Effective Computability Each step can be carried out.

IDEA Step by Step Procedure

Systematic Decomposition

Page 48: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 48

Stepwise Refinement

Also known as incremental development. Start with problem statement:

“Write an assembler program for a traffic stop light.” Decompose task into a few simpler subtasks.

Turn on the green LED for 5 seconds. Blink the green LED on and off at 1 second intervals for

6 seconds (3 offs and 3 ons). Blink the green LED on and off at 0.25 second intervals

for 4 seconds (8 offs and 8 ons). And finally, turn the green LED off for 10 seconds.

Repeat the process of dividing into subtasks until you get to the machine instruction level.

Systematic Decomposition

Page 49: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 49

F2274 Stoplight Example

;*******************************************************************************; CS/ECEn 124 Lab 4 - stoplight.asm: Software Toggle P1.0;******************************************************************************* .cdecls C,LIST, "msp430x22x4.h" ; MSP430F2274

;------------------------------------------------------------------------------ .text ; beginning of executable code;------------------------------------------------------------------------------RESET: mov.w #0x0280,SP ; init stack pointer mov.w #WDTPW+WDTHOLD,&WDTCTL ; stop WDT bis.b #0x01,&P1DIR ; set P1.0 as output

mainloop: xor.b #0x01,&P1OUT ; toggle P1.0 mov.w #0,r15 ; use R15 as delay counter

delayloop: dec.w r15 ; delay over? jnz delayloop ; n jmp mainloop ; y, toggle led

;------------------------------------------------------------------------------; Interrupt Vectors;------------------------------------------------------------------------------ .sect ".reset" ; MSP430 RESET Vector .short RESET ; start address .end

Systematic Decomposition

Page 50: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 50

LEDs

A light-emitting diode (LED) is a semiconductor light source

When a diode is forward biased (switched on), electrons are able to recombine with holes within the device, releasing energy in the form of photons

Device: LED

Page 51: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 51

LEDs

6 LED’s on eZ430X Development Board P1.0 Red LED eZ430-RF2500 P1.1 Green LED eZ430-RF2500 P2.6 LED #1 (Green) P2.7 LED #2 (Orange) P3.3 LED #3 (Yellow) P4.6 LED #4 (Red)

Port bits must be enabled for output by writing a 1 to the port direction register

bis.b #0x03,&P1DIR ; eZ430-RF2500 LED's bic.b #0xc0,&P2SEL ; select GPIO bis.b #0x40,&P2DIR ; LED #1 (P2.6) bis.b #0x80,&P2DIR ; LED #2 (P2.7) bis.b #0x08,&P3DIR ; LED #3 (P3.3) bis.b #0x40,&P4DIR ; LED #4 (P4.6)

Device: LED

Page 52: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 52

LEDs

Turn LED off by writing a 0 to the port pin bic.b #0x03,&P1OUT ; eZ430-RF2500 LED's bic.b #0x40,&P2OUT ; LED #1 (P2.6) bic.b #0x80,&P2OUT ; LED #2 (P2.7) bic.b #0x08,&P3OUT ; LED #3 (P3.3) bic.b #0x40,&P4OUT ; LED #4 (P4.6)

Turn LED on by writing a 1 to the port pin bis.b #0x03,&P1OUT ; eZ430-RF2500 LED's bis.b #0x40,&P2OUT ; LED #1 (P2.6) bis.b #0x80,&P2OUT ; LED #2 (P2.7) bis.b #0x08,&P3OUT ; LED #3 (P3.3) bis.b #0x40,&P4OUT ; LED #4 (P4.6)

Toggle LED by XOR’ing a 1 to the port pin xor.b #0x03,&P1OUT ; eZ430-RF2500 LED's xor.b #0x40,&P2OUT ; LED #1 (P2.6) xor.b #0x80,&P2OUT ; LED #2 (P2.7) xor.b #0x08,&P3OUT ; LED #3 (P3.3) xor.b #0x40,&P4OUT ; LED #4 (P4.6)

Device: LED

Page 53: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 53

Page 54: Chapter 7 – MSP430 Assembler / Linker. BYU CS/ECEn 124Chapter 7 - MSP430 Assembler2 Concepts to Learn… MSP430 Assembler High Level vs. Assembly Assembly

BYU CS/ECEn 124 Chapter 7 - MSP430 Assembler 54

Most Commonly Used Directives

Mnemonic and Syntax Description

.bss symbol, size in bytes[, alignment] Reserves size bytes in the .bss (uninitialized data) section

.data Assembles into the .data (initialized data) section

.sect " section name" Assembles into a named (initialized) section

.text Assembles into the .text (executable code) section

.byte value1[, ..., valuen] Initializes one or more successive bytes in the current section

.char value1[, ..., valuen] Initializes one or more successive bytes in the current section

.string {expr1|"string1"}[,... , {exprn|"stringn"}] Initializes one or more text strings

.word value1[, ... , valuen] Initializes one or more 16-bit integers

.align [size in bytes] Aligns the SPC on a boundary specified by size in bytes, which must be a power of 2; defaults to word (2 byte)

.title " string " Prints a title in the listing page heading

.def symbol1[, ... , symboln] Identifies one or more symbols that are defined in the current module and that can be used in other modules

.include ["]filename["] Includes source statements from another file

.ref symbol1[, ... , symboln] Identifies one or more symbols used in the current module that are defined in another module

.else Assembles code block if the .if well-defined expression is false. (Optional)

.elseif well-defined expression Assembles code block if the .if well-defined expression is false and the .elseif condition is true. (Optional)

.endif Ends .if code block

.if well-defined expression Assembles code block if the well-defined expression is true

symbol .equ value Equates value with symbol

symbol .set value Equates value with symbol

.cdecls [options,] "filename"[, "filename2"[, ...] Share C headers between C and assembly code

.end Ends program