The same machine, two languages. How C compiles down to the instructions you already know. La misma máquina, dos lenguajes. Cómo C se compila a las instrucciones que ya conoces.
Santi Scagliusi, PhD
The compiler does not invent new operations. It emits the same MSP430 instructions you write by hand.El compilador no inventa operaciones nuevas. Emite las mismas instrucciones MSP430 que escribes a mano.
A C compiler reads this and produces an assembly file.Un compilador de C lee esto y produce un archivo en ensamblador.
Args arrive in R12 and R13. The sum stays in R12. The same instructions, fewer keystrokes.Los argumentos llegan en R12 y R13. La suma queda en R12. Las mismas instrucciones, menos tecleo.
One word is 16 bits. A type that fits in a word is cheap. Anything wider costs extra instructions and memory accesses.Una palabra son 16 bits. Un tipo que cabe en una palabra es barato. Lo más ancho cuesta instrucciones y accesos a memoria extra.
A pointer is 2 bytes: it holds a 16-bit address. One register, one instruction.Un puntero ocupa 2 bytes: guarda una dirección de 16 bits. Un registro, una instrucción.
No floating-point hardware: float and double run as software library calls.Sin hardware de coma flotante: float y double se ejecutan con llamadas a librería.
Pick the smallest type that holds your value. The width you choose becomes the number of instructions.Elige el tipo más pequeño que contenga tu valor. El ancho que elijas se convierte en número de instrucciones.
Every assembly program starts the same way: a code section, an entry label, stop the watchdog, set the stack, then run.Todo programa en ensamblador empieza igual: una sección de código, una etiqueta de entrada, parar el watchdog, fijar la pila y ejecutar.
.text lands the code in FRAM at 0x4400..text coloca el código en FRAM en 0x4400.
Stop the watchdog first, or it resets the chip mid-run.Para el watchdog primero, o reinicia el chip a mitad.
.intvec writes main into 0xFFFE: where the CPU starts..intvec escribe main en 0xFFFE: donde arranca la CPU.
CALL saves the address of the next instruction on the stack, then jumps. RET pops it back into PC.CALL guarda en la pila la dirección de la siguiente instrucción y salta. RET la recupera en el PC.
RET is just MOV @SP+, PC. The saved address goes straight back into the program counter.RET es simplemente MOV @SP+, PC. La dirección guardada vuelve directa al contador de programa.
A subroutine that uses R6 must save and restore it. Every PUSH needs a matching POP before RET.Una subrutina que use R6 debe guardarlo y restaurarlo. Cada PUSH necesita su POP antes de RET.
When RET runs, the top of the stack must hold the return address, not your saved R6. PUSH and POP keep SP balanced.Cuando se ejecuta RET, la cima de la pila debe contener la dirección de retorno, no tu R6 guardado. PUSH y POP mantienen el SP equilibrado.
Low address at the bottom, near SP. CALL wrote 0x4414; PUSH put old R6 below it.Dirección baja abajo, junto a SP. CALL escribió 0x4414; PUSH puso el R6 antiguo debajo.
A shared contract so C and assembly can call each other. The TI and GCC compilers both follow the MSP430 EABI.Un contrato común para que C y ensamblador se llamen entre sí. Los compiladores de TI y GCC siguen el EABI del MSP430.
Args fill R12→R15 in order. The result comes back in R12: same register, dual role. Los argumentos llenan R12→R15 en orden. El resultado vuelve en R12: el mismo registro, doble función.
The convention splits the working registers in two, so neither side saves more than it must.La convención divide los registros de trabajo en dos, para que ningún lado guarde más de lo necesario.
A tiny C function and the assembly a compiler produces from it. Read it line by line, it is instructions you know.Una función pequeña en C y el ensamblador que un compilador genera de ella. Léela línea a línea, son instrucciones que conoces.
Arguments a and b were already in R12 and R13. The compiler only needed a compare, a jump, and a return.Los argumentos a y b ya estaban en R12 y R13. El compilador solo necesitó una comparación, un salto y un retorno.
Every C control structure becomes the same two-instruction idea: set the flags, then a conditional jump.Toda estructura de control de C se vuelve la misma idea de dos instrucciones: fija los flags y luego salta condicionalmente.
if-else, do-while, every loop: the same shape. Only the condition and the jump change.if-else, do-while, cualquier bucle: la misma forma. Solo cambian la condición y el salto.
Counting toward zero drops the compare entirely. DEC already sets the Zero flag.Contar hacia cero elimina la comparación por completo. DEC ya fija el flag Zero.
DEC updates the flags as a side effect, so JNZ reads them directly.DEC actualiza los flags como efecto secundario, así que JNZ los lee directamente.
Write most code in C. Drop to assembly only where it pays off, and let the convention join the two.Escribe la mayoría en C. Baja a ensamblador solo donde compensa, y deja que la convención una ambos.
Assembly only where the compiler cannot match hand work: a tight loop, an ISR, exact timing.Ensamblador solo donde el compilador no iguala el trabajo manual: un bucle apretado, una ISR, temporización exacta.
Dereferencing a C pointer and the indirect register mode are the same mechanism: a register holds the address.Desreferenciar un puntero de C y el modo registro indirecto son el mismo mecanismo: un registro guarda la dirección.
Walking an array in C is autoincrement mode: R5 advances by the element size.Recorrer un array en C es el modo autoincremento: R5 avanza el tamaño del elemento.
An indexed access in C is indexed addressing: base register plus a constant offset.Un acceso indexado en C es direccionamiento indexado: registro base más un desplazamiento constante.
A 2-D table is stored row by row. The element address is a small offset calculation.Una tabla 2-D se guarda fila por fila. La dirección del elemento es un cálculo de desplazamiento pequeño.
Row 1, column 2 lands at byte +12: exactly what the arithmetic gives.Fila 1, columna 2 cae en el byte +12: justo lo que da la aritmética.
C and assembly share one machine and one contract.C y ensamblador comparten una máquina y un contrato.
The compiler emits MOV, ADD, CMP, jumps. Nothing you have not seen.El compilador emite MOV, ADD, CMP, saltos. Nada que no hayas visto.
The calling convention lets C and assembly call each other safely.La convención de llamada permite que C y ensamblador se llamen con seguridad.
A C pointer is a register holding an address: indirect addressing.Un puntero de C es un registro con una dirección: direccionamiento indirecto.