You are on page 1of 81

MSP430s Instruction Set

By
Dr. Andhe Pallavi
RNSIT
Features of MSP430s Instruction set
RISC architecture
27 core instructions
24 emulated instructions
orthogonal instruction set
all addressing modes can be used with
all instructions and registers
three core-instruction formats
Double operand
Single operand
Program flow control - Jump
Layout of an Assembly Language
Instruction
Copy the contents of the source to
destination
mov.w src ,dst
Byte, word and address instructions are
accessed using the .B, .W or .A extensions.
If the extension is omitted, the instruction is
interpreted as a word instruction.
Addressing Modes
seven addressing modes for the
source operand and
four addressing modes for the
destination operand

1. Register Mode
MOV R4, R5
Before: R4=6004h R5=1234h
After: R4=6004h R5=6004h
2. Indexed mode
X(Rn) -- X is a constant , Rn -CPU
registers
absolute memory location X+Rn is
addressed
Useful for lookup tables
MOV F000h(R5), R4
Before: R4=6004h R5=070Ah
Loc:0xF70A=0123h
After: R4=0123h R5=070Ah
Loc:0xF70A=0123h
3. Symbolic mode (PC Relative)
program counter PC is used as the base
address,
the constant is the offset to the data from
the PC.
X(PC) -- (PC + X) points to operand
MOV XPT, YPT
Move the content of source address XPT (x
pointer) to the destination address YPT (y
pointer).
Before: XPT=6004h Location YPT=070Ah
After: XPT= 6004h Location YPT=6004h

4. Absolute mode
direct addressing mode
label is preceded by &
MOV &XPT, &YPT
5. Indirect register mode
MOV @(R4), R5
not valid for destination operands
emulated with the indexed mode
format 0(Rn)
6. Indirect auto increment mode
@Rn+ -- operand is incremented
MOV @R4+, R5
Similar to indirect register mode,
not valid for destination operands

7. Immediate mode
MOV #0A2h, R5
not valid for destination operands
Double operand core
instructions
Arithmetic instructions

Logical and register control
instructions

Data instructions
1. Arithmetic instructions
2. Logical and register control
instructions
Masks & usage of BIC & BIS
Masks are used to test/affect individual
bits.
A pattern used to select bits is called a
mask and has all bits zero, except for a 1
in the position we want to select.
For example, the standard definition for
selecting bit 3 is BIT3 = 00001000
Similarly ~BIT3 = 11110111 using these
masks only one bit is affected, the rest
remain unchanged
BIS
We have BIT3 = 00001000

Now set bit 3 of port 1 with
P1OUT = P1OUT | BIT3,

bis.b #BIT3, &P2OUT

which leaves the values of the other
bits unchanged.
BIC
Clearing a bit (to 0) is done using AND,
which requires
0-to clear & 1- to leave unchanged
the mask should have all ones except for
the bit that we wish to clear,
so we use ~BIT3= 11110111 rather than
BIT3 for using AND
Using BIC we can use the mask directly
as bic.b #BIT3, &P1OUT
Note: BIC(.B or .W) src, dst implies
.not.src .and. dst dst
3 Data instructions
Single operand core
instructions
Logical and register control
instructions
Program flow control instructions
Program flow control Jumps
1. Logical and register control
instructions
2. Program flow control instructions
Program flow control Jumps
Emulated instructions
1. Arithmetic instructions
2. Logical and register control
instructions
3. Data instructions
4. Program flow control
Shift and Rotate Instructions
No logical
shift in
MSP430
Programming in ALP
1. Data Transfer - Block move,
Exchange, Sorting.
2. Arithmetic Instructions - Addition/
subtraction & Logical Instructions
3. Programs to generate delay &
Counters
4. Code conversion: ASCII to hexa-
decimal conversion
Program 2 - Byte data transfer
Problem definition
Write an assembly language
program to transfer n =10 bytes of
data from location 20h to location
40h (without overlap).
ALP Program
main: NOP ; main program

; Stop watchdog timer
MOV.W #WDTPW+WDTHOLD, &WDTCTL

MOV #0x20,R4 ;Initialize source pointer
MOV #0x40,R5 ;Initialize destination pointer
MOV.B #10, R6 ; Initialize counter

NEXT: MOV.B 0(R4),0(R5) ;Move a byte of
data from source to destination
INC R4 ; Increment source pointer
once (byte transfer)

INC R5 ;Increment destination pointer
DEC.B R6 ; Decrement Counter
JNZ NEXT ; Repeat till counter is zero

