Introcution to the MSP430 assembly language

The assembly code has this format:

label:  mnemonic  operand_list      ;comment

Each part is not mandatory. Here is an example of a (useless and buggy) loop:

init:
        mov.b   0, R4           ; initialize the R4 register with 0 (decimal)

loop: 
        add.b   1, R4           ; add 1 to R4
        nop                     ; no operation (and no operand)
        jmp     loop

Labels

The labels are symbols corresponding to a memory address. They must begin in the first column.

Mnemonics:

the mnemonics can correspond to - instructions (mov, bitc, jmp, ...) - Assembler directives (.cdecls, .data, .sect, ...) - macros

The mnemonics are suffixed with ".b" or ".w", depending if we want to work with a byte (8 bits) or a word (16 or 32 bits depending the microcontroller)

The usual mnemonics instructions are:

Bit / Register instructions:

Operation operand Description C V N Z Emulated
BIC(.B) src,dst .not.src .and. dst → dst
clear bits in destination
BIS(.B) src,dst src .or. dst → dst
set bits in destination
CLR(.B) dst mov #0, dst
Clear destination
yes
MOV(.B) src,dst src → dst
copy (not move) source to destination
CLRC bic #1, SR
Clear carry bit
0 yes
CLRN bic #4, SR
Clear negative bit
0 yes
CLRZ bic #2, SR
Clear zero bit
0 yes
SETC bis #1, SR
Set carry bit
1 yes
SETN bis #4, SR
Set negative bit
1 yes
SETZ bis #2, SR
Set zero bit
1 yes

Logic instructions:

Operation operand Description C V N Z Emulated
XOR(.B) src,dst src .xor. dst → dst
xor source with destination
x x x x
AND(.B) src,dst src .and. dst → dst 0 x x x
BIS(.B) src,dst src .or. dst → dst
INV(.B) dst #OFFFFh .xor. dst
Invert destination
x x x x yes

Arithmetic instructions:

Operation operand Description C V N Z Emulated
ADC(.B) dst dst + C → dst
Add carry to destination
x x x x
ADD(.B) src,dst src + dst → dst
Add source to destination
x x x x
ADDC(.B) src,dst src + dst + C → dst
Add source and carry to destination
x x x x
DADC(.B) dst dst + C → dst (decimal)
Decimal add carry to destination
x x x x yes
DADD(.B) src,dst src + dst + C → dst (decimal)
Decimal addition
x x x x
DEC(.B) dst dst - 1 → dst
Decrement destination
x x x x yes
DECD(.B) dst dst - 2 → dst
Decrement destination twice
x x x x yes
INC(.B) dst dst +1 → dst
Increment destination
x x x x yes
INCD(.B) dst dst+2→dst
Increment destination twice
x x x x yes
SUB(.B) src,dst dst + .not.src + 1 → dst
Substract source from destination
x x x x
SUBC(.B) src,dst dst + .not.src + C → dst
Substract source and carry from destination
x x x x
SBC(.B) dst Subtract carry from destination x x x x yes

Tests/comparisons operations:

Operation operand Description C V N Z Emulated
CMP(.B) src,dst dst - src
Compare src with dst
x x x x
BIT(.B) src,dst src .and. dst
Test bits in destination
0 x x x
TST(.B) dst cmp #0, dst
Test destination
x x x x yes

Program flow

Operation operand Description C V N Z Emulated
CALL dst PC+2 → stack, dst → PC
Subroutine call to destination
RETI TOS → SR, SP + 2 → SP
TOS → PC, SP + 2 → SZP
Return from interrupt
RET TOS → PC, SP + 2 → SP
Return from subroutine
yes
BR dst mov dst, PC
Branch to destination
yes
DINT bic #8, SR
Disable general interrupts
yes
EINT bis #8, SR
Enable general interrupts
yes
NOP mov #0, R3
No operation
yes
JC/JHS Label Jump to Label if Carry-bit is set
JEQ/JZ Label Jump to Label if Zero-bit is set
JGE Label Jump to Label if (N .XOR. V) = 0
JL Label Jump to Label if (N .XOR. V) = 1
JMP Label Jump to Label unconditionally
JN Label Jump to Label if Negative-bit is set
JNC/JLO Label Jump to Label if Carry-bit is reset
JNE/JNZ Label Jump to Label if Zero-bit is reset

(TOS) = Top Of Stack

Others

Operation operand Description C V N Z Emulated
POP(.B) dst &SP→temp, SP+2 → SP, temp→dst
Item from stack
yes
PUSH(.B) src SP - 2 → SP, src → @SP
Push source on stack
RLA(.B) dst add dst, dst
Rotate left arithmetically (dest x 2)
x x x x yes
RLC(.B) dst addc dst, dst
Rotate left through carry
x x x x yes
RRA(.B) dst MSB → MSB ....LSB → C
Roll destination right
0 x x x
RRC(.B) dst C → MSB .........LSB → C
Roll destination right through carry
x x x x
SWPB dst swap bytes
swap bytes in destination
SXT dst Bit7 → Bit8 ........ Bit15
Sign extend destination
0 x x x

The usual assembler directives are:

directive description
.cdecls [options,] "filename" Import a C header file in the assembly code
.def symbol1 [, ... , symboln] Identifies one or more symbols that are defined in current module
.text Assembles into the .text (executable code) section
.retain Override ELF conditional linking and retain current section
.retainrefs Retain any sections that have references to current section.
.end Ends program

Source: TI assembly mnemonics local cache

Comments !

links

social