Apuntes de referencia · Programación en ensamblador Reference handout · Assembly programming
Toda instrucción del MSP430 ocupa una palabra de 16 bits. Esa palabra puede ir seguida de una o dos palabras de extensión cuando un operando necesita un dato adicional (un índice, una dirección absoluta o un inmediato). Existen tres formatos según cuántos operandos lleve la instrucción. Every MSP430 instruction occupies a single 16-bit word. That word may be followed by one or two extension words when an operand needs extra data (an index, an absolute address, or an immediate). There are three formats, depending on how many operands the instruction takes.
Opcode Rsregistro fuentesource register Rdregistro destinodestination register As / Adbits de modomode bits B/Wbyte/palabrabyte/word offset
Se usa la nomenclatura de Texas Instruments (Rs, Rd, As, Ad). En algunas hojas aparece como Rf, Rd, Df, Dd respectivamente. Texas Instruments naming is used (Rs, Rd, As, Ad). Some sheets label these Rf, Rd, Df, Dd respectively.
El opcode de 4 bits identifica la operación. Rs y As describen el origen; Rd y Ad el destino. B/W es activo a nivel bajo: 0 = palabra, 1 = byte. The 4-bit opcode names the operation. Rs and As describe the source; Rd and Ad the destination. B/W is active-low: 0 = word, 1 = byte.
| Opcode | Nem. | Opcode | Nem. | Opcode | Nem. |
|---|---|---|---|---|---|
| 0100 | MOV | 1000 | SUB | 1100 | BIC |
| 0101 | ADD | 1001 | CMP | 1101 | BIS |
| 0110 | ADDC | 1010 | DADD | 1110 | XOR |
| 0111 | SUBC | 1011 | BIT | 1111 | AND |
ADD R4, R5 (ambos en modo registro, palabra):
Let us encode ADD R4, R5 (both register mode, word):
Los seis bits altos son fijos (000100). El operando único usa sus propios bits de modo (As) y registro (Rd), por lo que admite todos los modos de direccionamiento. Solo RRC, RRA y PUSH tienen forma de byte; SWPB, SXT, CALL y RETI son siempre de palabra (bit 6 = 0). The top six bits are fixed (000100). The single operand carries its own mode (As) and register (Rd) bits, so it accepts every addressing mode. Only RRC, RRA and PUSH have a byte form; SWPB, SXT, CALL and RETI are word-only (bit 6 = 0).
| Opcode (9..7)Opcode (9..7) | Nem. | ByteByte | OperaciónOperation |
|---|---|---|---|
| 000 | RRC | ✓ | rotación a la derecha con carryrotate right through carry |
| 001 | SWPB | — | intercambia byte alto y bajoswap high and low byte |
| 010 | RRA | ✓ | desplazamiento aritmético a la derechaarithmetic shift right |
| 011 | SXT | — | extiende el signo del byte bajosign-extend the low byte |
| 100 | PUSH | ✓ | apila el operandopush the operand |
| 101 | CALL | — | llamada a subrutinacall subroutine |
| 110 | RETI | — | retorno de interrupciónreturn from interrupt |
RRA R6 (modo registro, palabra):
Let us encode RRA R6 (register mode, word):
El salto es relativo al PC: PC ← PC + 2 + desplazamiento × 2. El campo de desplazamiento es de 10 bits en complemento a dos, así que vale de -512 a +511. Por el +2 de la fórmula (el PC ya apunta a la instrucción siguiente), el destino queda entre 511 palabras atrás y 512 adelante respecto al propio salto. Los tres bits de condición eligen qué flag se comprueba. The jump is PC-relative: PC ← PC + 2 + offset × 2. The 10-bit two's-complement offset field holds -512 to +511. Because of the +2 in the formula (PC already points at the next instruction), the target lands between 511 words back and 512 forward of the jump itself. The three condition bits choose which flag is tested.
| Cond | Nem. | Salta si…Taken when… | SentidoMeaning |
|---|---|---|---|
| 000 | JNE / JNZ | Z = 0 | distinto / no ceronot equal / not zero |
| 001 | JEQ / JZ | Z = 1 | igual / ceroequal / zero |
| 010 | JNC / JLO | C = 0 | menor sin signounsigned < |
| 011 | JC / JHS | C = 1 | mayor o igual sin signounsigned ≥ |
| 100 | JN | N = 1 | negativonegative |
| 101 | JGE | N ⊕ V = 0 | mayor o igual con signosigned ≥ |
| 110 | JL | N ⊕ V = 1 | menor con signosigned < |
| 111 | JMP | — | incondicionalunconditional |
JEQ destino con un desplazamiento de +5 palabras:
Let us encode JEQ target with an offset of +5 words:
Los bits As (2 bits, fuente) y Ad (1 bit, destino) no eligen el modo por sí solos: su significado depende del registro al que acompañan. Esto permite reutilizar las mismas combinaciones para modos especiales mediante el generador de constantes (R2 y R3). The As (2-bit, source) and Ad (1-bit, destination) bits do not select the mode on their own: their meaning depends on the register they accompany. This lets the same combinations be reused for special modes through the constant generator (R2 and R3).
| RegistroRegister | As | SintaxisSyntax | ModoMode | Qué haceWhat it does |
|---|---|---|---|---|
| Rn | 00 | Rn | RegistroRegister | el operando es el propio registrothe operand is the register itself |
| Rn | 01 | X(Rn) | IndexadoIndexed | dirección = Rn + X (X en palabra de extensión)address = Rn + X (X in extension word) |
| Rn | 10 | @Rn | IndirectoIndirect | Rn contiene la dirección del operandoRn holds the operand's address |
| Rn | 11 | @Rn+ | Indirecto con postincrementoIndirect autoincrement | como @Rn y luego Rn += 1 o 2like @Rn, then Rn += 1 or 2 |
| R0 (PC) | 01 | ETIQ | Simbólico (relativo a PC)Symbolic (PC-relative) | dirección = PC + Xaddress = PC + X |
| R0 (PC) | 11 | #N | InmediatoImmediate | @PC+: la constante va en la palabra de extensión@PC+: the constant is in the extension word |
| R2 (SR) | 01 | &ETIQ | AbsolutoAbsolute | X(0): dirección literal, sin sumar PCX(0): literal address, no PC added |
| R2 (SR) | 10 | #4 | ConstanteConstant | generador de constantes: +4constant generator: +4 |
| R2 (SR) | 11 | #8 | ConstanteConstant | generador de constantes: +8constant generator: +8 |
| R3 (CG2) | 00 | #0 | ConstanteConstant | generador de constantes: 0constant generator: 0 |
| R3 (CG2) | 01 | #1 | ConstanteConstant | generador de constantes: +1constant generator: +1 |
| R3 (CG2) | 10 | #2 | ConstanteConstant | generador de constantes: +2constant generator: +2 |
| R3 (CG2) | 11 | #-1 | ConstanteConstant | generador de constantes: 0xFFFFconstant generator: 0xFFFF |
| RegistroRegister | Ad | SintaxisSyntax | ModoMode | Qué haceWhat it does |
|---|---|---|---|---|
| Rn | 0 | Rn | RegistroRegister | el destino es el propio registrothe destination is the register itself |
| Rn | 1 | X(Rn) | IndexadoIndexed | dirección = Rn + Xaddress = Rn + X |
| R0 (PC) | 1 | ETIQ | Simbólico (relativo a PC)Symbolic (PC-relative) | dirección = PC + Xaddress = PC + X |
| R2 (SR) | 1 | &ETIQ | AbsolutoAbsolute | X(0): dirección literalX(0): literal address |
El núcleo del MSP430 tiene 27 instrucciones reales. El resto son emuladas: el ensamblador las traduce a una instrucción real, sin coste extra. [.T] indica que existe forma de byte (.B) y palabra (.W).
The MSP430 core has 27 real instructions. The rest are emulated: the assembler rewrites them into a real instruction at no extra cost. [.T] marks that a byte (.B) and word (.W) form exist.
f = origen (fuente), admite todos los modos.src = source, any mode. d = destino (solo Rn o M(Rn)).dst = destination (Rn or M(Rn) only). EMU= emulada (muestra la instrucción real).= emulated (shows the real instruction).
Flags:Flags: * según resultadoset by result – sin cambiounchanged 0/1 forzadoforced a 1 si resultado ≠ 0set if result ≠ 0
| Nem. | Op.Op. | Función / EmulaciónFunction / Emulation | V N Z C |
|---|---|---|---|
| MovimientoMove | |||
| MOV[.T] | f, dsrc, dst | d ← fdst ← src | {----} |
| PUSH[.T] | fsrc | SP ← SP-2, MEM(SP) ← fSP ← SP-2, MEM(SP) ← src | {----} |
| POP[.T] EMU | ddst | d ← MEM(SP), SP ← SP+2 ≡ MOV @SP+, ddst ← MEM(SP), SP ← SP+2 ≡ MOV @SP+, dst | {----} |
| SWPB | fsrc | f15..8 ↔ f7..0src15..8 ↔ src7..0 | {----} |
| CLR[.T] EMU | ddst | d ← 0 ≡ MOV #0, ddst ← 0 ≡ MOV #0, dst | {----} |
| AritméticasArithmetic | |||
| ADD[.T] | f, dsrc, dst | d ← d + fdst ← dst + src | {FFFF} |
| ADDC[.T] | f, dsrc, dst | d ← d + f + Cdst ← dst + src + C | {FFFF} |
| ADC[.T] EMU | ddst | d ← d + C ≡ ADDC #0, ddst ← dst + C ≡ ADDC #0, dst | {FFFF} |
| INC[.T] EMU | ddst | d ← d + 1 ≡ ADD #1, ddst ← dst + 1 ≡ ADD #1, dst | {FFFF} |
| INCD[.T] EMU | ddst | d ← d + 2 ≡ ADD #2, ddst ← dst + 2 ≡ ADD #2, dst | {FFFF} |
| SUB[.T] | f, dsrc, dst | d ← d − fdst ← dst − src | {FFFF} |
| SUBC[.T] | f, dsrc, dst | d ← d − f − Cdst ← dst − src − C | {FFFF} |
| SBC[.T] EMU | ddst | d ← d − C ≡ SUBC #0, ddst ← dst − C ≡ SUBC #0, dst | {FFFF} |
| DEC[.T] EMU | ddst | d ← d − 1 ≡ SUB #1, ddst ← dst − 1 ≡ SUB #1, dst | {FFFF} |
| DECD[.T] EMU | ddst | d ← d − 2 ≡ SUB #2, ddst ← dst − 2 ≡ SUB #2, dst | {FFFF} |
| CMP[.T] | f, dsrc, dst | d − f (solo flags)dst − src (flags only) | {FFFF} |
| TST[.T] EMU | ddst | d − 0 ≡ CMP #0, ddst − 0 ≡ CMP #0, dst | {0FF1} |
| SXT | fsrc | f15..8 ← f7src15..8 ← src7 | {0FFZ} |
| INV[.T] EMU | ddst | d ← NOT d ≡ XOR #-1, ddst ← NOT dst ≡ XOR #-1, dst | {FFFZ} |
| DADD[.T] | f, dsrc, dst | d ← d + f + C (BCD)dst ← dst + src + C (BCD) | {FFFF} |
| DADC[.T] EMU | ddst | d ← d + C (BCD) ≡ DADD #0, ddst ← dst + C (BCD) ≡ DADD #0, dst | {FFFF} |
| Lógicas y manejo de bitsLogic and bit handling | |||
| AND[.T] | f, dsrc, dst | d ← d AND fdst ← dst AND src | {0FFZ} |
| BIT[.T] | f, dsrc, dst | d AND f (solo flags)dst AND src (flags only) | {0FFZ} |
| BIC[.T] | f, dsrc, dst | d ← d AND NOT fdst ← dst AND NOT src | {----} |
| BIS[.T] | f, dsrc, dst | d ← d OR fdst ← dst OR src | {----} |
| XOR[.T] | f, dsrc, dst | d ← d XOR fdst ← dst XOR src | {FFFZ} |
| Bits de estadoStatus bits | |||
| CLRC EMU | — | C ← 0 ≡ BIC #1, SR | {---0} |
| SETC EMU | — | C ← 1 ≡ BIS #1, SR | {---1} |
| CLRN EMU | — | N ← 0 ≡ BIC #4, SR | {-0--} |
| SETN EMU | — | N ← 1 ≡ BIS #4, SR | {-1--} |
| CLRZ EMU | — | Z ← 0 ≡ BIC #2, SR | {--0-} |
| SETZ EMU | — | Z ← 1 ≡ BIS #2, SR | {--1-} |
| Desplazamiento y rotaciónShift and rotate | |||
| RRC[.T] | fsrc | rotación a la derecha con carryrotate right through carry | {0FFF} |
| RLC[.T] EMU | ddst | rotación a la izquierda con carry ≡ ADDC d, drotate left through carry ≡ ADDC dst, dst | {FFFF} |
| RRA[.T] | fsrc | desplazamiento aritmético a la derechaarithmetic shift right | {0FFF} |
| RLA[.T] EMU | ddst | desplazamiento aritmético a la izquierda ≡ ADD d, darithmetic shift left ≡ ADD dst, dst | {FFFF} |
| Salto y subrutinasJump and subroutines | |||
| JMP | dsp | PC ← PC + dsp×2 (incondicional)(unconditional) | {----} |
| JEQ / JZ | dsp | salta si Z = 1jump if Z = 1 | {----} |
| JNE / JNZ | dsp | salta si Z = 0jump if Z = 0 | {----} |
| JC / JHS | dsp | salta si C = 1 (≥ sin signo)jump if C = 1 (unsigned ≥) | {----} |
| JNC / JLO | dsp | salta si C = 0 (< sin signo)jump if C = 0 (unsigned <) | {----} |
| JN | dsp | salta si N = 1jump if N = 1 | {----} |
| JGE | dsp | salta si N ⊕ V = 0 (≥ con signo)jump if N ⊕ V = 0 (signed ≥) | {----} |
| JL | dsp | salta si N ⊕ V = 1 (< con signo)jump if N ⊕ V = 1 (signed <) | {----} |
| BR EMU | fsrc | PC ← f ≡ MOV f, PCPC ← src ≡ MOV src, PC | {----} |
| CALL | fsrc | SP ← SP-2, MEM(SP) ← PC, PC ← fSP ← SP-2, MEM(SP) ← PC, PC ← src | {----} |
| RET EMU | — | PC ← M(SP), SP ← SP+2 ≡ MOV @SP+, PC | {----} |
| RETI | — | SR ← M(SP), SP ← SP+2, PC ← M(SP), SP ← SP+2 | {FFFF} |
| MisceláneaMiscellaneous | |||
| DINT EMU | — | GIE ← 0 ≡ BIC #8, SR | {----} |
| EINT EMU | — | GIE ← 1 ≡ BIS #8, SR | {----} |
| NOP EMU | — | no hace nadano operation ≡ MOV R3, R3 | {----} |
El coste en ciclos (MCLK) depende solo de los modos de direccionamiento, no de la operación: la ALU no añade ciclos. Cada acceso a memoria cuesta un ciclo, y el byte y la palabra tardan lo mismo. Una instrucción EMU tarda exactamente lo que su instrucción real. Valores del MSP430 en modo CPU de 16 bits (64 KB). Cycle cost (MCLK) depends only on the addressing modes, not on the operation: the ALU adds no cycles. Each memory access costs one cycle, and byte and word take the same time. An EMU instruction takes exactly what its real instruction takes. Values for the MSP430 in 16-bit (CPU) mode (64 KB).
| OrigenSource | Destino: registro (Rn)Destination: register (Rn) | Destino en memoria (indexado / simbólico / absoluto)Destination in memory (indexed / symbolic / absolute) | ||
|---|---|---|---|---|
| cicloscycles | pal.words | cicloscycles | pal.words | |
| Rn registroregister | 1 | 1 | 4 | 2 |
| @Rn indirectoindirect | 2 | 1 | 5 | 2 |
| @Rn+ postincrementoautoincrement | 2 | 1 | 5 | 2 |
| #N inmediatoimmediate | 2 | 2 | 5 | 3 |
| X(Rn) indexadoindexed | 3 | 2 | 6 | 3 |
| ETIQ simbólicosymbolic | 3 | 2 | 6 | 3 |
| &ETIQ absolutoabsolute | 3 | 2 | 6 | 3 |
El destino solo admite 4 modos: registro y tres en memoria (indexado X(Rn), simbólico ETIQ y absoluto &ETIQ), que cuestan lo mismo y por eso comparten columna. No hay destino indirecto, postincremento ni inmediato. The destination supports only 4 modes: register and three in memory (indexed X(Rn), symbolic ETIQ and absolute &ETIQ), which cost the same and therefore share a column. There is no indirect, autoincrement or immediate destination.
+1 si el destino es el PC (p. ej. BR, MOV x, PC).if the destination is the PC (e.g. BR, MOV x, PC). +1 en DADD.for DADD.
El coste lo fijan los bits As/Ad: los modos inmediato (#N), simbólico (ETIQ) y absoluto (&ETIQ) reutilizan la codificación —y el tiempo— de @Rn+ (As=11) y X(Rn) (As=01). Por eso una tabla indexada por bits necesita menos filas (ver Sección 2). Cost is set by the As/Ad bits: the immediate (#N), symbolic (ETIQ) and absolute (&ETIQ) modes reuse the encoding —and the timing— of @Rn+ (As=11) and X(Rn) (As=01). That is why a bit-indexed table needs fewer rows (see Section 2).
| Modo del operandoOperand mode | RRA · RRC · SWPB · SXT | PUSH | CALL | pal.words |
|---|---|---|---|---|
| Rn | 1 | 3 | 4 | 1 |
| @Rn | 3 | 4 | 4 | 1 |
| @Rn+ | 3 | 4 | 5 | 1 |
| #N | — | 4 | 5 | 2 |
| X(Rn) | 4 | 5 | 5 | 2 |
| ETIQ | 4 | 5 | 5 | 2 |
| &ETIQ | 4 | 5 | 5 | 2 |
PUSH y CALL solo leen el operando: aceptan cualquier modo, incluido inmediato (PUSH #5, CALL #rutina) e indexado. RRA/RRC/SWPB/SXT modifican el operando en su sitio, así que admiten todos los modos menos inmediato (no se puede reescribir una constante): de ahí el «—». PUSH and CALL only read the operand: any mode is allowed, including immediate (PUSH #5, CALL #routine) and indexed. RRA/RRC/SWPB/SXT modify the operand in place, so they allow every mode except immediate (you cannot rewrite a constant): hence the “—”.
| OperaciónOperation | CiclosCycles | pal.words |
|---|---|---|
| Jxx salto cond. / incond.cond. / uncond. jump | 2 | 1 |
| RET ≡ MOV @SP+, PC | 3 | 1 |
| RETI | 5 | 1 |
| Atención a interrupciónInterrupt service | 6 | — |
| Reset | 4 | — |
MOV.W 4(R5), &2000h — origen indexado X(Rn) → destino absoluto en memoria.indexed source X(Rn) → absolute destination in memory.