JMP $ ; jump to current location '$'
; (endless loop)
END
Result:
Use View -> Memory
Initial view of memory




Fill source locations i.e., locations from 20H
onwards with some arbitrary data as shown
below before execution
Keep the cursor (using the mouse) at the
JMP $ instruction & execute the program
using Debug -> Run to Cursor
After execution observe the contents of the
destination memory locations 40h onwards
Program 3 Block words transfer
For byte transfers
MOV.B 0(R4),0(R5)
INC R4; INC R5

Changed to word as
MOV 0(R4),0(R5);
INCD R4
INCD R5
Program 4 Data transfer with overlap
Write an assembly language program
to transfer n =10 bytes of data from
location 20h to location 25h (with
overlap)
Solution: start transferring data
from the last location of source
array to the last location of the
destination array
Memory window before & after
execution
ALP
main: MOV.W #WDTPW+WDTHOLD, &WDTCTL
MOV #0x29, R4 ;End address of
source string=start address + n

MOV #0x2E,R5 ;End address of
destination string

MOV.B #10, R6 ;counter
Cont..,
NEXT: MOV.B 0(R4),0(R5) ;move data byte

DEC R4 ;update source pointer
DEC R5

DEC.B R6 ;repeat till zero
JNZ NEXT

JMP $
END
Program 5Exchange of data
main: MOV.W #WDTPW+WDTHOLD, &WDTCTL
MOV #0x20,R4 ; Array1 pointer
MOV #0x30,R5 ; Array2 pointer
MOV.B #05, R6 ; Counter
NEXT: MOV.B @R4,R7 ;Get data byte
from array1 into R7
MOV.B @R5,0(R4) ;move data
byte from array2 to array1
MOV.B R7,0(R5) ; store R7 contents
(array1 data) into array2
Cont..,
INC R4 ; point to next byte in array1
INC R5 ; point to next byte in array2
DEC.B R6 ; Decrement counter
JNZ NEXT ; Repeat till zero
JMP $
END
Program 1
MSP430 Assembly Language Program to
illustrate Arithmetic operations (ADDITION-
binary & decimal, SUBTRACTION) and
logical operations (AND, OR, XOR).
Problem definition
Given two eight bit numbers w & v (say
at 20h) perform binary & decimal addition,
subtraction, logical AND, OR and XOR
operations on the 2 numbers and store the
results in consecutive memory locations.
ALP Program
main: MOV.W #WDTPW+WDTHOLD,&WDTCTL ;Stop
watchdog timer
MOV #0x20, R4 ;pointer to 1st number

;R5=1st number & R4 points to 2nd number
MOV.B @R4+, R5

;R6 = 2nd number & R4 points to add result location
MOV.B @R4+, R6

MOV.B R6, 0(R4) ;move 2nd num to result location &
ADD.B R5, 0(R4) ;add to the result location the 1st
number
Cont..,1
INC R4
;point to next result location (to store the
result of next operation)

MOV.B R6, 0(R4)
; move 2nd number in R6 to result location

DADD.B R5, 0(R4)
;decimal add 1st number in R5 to result
location
Cont..,2
INC R4
; point to next result location

MOV.B R6, 0(R4)
; 2nd number in R6 to result location

SUB.B R5, 0(R4)
;subtract 1st number in R5 from result
location & store
Cont..,3
INC R4
MOV.B R6, 0(R4)
AND.B R5, 0(R4) ;AND
INC R4
MOV.B R6, 0(R4)
BIS.B R5, 0(R4) ; OR operation
INC R4
MOV.B R6, 0(R4)
XOR.B R5, 0(R4) ;EX-OR operation
INC R4
JMP $
END
Result:
For the 2 numbers 32h and 59h
stored in consecutive locations, the
results are 8B (binary addition),
91(decimal addition),
27 (subtraction of 59 -32),
10h(AND), 7B (OR) & 6B (XOR)
Program 7 Code conversion
Write an ALP to implement ASCII to
hexadecimal conversion
ASCII code (for example say 45) at
RAM location 0050h is converted to
hexadecimal -0E and stored at the
next consecutive location 51h
ALP
main: MOV.W #WDTPW+WDTHOLD, &WDTCTL
MOV #0x050, R4 ; R4 has address 50H
(remember 50h has the ASCII number to
be converted)

MOV.B @R4+, R6 ; Get data
pointed by R4 into register R6. At the same
time increment R4 to point to next location
i.e., 51h

SUB.B #0x30,R6 ; subtract 30h from R6

