



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Milenkovic; Class: INTRO TO EMBEDDED COMPUTER SYS; Subject: Computer Engineering; University: University of Alabama - Huntsville; Term: Summer 2009;
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




2. What do assemblers do?
Assemblers typically provide the following capabilities.
Assembly Language Syntax
Each assembler has its own unique syntactical structure (use of uppercase or lowercase, label
definitions, token separators, etc). In spite of this, all assemblers share some common features.
#include "msp430.h" ; #define controlled include file
ORG 0FF00h myStr: DB "HELLO WORLD, I AM THE MSP430!" ; the string is placed on the stack ; the null character is automatically added after the '!'
NAME main ; module name
PUBLIC main ; make the main label visible ; outside this module ORG 0FFFEh DC16 init ; set reset vector to 'init' label
RSEG CSTACK ; pre-declaration of segment RSEG CODE ; place program in 'CODE' segment
init: MOV #SFE(CSTACK), SP ; set up stack
main: NOP ; main program MOV.W #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer BIS.B #0FFh,&P1DIR ; configure P1.x output MOV.W #myStr, R4 ; load the starting address of the string into the register R CLR.B R5 ; register R5 will serve as a counter gnext: MOV.B @R4+, R6 ; get a new character CMP #0,R JEQ lend ; go to the end CMP.B #'E',R JNE gnext INC R5 ; increment counter JMP gnext
lend: MOV.B R5,&P1OUT ; Set all P1 pins BIS.W #LPM4,SR ; LPM NOP ; Required only for debugger
END
Figure 3. MSP430 Assembly Code for Count Character Program.
Here is a short description of the assembly code.
C-style /* comment */ notation.
be included in the source. The header file includes all macro definitions, for example, special function register addresses (WDTCTL), and control bits (WDTPW+WDTHOLD).
the value of an expression that follows. Here ORG 0FF00h sets the location counter at the
absolute address 0FF00h. This means that location counter is moved to this address.
myStr DB "HELLO WORLD, I AM THE MSP430!". As explained, this directive will allocate
30 bytes in memory starting at the address 0FF00h and initialize it with the string content.
The content in memory will be as follows: 48 45 4c 4c 4f 20 57 4f 52 4c 44 2c 20 49 20 41
4d 20 54 48 45 20 4d 53 50 34 33 30 21 00.
logic always generates a RESET interrupt request (it is the highest priority interrupt request).
The value stored at the address 0xFFFE (the last word in the 64KB address space) is reserved
to keep the starting address of the reset handler (interrupt service routine), and the first thing
that the microcontroller does is to fetch the content from this address and put it in the
program counter (PC, register R0). Thus, the starting address of our program should be stored at location 0xFFFE. Here, we move location counter to 0xFFFE and allocate 2 bytes
(DC16 allocates 16 bits or two bytes) that will be initialized with the starting address of the
main program. The starting address of the main program is marked by the label init.
memory. RSEG is used to mark the beginning of a relocatable code or data segment. CODE
and DATA are recognized segment types that are resolved by the linker. The IAR XLINK
linker can recognize any other type of segment (e.g., CSTACK for program stack).
does not use the stack, so we could have omitted RSEG CSTACK and this instruction.
timer control register (WDTCTL) to disable it. The watchdog timer by default is active upon reset, generating interrupt requests periodically. As this functionality is not needed in our
program, we simply need to disable it.
register PxDIR determines whether the port x is input or output (we can configure each
individual port pin). Our program drives all pins of the port P1, so it should be configured as the output (P1DIR=0xFF). Register R4 is loaded to point to the first character in the string.
Register R5, the counter, is cleared before starting the main program loop.
new character (one byte) from the string (MOV.B @R4+, R6). The current character is kept in
register R6. We compare the current character with the NULL character (CMP #0, R6). If it
is the NULL character it is the end of the string and we exit the loop (JEQ lend). Pay
attention that we used JEQ instruction? Why?
If it is not the end of the string, we compare the current character with ‘E’. If there is no
match we go back on the first instruction in the loop. Otherwise, we increase the value of the
counter (register R5).
Finally, we move the lower byte from R5 to the parallel port 1, P1OUT=R5[7:0].
Note: Absolute vs. Relocatable Assemblers
Absolute assemblers. When writing assembly programs we know from our hardware design
where the code is to be located in memory. A special directive ORG provides this information to
the assembler. All code is located absolutely at a specific memory address. The simplest form of
assembler is so called absolute assembler. It takes the source assembly file and produces an
executable file that is downloaded to the target system.
Disadvantages of absolute assemblers are as follows: all code must be in one module which is
inconvenient for larger group projects; program becomes less portable across different platforms.