Lecture 3 C & Assembly - Yeditepe Üniversitesi Bilgisayar...

27
Lecture 3 C & Assembly 1

Transcript of Lecture 3 C & Assembly - Yeditepe Üniversitesi Bilgisayar...

Lecture 3C & Assembly

1

C

• This is now the language of choice for smallmicrocontrollers, for many good reasons.

• Its built-in and user-defined types, data structures, andflexible control flow make it far more efficient andreliable to write in C than assembly language.

• At the same time, it is a sufficiently low-level languagethat compilers can use to produce efficient code.

• This is aided by the architecture of modern processors,such as the MSP430, which are designed withcompilers in mind rather than for hand-craftedassembly code.

2

Declarations in C

• const– Means that the value should not be modified: it is constant.

• volatile– Means that a variable may appear to change “spontaneously,”

with no direct action by the user’s program. The compiler must therefore not keep a copy of the variable in a register for efficiency (like a cache).

– Nor can the compiler assume that the variable remains constant when it optimizes the structure of the program—rearrangingloops, for instance. If the compiler did either of these, the program might miss externally induced changes to the contents of the memory associated with the variable.

3

example

• The peripheral registers associated with the input ports must obviously be treated as volatile in an embedded system. The values in these depend on the settings of the switches or whatever is connected to the port outside the MCU.

• Clearly the compiler must not assume that these never change. Here is a simple example, where the program waits for the value on its input port P1IN to change from the value OldP1IN. The loop would be pointless, and would be optimized out of existence, if P1IN were not declared volatile.

while (P1IN == OldP1IN) { // wait for change in P1IN}

4

shifts & rotations

• value <<= 1 to shift value left by one place.

• Right-shifts work differently for signed and unsigned numbers.

• Logical shift inserts zeroes for both right and left shifts.

• Arithmetic shift inserts zeroes for left shifts but the most significant bit, which carries the sign, is replicated for right shifts.

• Rotation does not introduce or lose any bits; bits that are moved out of one end of the register are passed around to the other.

5

shifts & rotations

6

Low-Level Logic Operations

• It is essential to distinguish between the Boolean logic operations and their bitwise counterparts:

• The Boolean form, such as the AND operation in if (A && B), treats A and B as single Boolean values. Typically A = 0 means false and A = 0 means true. (A bool type in C++ and C99 can be used by including the header file stdbool.h.)

• In contrast, the bitwise operator & acts on each corresponding pair of bits in A and B individually. This means that eight operations are carried out in parallelif A and B are bytes. For example, if A = 10101010 and B = 11001100,then A & B = 10001000.

7

Masks to Test Individual Bits

8

Bit fields

__no_init volatile union {unsigned short TACTL; // Timer_A Controlstruct {

unsigned short TAIFG : 1; // Timer_A counter interrupt flagunsigned short TAIE : 1; // Timer_A counter interrupt enableunsigned short TACLR : 1; // Timer_A counter clearunsigned short : 1;unsigned short TAMC : 2; // Timer_A mode controlunsigned short TAID : 2; // Timer_A clock input dividerunsigned short TASSEL : 2; // Timer_A clock source selectunsigned short : 6;

} TACTL_bit;} @ 0x0160; //@ 0x0160, which is an extension to standard C that fixes the

address of the variable.

9

10

MSP430 Software Development Tools

The MSP430™ is supported by a set of software development tools, which includes an optimizing C/C++ compiler, an assembler, a linker, and assorted utilities.

The MSP430 is supported by the following assembly language development tools:• Assembler• Archiver• Linker• Library information archiver• Absolute lister• Cross-reference lister• Object file display utility• Disassembler• Name utility• Strip utility• Hex conversion utility

11

Assembly Language

• There are a few cases, notably interrupt service routines, where it may still beworthwhile to use assembly for its efficiency. A few operations cannot be representeddirectly in C and may therefore be much faster in assembly, including bitwise rotations,binary-coded-decimal arithmetic and detailed manipulation of registers. On the other hand,most C compilers offer intrinsic functions for these operations.

12

Layout of Assembly Language

The four parts are1. label—starts in the first column and may be followed by a

colon (:) for clarity.2. operation—either an instruction, which is translated into

binary machine codefor the processor itself, or a directive, which controls the

assembler.3. operands—data needed for this operation (not always

required).4. comment—the rest of the line, following a semicolon (;).

13

Light LED (IAR)

14

• .w or .b

• addressing modes

• A single character denotes the mode in the operand.• immediate, #: The value itself (word or byte) is given and

stored in the word

following the instruction. This is also known as a literal value.

• absolute, &: The address of a register in memory space is given and stored in the

word following the instruction.

• register, R: This specifies one of the 16 registers in the CPU.

15

16

Led Blink (CCS)

17

Segment control IAR vs. CCS

18

Sharing C/C++ Header Files With Assembly Source (IAR vs. CCS)

• The IAR A430 assembler supports certain C/C++ preprocessor directives directly and, thereby, allowsdirect including of C/C++ header files such as the MSP430 device-specific header files (msp430xxxx.h)into the assembly code:

#include "msp430x14x.h" // Include device header file

• With the CCS Asm430 assembler, a different scheme that uses the .cdecls directive must be used. Thisdirective allows programmers in mixed assembly and C/C++ environments to share C/C++ headers containing declarations and prototypes between the C/C++ and assembly code:

.cdecls C,LIST,"msp430x14x.h" ; Include device header file

19

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 with other sections. Each section of an object file is separate and distinct. Object files usually contain three default sections:

.text section usually contains executable code

.data section usually contains initialized data

.bss section usually reserves space for uninitialized variables

• In addition, the assembler and linker allow you to create, name, and link named sections that are used like the .data, .text, and .bsssections.

• Named sections are created with .sect (initialized)• Named sections are created with .usect (uninitialized)

20

Partitioning Memory into LogicalBlocks

21

Uninitialized sections

• Reserve space in the memory• The initialized section directives (.text, .data, and .sect)

tell the assembler to stop assembling into the current section and begin assembling into the indicated section.

• The .bss and .usect directives, however, do not end the current section and begin a new one; they simply escape from the current section temporarily. The .bssand .usect directives can appear anywhere in an initialized section without affecting its contents.

22

Initialized sections

• Initialized sections contain executable code or initialized data. The contents of these sections are stored in the object file and placed in MSP430 memory when the program is loaded. Each initialized section isindependently relocatable and may reference symbols that are defined in other sections. The linkerautomatically resolves these section-relative references.

• Three directives tell the assembler to place code or data into a section.

23

Named sections

24

Forexample, you create a subsection called _func within the .text section:.sect ".text:_func"

Section Program Counters

• The assembler maintains a separate program counter for each section. These program counters are known as section program counters, or SPCs.

• An SPC represents the current address within a section of code or data. Initially, the assembler sets each SPC to 0. As the assembler fills a section with code or data, it increments the appropriate SPC. If you resume assembling into a section, the assembler remembers the appropriate SPC's previous value and continues incrementing the SPC from that value.

• The assembler treats each section as if it began at address 0; the linker relocates each section according to its final location in the memory map.

25

26

•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.

Default Allocation for MSP430

27