Cont..,
CMP # 0x0A,R6 ; Check if
contents of R6 are greater than 0Ah

JNC SKIP ; jump to skip if R6
contents < 0A
SUB #0x07, R6 ; if R6 > 0A,
subtract 07 further
SKIP: MOV.B R6, 0(R4) ; Store the
converted data in memory location (51h)
pointed by R4
JMP $
C projects for MSP430
microcontrollers
#include "io430.h
int main( void )
{ // Stop watchdog timer to prevent time
out reset

WDTCTL = WDTPW + WDTHOLD;

TYPE MAIN PROGRAM HERE

return 0;}
Example C Program :
Real-time clock
#include "io430.h"
#include <intrinsics.h> // Intrinsic functions
#include <stdint.h> // Integers of defined sizes
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;

static unsigned short seconds = 0x00; // initial time
in BCD
static unsigned short minutes = 0x00;
static unsigned short hours = 0x00;
while(1)
{ // Update time
seconds = __bcd_add_short(seconds , 0x01);
// ++ seconds
if (seconds >= 0x60) {
seconds = 0; // start new minute
minutes = __bcd_add_short(minutes , 0x01);
if (minutes >= 0x60) { minutes = 0; // start
new hour
hours = __bcd_add_short(hours , 0x01);
if (hours >= 0x24) {hours = 0; // start new day
} } } } return 0;}
Cont..,
Mixing C and Assembly Language
Write parts of the code in assembly
language because some of the
functions/instructions available in
assembly language are not available
in standard C.
3 Methods:
1. Use Intrinsic functions
2. Use Inline Assembly
3. Assembly language Subroutine
Intrinsic functions
For example DADD instruction in
assembly is realized as a function in C.
Increment decimally by calling the
intrinsic function _ _bcd_add_short(),
which in turn uses the MSP430s dadd
instruction.
This ensures that 0x09 is incremented
in BCD to 0x10
Intrinsic Functions Cont..,
intrinsic function _ _bcd_add_long()
to call dadd for the decimal addition
of two 32-bit numbers in C
_ _swap_bytes() intrinsic function
calls the swpb instruction.

(all intrinsic functions are preceded
by a double underscore).
Embedded C Data types
In the Hex to BCD conversion larger
data types needed
#include <stdint.h> // Integers of
defined sizes
uint8_t, uint32_t,
Generally int => 16 bits
2. Use Inline Assembly
only a line or two of assembly language
is used in the entire C program.
The ALP code is embedded in the
declaration asm() whose argument
is the line(s) of assembly code.
Example: asm("mov.b &P1IN, &dest")

3. Assembly language Subroutine
write a complete subroutine in assembly
language and call it from C.
It is important to ensure that the assembly language
reads the parameters from, and returns its value to, the
places where the C compiler expects.
This depends on the conventions used by each compiler,
which means that a program with mixed C and
assembly language lacks portability and
may not work if you change development
environment.
For example a certain compiler uses only registers R12
to R15, to pass/return the parameters from the ALP.
have to be careful when using this feature. This is a good
reason for avoiding the technique wherever possible
Interrupts and Interrupt Programming
Interrupts are generated by hardware,
but can be initiated by software
MSP430 uses vectored interrupts,
with each ISR having its own vector,
which is stored at a predefined address in
a vector table at the end of the program
memory (addresses 0xFFC00xFFFF).
Interrupts Cont..,
Each interrupt has a flag,
which is raised (set) when the condition for the
interrupt occurs.
Example: Timer_A sets the TAIFG flag in
the TACTL register when the counter TAR
returns to 0.
Each interrupt flag has a corresponding
enable bit, TAIE in this case
Most interrupts are maskable --effective
only if the general interrupt enable (GIE)
bit is set in the status register (SR).
Interrupts -- Priority
Each interrupt vector has a distinct
priority.
The priorities are fixed in hardware and
cannot be changed by the user.
They are determined by the address of
the vector
A higher address means a higher priority.
The reset vector has address 0xFFFE,
which gives it the top priority, followed by
0xFFFC for the single nonmaskable
interrupt vector
Nonmaskable Interrupts
Three modules can request a non-
maskable interrupt:
Oscillator fault (OFIFG),
Access violation to flash memory (ACCVIFG)
an active edge on the external RST/NMI pin
if it has been configured for interrupts
rather than reset.
The shared vector means that the ISR
must first identify the source of the
interrupt
Interrupt programming
An ISR is same as a subroutine but
with the following distinctions:
The address of the subroutine, must be
stored in the appropriate interrupt vector
The routine must end with reti rather than
ret
interrupts must be enabled
Example of Interrupt programming
main: mov.w #WDTPW|WDTHOLD ,& WDTCTL ;
Stop watchdog timer
MOV #00,R5
mov.w #49999 ,& TACCR0 ; Period for up mode

