MACHINE CODE
Layout of bits in Format I is shown below. There are individual
fields, they are:
• Opcode: operation code. Highest 12 values for format I and rest for jump and format II
• S-reg, D-reg: 4 bits. Specifies the CPU registers.
• As: 2 bits. Gives mode of addressing for the source
• Ad: 1 bit gives mode of addressing for destination
Format 2:
Single operand
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Op-code b/w Ad D/S-reg
Jump instruction
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Op-code Condition 10-bit, 2’s complement PC offset
• There are 7 addressing modes for the source.
• There are 4 addressing modes for destination of which, 2 are emulated
•For destination:
Register mode; Indexed mode; Symbolic mode;
EXAMPLE:
Instruction: 1.) mov.w R5,R6
Op-code
mov S-regr5 RegisterAd 16-bitsb/w RegisterAs D-regr4
0 1 0 0
0 1 0 1
0
0
0 0
0 1 10
Encoded value: 0x4506 2.) add.w R5,R6
3.) mov.w #5, R6;
For double operand instruction ( labels)
mov.w r5,TONI
Op-code
mov S-regr5
Ad Symboli
c
b/w
16-bits RegisterAs D-regPC
0 1 0 0
0 1 0 1
1
0
0 0
0 0 0 0
2’s complement PC-relative destination index
Copy the contents of a PC-relative memory location to
another PC-relative memory location
Assembly:
mov.b EDEN,TONI
Instruction code:
0x40d0
Three word instruction
The CPU copies the 8-bit contents of EDEN (pointed to by
source index + PC
) to TONI (pointed to by
destination index + PC
)
Op-code
mov S-regPC SymbolicAd 8-bitsb/w SymbolicAs D-regPC
0 1 0 0
0 0 0 0
1
1
0 1
0 0 0 0
2’s complement PC-relative source index
Logically shift the contents of register
r5
to the right through the
status register carry
Assembly:
rrc.w r5
Instruction code:
0x1005
Op-code
rrc
16-bits
b/w
Register
Ad
D-reg
r5
0 0 0 1 0 0 0 0 0
0
0 0
0 1 0 1
Assembly:
rra.b &P2OUT
Instruction code:
0x1152
Op-code
rra
8-bits
b/w
Indexed
Ad
D-reg
r2
0 0 0 1 0 0 0 1 0
1
0 1
0 0 1 0
Absolute memory address (P2OUT)
JUMP
INSTRUCTIONS
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Op-code Condition 10-bit, 2’s complement PC offset
Jump instructions are used to direct program flow to
another part of the program.
The condition on which a jump occurs depends on the
Condition field consisting of 3 bits:
000: jump if not equal 001: jump if equal
010: jump if carry flag equal to zero 011: jump if carry flag equal to one 100: jump if negative (N = 1)
101: jump if greater than or equal (N = V) 110: jump if lower (N V)
Continue execution at the label
main
if the carry bit is set
Assembly:
jc main
Instruction code:
0x2fe4
One word instruction
The CPU will add to the
PC
(R0) the value
-28 x 2
if the
carry is set
Op-code
JC
Condition
Carry Set
10-Bit, 2’s
complement PC
offset
-28
ILLEGAL OPERATIONS
Fetching data:
˚
data fetched from non-existent region is random.
˚
Data for word must be aligned to even address.
˚
Peripheral registers for word access, meant for read only as
words.
Illegal instructions:
•
Instructions are 16-bit word. Not all 2
16are used.
•
Ranges 0x0000-0x0FFF and 0x1000-0x1FFF are not used.
•
0x0FFFF, illegal opcode trap. Program wanders into un-
programmed flash memory.
Fetching data
:
•
Legit to read from flash or Ram but not peripheral register.
•
It is not preferred to read from non-existent memory, causes
reset.
REVIEW
Instruction:
mnemonic src,dst ;
Registers:• Header file : #include<msp430XX.h>
• Relocating segments: RSEG / ASEG
• File : *.s43
• Use infinite loop as there is no operating system to take over in a relatively small embedded system.
EXAMPLES
• Basic program is the hello world program.
#include <stdio.h> void main (void)
{
printf("hello , world\n"); }
• Back then, the system would send the printf() to a printer or any display device.
• It can also be displayed on character LCD, but simpler to turn on the LEDs.
LEDS
#include <msp430x11x1.h> ; Header file for this device
ORG 0xF000 ; Start of 4KB flash memory
Reset: ; Execution starts here
mov.w #WDTPW|WDTHOLD ,& WDTCTL ; Stop watchdog timer
mov.b #00011000b,& P2DIR ; Set pins with LEDs to output
mov.b #00001000b,& P2OUT; LED2 (P2.4) on , LED1 (P2.3) off (active low!)
InfLoop: ; Loop forever ...
jmp InfLoop ; ... doing nothing
;---ORG 0xFFFE ; Address of MSP430 RESET Vector
#include <msp430XX.h> // Specific device
void main (void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P2DIR = 0x18; // Set pins with LEDs to output , 0b00011000
P2OUT = 0x08; // LED2 (P2.4) on , LED1 (P2.3) off (active low!)
for (;;)
{ // Loop forever ...
} // ... doing nothing
COPYING STRINGS
ALP to copy string
MyStrCpy: jmp CopyTest
CopyLoop: mov.b @R14+,0(R12) inc.w R12
CopyTest: tst.b 0(R14) jnz CopyLoop
clr.b 0(R12) terminate string
ret
Copying a string in C
void MyStrCpy(char * dest , const char * src) {
while (*src != '\0') // test for end of string {
*dest++ = *src ++; // copy nonnull character
}
*dest = '\0'; // terminate destination string
Copying a block of data between fixed addresses.
mov.w #BeginSource ,R14 ; Load address of source
jmp CopyTest CopyLoop:
mov.b @R14+,BeginDest -BeginSource+0 xFFFF(R14) CopyTest:
cmp.w #EndSource ,R14 ; Set flags for (R14 - end of source)
jlo CopyLoop
jmp $ ; Infinite , empty loop
RSEG DATA16_C ; Segment for constant data in ROM
BeginSource: ; Source data
DB "hello , world\n“ ; "" causes a '\0' to be appended
Reading a value from push button
ALP
bit.b #BIT1 ,&P2IN ; Test button on bit 1 of P2IN; C = ˜Z
rrc.b ShiftReg ; Insert C into msb of shift register In C
ShiftReg >>= 1; // Shift register right by 1 position
if (P2IN_bit.P2IN_1 == 1)
{ // Test button on bit 1 of P2IN
ShiftReg |= BIT7; // Set msb of byte
} else {
ShiftReg &=˜BIT7; // Clear msb of byte
REFLECTION ON CPU
INSTRUCTION SET
•
The MSP430 was designed from the start for low power
consumption, computing in very few clock cycles
•
Most programs today are written in C so the processor is
designed for efficient compilation
•
an advantage of a 16-bit processor is that registers can be
used for either data or addresses.
•
Moreover, most ADC now produce more than 8 bits of
output ,so it is more efficient to handle words rather than
bytes.
IS MSP430 RISC?
Registers of CPU
• a straightforward set of 16 registers for addresses and data.
• The constant generator is a particularly creative feature.
Is MSP 430 a RISC?
Four main features of a RISC processor:
Small set of general-purpose instructions Large bank of general-purpose registers Load–store architecture
Single-cycle execution
Ex: clearing control bit in the timer
bic.w #MC0|MC1 ,& TACTL ; stop timer [3 words , 5 cycles]
Also
load.w #TACTL ,R4 ; load address of TACTL [2w, 2c]
load.w @R4 ,R5 ; load value of TACTL [1w, 2c]
load.w #MC0|MC1 ,R6 ; load immediate operand [2w, 2c]
bic.w R6 ,R5 ; perform operation [1w, 1c]
Addressing Modes
• A Format I instruction with two operands must fit into a 16-bit word
• 4 bits for operation, 4 bits for dst, src regester and 1 bit for B/W, leaving only 3 bits left.
• 2 for src, 1 for dst. This makes use of constant generator to give different modes for source easily.
• The lack of indirect or autoincrement modes for the destination can be frustrating when writing assembly language
• A missing mode for addressing is predecrement, useful to push data onto stack
Conclusion
• MSP430 has orthogonality.
• Access to memory is simple and uniform.
• The constant generator makes programs faster and more compact.
• It is extremely easy to forget the # when calling subroutines in the most straightforward way and the results are disastrous.
EXTRAS
For an LED,
Toggling bits P1OUT |= BIT4;
To turn it off, we give: P1OUT &= ~BIT4;
To toggle its state (off if on, on if off): P1OUT ^= BIT4;
Coding Interrupts
Every ISR has this structure:
#pragma vector = <VECTOR_NAME> __interrupt void <ISR_NAME> (void) {
BUTTON
PRESS
#include <msp430XX.h> #define red_LED BIT0 #define grn_LED BIT6 #define BTN BIT3 void delay(void);
void main(void) { unsigned int flash;
WDTCTL = WDTPW + WDTHOLD; P1OUT = 0;
P1DIR |= red_LED + grn_LED; // LED pins to outputs, BTN is // still an input by default.
for (;;) {
for (flash=0; flash<7; flash++) {
P1OUT |= red_LED; // red LED on delay(); // call delay function P1OUT &= ~red_LED; // red LED off delay(); // delay again
}
while ((P1IN & BTN) == BTN); // wait for button press for (flash=0; flash<7; flash++) {
P1OUT |= grn_LED; // green LED on delay();
P1OUT &= ~grn_LED; // green LED off delay();
}
while ((P1IN & BTN) == BTN); // wait for button press }