mov.w #CCIE ,& TACCTL0 ; Enable
interrupts on Compare 0

mov.w #MC_1|ID_3|TASSEL_2|TACLR ,& TACTL
;Set up Timer

bis.w #GIE ,SR ; Enable interrupts (just TACCR0)
jmp$ ;Loop forever; interrupts do all

Example Cont..,
; ISR for TACCR0 CCIFG
TA0_ISR: INC R5
reti
;
COMMON INTVEC ;Segment for vectors (in Flash)

ORG 0xFFEC ;Interrupt vector for timer A in 4x series
DW TA0_ISR ; ISR for TA0 interrupt

ORG 0xFFFE ; Reset vector
DW Reset ; Address to start execution

END
Low Power Modes
six operating modes with 5 low power modes--
(LPM0) to (LPM4)
In LPM0 only CPU is off as MCLK is off.
SMCLK and ACLK are active.
In LPM1 LPM2, certain peripherals along with
CPU are switched off, by selective switching
off SMCLK sources.
In LPM3 only ACLK is active and hence, the
peripherals which run on ACLK are enabled.
In LPM4 CPU and all clocks are disabled and the
current is I 0.1A. The device can be
wakened only by an external signal. This is also
called RAM retention mode.

current consumptions in various modes
Low power programming
low power modes of MSP430 can be
entered by clearing the bits in SR reg
Low power programming Cont..,
In assembly, the code below puts the MSP430
into LPM3, with interrupts enabled. (GIE=1)
bis.w #GIE|LPM3 ,SR

If GIE is already set, the code given below is
used:
bis.w #LPM3, SR

In C, the file intrinsics.h should be included in
the program. The code is :
_low_power_mode_3 ();
// enter LPM3 with interrupts enabled
Digital I/O ports
In the current portfolio of MSP430
family, there are 1080 input/
output pins on different devices.

The F20xx has one complete 8-pin
port and 2 pins on a second port,

while the largest devices have ten
full ports.
Pin Configuration
Almost all pins can be used either for digital
input/output or for multiple other functions
and
their operation must be configured when the
device starts up.
For example, pin P1.0 on the F2013 can be a
digital input,
digital output,
input TACLK,
output ACLK, or
analog input A0+.
This is a choice of five functions and therefore
needs at least 3 bits for selection.
Port Registers
Up to eight registers are associated with
digital input/output functions for each pin
PxDIR
Bit = 0: The port pin is
switched to input direction
Bit = 1: The port pin is
switched to output direction
Pullup/Pulldown Resistor Enable Registers PxREN
(MSP430F47x3/4 and MSP430-F471xx only)
I/O Ports Programming using C and
assembly
codes (either C or assembly language)
associated with the I/O Ports can be
classified into three groups
For configuration of the pins
For different functions when configured as
input pin
For different functions when configured as
output pin
Cont..,
P1IN.3 -- bit 3 of the byte P1IN
suffix b => binary number and suffix
x => hexadecimal number
Bitwise operators used in C are:
&(AND); | (OR); ^ (XOR) ; ~ ( NOT)
P1OUT = P1OUT | BIT3, is usually
abbreviated to P1OUT |= BIT3
Masks are used -- standard definition
BIT3 = 00001000 for selecting bit 3

Codes to configure the port pins
Cont..,
Codes for operations on pins configured
as output pins. Use PxOUT register
Operations on pins configured as
input pins. Use PxIN register
Program 8 - Counters
main: MOV.W #WDTPW+WDTHOLD, &WDTCTL
MOV.B #0xFF,&P2DIR ;set up PORT2 as output port
MOV.B #0x00,R4 ;Initialize count as 00 for upcount

NEXT: INC.B R4 ; Increment for next count

MOV.W #0x02,R5 ;set up a small delay
DELAY: DEC.W R5
JNZ DELAY

MOV.B R4, &P2OUT ;send the count to port2
JMP NEXT ;(endless loop)
END
Results
Other Counters
For a Binary down counter just
replace INC.B R4 with DEC.B R4
For a BCD up counter replace INC.B
R4 with
CLRC and DADD.B #0x01,R4
For a BCD down counter replace
INC.B R4 with the two instructions
CLRC
DADD.B #0x99,R4
Questions?