Professional Documents
Culture Documents
Topics to Cover
MSP430 ISA Assembler Primer Instruction Formats Double Operand Instructions Single Operand Instructions Jump Instructions Addressing Modes Instruction Length Instruction Cycles Instruction Disassembly
Chapter 05 - MSP430 ISA 2
Levels of Transformation
Problem Machine
Abstract, machine-independent; easier to write, read, debug, maintain More concrete, machine-dependent; error prone, harder to write, read, debug, maintain
Problem
Algorithm
Problem
Assembler
Engineer
Compiler
Coder
MSP430 ISA
The computer ISA defines all of the programmer-visible components and operations of the computer
Memory organization address space -- how may locations can be addressed? addressibility -- how many bits per location? Register set how many? what size? how are they used? Instruction set opcodes data types addressing modes
ISA provides all information needed for someone that wants to write a program in machine language (or translate from a high-level language to machine language).
Chapter 05 - MSP430 ISA 4
MSP430 ISA
MSP430 ISA
4 basic addressing modes. 8/16-bit instruction addressing formats. Memory architecture 16 16-bit registers 16-bit Arithmetic Logic Unit (ALU). 16-bit address bus (64K address space) 16-bit data bus (8-bit addressability) 8/16-bit peripherals
MSP430 ISA
MSP430 Registers
Points to (has the address of) the next instruction to be fetched. Each instruction occupies an even number of bytes. Therefore, the least significant bit (LSB) of the PC register is always zero. After executing an instruction, the PC register will have incremented by 2, 4, or 6 and point to the next instruction.
The MSP430 CPU pushes the return address of subroutine calls and interrupts on the stack. User programs store local data on the stack. The SP can be incremented or decremented automatically with each stack access. The stack grows down thru RAM and thus SP must be initialized with a valid RAM address. SP always points to an even address, so its LSB is always zero.
Chapter 05 - MSP430 ISA 6
MSP430 ISA
MSP430 Registers
The status of the MSP430 CPU is found in register R2. Only accessible through register addressing mode - all other addressing modes are reserved to support the constant generator.
V Overflow bit
SCG1
SCG0 OSCOFF CPUOFF GIE N Z C
MSP430 ISA
MSP430 ALU
Performs instruction arithmetic and logical operations. Instruction execution affects the state of the following flags:
The MCLK (Master) clock signal drives the CPU and ALU logic.
MSP430 ISA
A little-endian machine stores the least significant byte first (lowest address) within a word data type.
BYU CS/ECEn 124 Chapter 05 - MSP430 ISA 9
MSP430 ISA
Quiz 5.1
Pair up with another student and answer the following questions: 1. What is an ISA?
10
MSP430 ISA
16-bit Arithmetic Logic Unit (ALU). 16-bit address bus (64K address space) 16-bit data bus (8-bit addressability) 8/16-bit peripherals Little-endian storage order
11
Assembler Primer
MSP430 Assembler
1. labelstarts in the column 1 and may be followed by a colon (:) for clarity. 2. operationeither an instruction, which is translated into binary machine code for the processor itself, or a directive, which controls the assembler. 3. operandsdata needed for this operation (not always required). 4. commenttext following a semicolon (;).
BYU CS/ECEn 124 Chapter 05 - MSP430 ISA 12
Assembler Primer
MSP430 Assembler
Labels are case sensitive, but instructions and directives are not - pick a style and stick with it. Use comments freely in assembly language otherwise your program is unreadable and very difficult to debug. The default base (radix) of numbers in assembly language is decimal. The C-style notation 0xA5 for hexadecimal numbers is now widely accepted by assemblers. Other common notations include $A5, h'A5' and 0A5h. Binary numbers can similarly be written as 10100101b. Use symbolic names for constants and expressions. The ".equ" and ".set" assembler directives provide macro text replacement for this purpose. (Make upper case.)
Chapter 05 - MSP430 ISA 13
Assembler Primer
14
Instruction Formats
MSP430 Instructions
There are three formats used to encode instructions for processing by the CPU core:
The instructions for double and single operands, depend on the suffix used, (.w) word or (.b) byte These suffixes allow word or byte data access If the suffix is ignored, the instruction processes word data by default
Chapter 05 - MSP430 ISA 15
Instruction Formats
MSP430 Instructions
The MSP430 ISA uses three formats to encode instructions for processing by the CPU core:
Single and double operand instructions process word (16-bits) or byte (8-bit) data operations.
Instruction suffix selects word (.w) or byte (.b) Instructions process word data by default.
The first 4-bits (nybble) of an instruction is called the opcode and specifies the instruction format.
Chapter 05 - MSP430 ISA 16
Instruction Formats
MSP430 Instructions
Memory
0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0
PC
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1
0 0 1 0 1 1 1 1 1 1 1 0 0 1 0 0
Instruction Register
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
mov.w #0x0600,r1
0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0
Opcode
4 to 16 Decoder
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Opcode 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Instruction Undefined RCC, SWPB, RRA, SXT, PUSH, CALL, RETI JNE, JEQ, JNC, JC JN, JGE, JL, JMP MOV ADD ADDC SUBC SUB CMP DADD BIT BIC BIS XOR AND
Double Operand
17
MSP430 Instructions
6
b/w
5
As
Opcode
S-reg
D-reg
13
12
11
10
Condition
18
Mnemonic
Arithmetic instructions ADD(.B or .W) src,dst ADDC(.B or .W) src,dst DADD(.B or .W) src,dst SUB(.B or .W) src,dst SUBC(.B or .W) src,dst
Description
Add source to destination Add source and carry to destination Decimal add source and carry to destination Subtract source from destination Subtract source and not carry from destination
Logical and register control instructions AND(.B or .W) src,dst BIC(.B or .W) src,dst BIS(.B or .W) src,dst BIT(.B or .W) src,dst XOR(.B or .W) src,dst Data instructions CMP(.B or .W) src,dst MOV(.B or .W) src,dst dst-src srcdst Compare source to destination Move source to destination AND source with destination Clear bits in destination Set bits in destination Test bits in destination XOR source with destination
19
0100
0101
00
0100
One word instruction The instruction instructs the CPU to copy the 16-bit 2s complement number in register r5 to register r4
20
Mnemonic RRA(.B or .W) dst RRC(.B or .W) dst SWPB( or .W) dst SXT dst
PUSH(.B or .W) src SP-2SP, src@SP Program flow control instructions CALL(.B or .W) dst SP-2SP, PC+2@SP dstPC RETI
BYU CS/ECEn 124
@SP+SR, @SP+SP
Logically shift the contents of register r5 to the right through the status register carry
Opcode rrc
000100000
b/w 16-bits
0
Ad Register
00
D-reg r5
0101
One word instruction The CPU shifts the 16-bit register r5 one bit to the right (divide by 2) the carry bit prior to the instruction becomes the MSB of the result while the LSB shifted out replaces the carry bit in the status register
Chapter 05 - MSP430 ISA 22
Jump Instructions
13
12
11
10
Condition
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) 111: unconditional jump
Chapter 05 - MSP430 ISA 23
Jump Instructions
Jump instructions are executed based on the current PC and the status register Conditional jumps are controlled by the status bits Status bits are not changed by a jump instruction The jump off-set is represented by the 10-bit, 2s complement value:
Thus, the range of the jump is -511 to +512 words, (-1023 to 1024 bytes ) from the current instruction Note: Use a BR instruction to jump to any address
Chapter 05 - MSP430 ISA 24
Jump Instructions
One word instruction The CPU will add to the incremented PC (R0) the value -28 x 2 if the carry is set
25
Today
Exam I
Thursday, Feb 7 thru Monday, Feb 11 Testing Center, Grant Building 60 Questions, Chapters 1 through 5 50% penalty if you miss exam Good idea to have scratch paper on hand 1 page, handwritten notes Take the pre-test exam!! Study slides, homework, and reading material
Chapter 05 - MSP430 ISA 26
MSP430 Instructions
Quiz 5.2
1. How are the sixteen MSP430 registers the same? 2. How do they differ? 3. What does 8-bit addressibility mean? 4. Why does the MSP430 have a 16-bit data bus? 5. What does the following instruction do?
addc.w
BYU CS/ECEn 124
r11,r12
Chapter 05 - MSP430 ISA 27
MSP430 Instructions
28
MSP430 Instructions
Addressing Modes
Indexed Register
Indirect Auto-inc
cnt: var:
.word .word
Addressing Modes
The MSP430 has four basic addressing modes for the source address:
00 = Rs - Register 01 = x(Rs) - Indexed Register 10 = @Rs - Register Indirect 11 = @Rs+ - Indirect Auto-increment
When used in combination with registers R0-R3, three additional source addressing modes are available:
Addressing Modes
There are only two basic addressing modes for the destination address:
When used in combination with registers R0/R2, two additional destination addressing modes are available:
33
Addressing Modes
00 = Register Mode
add.w r4,r10
Memory PC PC
0x540a 0x540a IR
;r10 = r4 + r10
CPU
Registers
PC R4
ADDER
+2
R10
ALU
34
Addressing Modes
01 = Indexed Mode
add.w 6(r4),r10 ;r10 = M(r4+6) + r10
Memory PC PC PC
0x541a 0x0006 0x541a IR
CPU
Registers
PC R4
ADDER
+2
R10
ALU
35
Addressing Modes
+2
R10
ALU
36
Addressing Modes
+2
0002
R4
ADDER
R10
ALU
37
Addressing Modes
+2
ADDER
cnt
R10
ALU
38
Addressing Modes
+2
0000
ADDER
cnt
R10
ALU
39
Addressing Modes
+2
ADDER
R10
ALU
40
Addressing Modes
Constant Generator
add.w #1,r10
Memory PC PC
0x531a 0x531a IR
;r10 = #1 + r10
CPU
Registers
0000 0001 0002 0004 0008 ffff
PC
+2
ADDER
R10
ALU
41
Addressing Modes
+2
PC cnt
Address Bus
var
ALU
42
Instruction Length
MSP430 Instructions
Instruction Length
Format I:
Format II: Format III:
8 8
7 6 5 4 Ad b/w As 7 6 5 4 b/w Ad
3 3
2 1 D-reg
2 1 0 D/S-reg
1 additional word for each of the following addressing modes: mov 10(r4),r5
Source index mode (As = 01) Source immediate mode (As = 11, SR = PC) Destination index mode (Ad = 1)
Chapter 05 - MSP430 ISA
mov cnt,r5 mov &P1IN,r5 mov #100,r5 mov r4,10(r5) mov r4,cnt mov r4,&P1OUT
44
Instruction Length
Quiz 5.3
What is the length (in words) for each of the following instructions?
Instruction add.w r5,r6 add.w cnt(r5),r6 add.w @r5,r6 add.w @r5+,r6 add.w cnt,r6 add.w &cnt,r6 add.w #100,r6 mov.w r10,r11 mov.w @r5,6(r6) mov.w 0(r5),6(r6) L
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. 15. 16. 17. 18. 19. 20.
Instruction mov.w EDE,TONI mov.b &MEM,&TCDAT mov.w @r10,r11 mov.b @r10+,tab(r6) mov.w #45,TONI mov.w #2,&MEM mov.b #1,r11 mov.w #45,r11 mov.b #-1,-1(r15) mov.w @r10+,r10
BYU CS/ECEn
45
Instruction Length
What is the length (in words) for each of the following instructions?
Instruction add.w r5,r6 add.w cnt(r5),r6 add.w @r5,r6 add.w @r5+,r6 add.w cnt,r6 add.w &cnt,r6 add.w #100,r6 mov.w r10,r11 mov.w @r5,6(r6) mov.w 0(r5),6(r6) L 1 2 1 1 2 2 2 1 2 3
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
11. 12. 13. 14. 15. 16. 17. 18. 19. 20.
Instruction mov.w EDE,TONI mov.b &MEM,&TCDAT mov.w @r10,r11 mov.b @r10+,tab(r6) mov.w #45,TONI mov.w #2,&MEM mov.b #1,r11 mov.w #45,r11 mov.b #-1,-1(r15) mov.w @r10+,r10
L 3 3 1 2 3 2 1 2 2 1
46
BYU CS/ECEn
Instruction timing:
1 cycle to fetch instruction word +1 cycle if source is @Rn, @Rn+, or #Imm +2 cycles if source uses indexed mode
1st to fetch base address 2nd to fetch source Includes absolute and symbolic modes
+2 cycles if destination uses indexed mode +1 cycle if writing destination back to memory +1 cycle if writing to PC (R0) Jump instructions are always 2 cycles
Blinky Lab 47
Example Cycles/Length...
Example add R5,R8 Src Rn Dst Rm Cycles Length 1 1
add @R5,R6
mov @R5+,R0 add R5,4(R6)
@Rn
@Rn+ Rn
Rm
PC x(Rm)
2
3 4
1
1 2
add R8,EDE
add R5,&EDE add #100,TAB(R8)
Rn
Rn #n #1
EDE
&EDE x(Rm) &EDE
Blinky Lab
4
4 5
2
2 3
add &TONI,&EDE
add #1,&EDE
BYU CS/ECEn 124
&TONI &EDE
6
4
3
2
48
Quiz 5.4
;******************************************************************************* ; CS/ECEn 124 Lab 3 - blinky.asm ;******************************************************************************* ; cycles = --; MCLK = --- cycles / 10 seconds = --- Mhz ; CPI = MCLK / --; MIPS = MCLK / CPI / 1000000 = --- MIPS .cdecls C,LIST, "msp430.h" ; MSP430 COUNT .equ 0 ; delay count ;-----------------------------------------------------------------------------.text ; beginning of executable code ;-----------------------------------------------------------------------------RESET: mov.w #0x0280,SP ; init stack pointer mov.w #0x5a80,&0x0120 ; stop WDT (WDTCTL) bis.b #0x01,&0x0022 ; set P1.0 as output (P1DIR) mainloop: delayloop: xor.b mov.w sub.w jnz jmp #0x01,&0x0021 #COUNT,r15 #1,r15 delayloop mainloop ; ; ; ; ; toggle P1.0 (P1OUT) use R15 as delay counter delay over? n y, toggle led
;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET Vector .word RESET ; start address .end
Blinky Lab
49
;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET Vector .word RESET ; start address .end
Blinky Lab
50
Quiz 5.5
Given a 1.2 MHz processor, what value for DELAY would result in a 1/4 second delay?
DELAY .equ ???
mov.w
delay1: delay2: mov.w sub.w jne sub.w jne
#DELAY,r12
#1000,r15 #1,r15 delay2 #1,r12 delay1
;
; ; ; ; ;
BYU CS/ECEn
51
Given a 1.2 MHz processor, what value for DELAY would result in a 1/4 second delay?
DELAY .equ ???
mov.w
delay1: delay2: mov.w sub.w jne sub.w jne
#DELAY,r12
#1000,r15 #1,r15 delay2 #1,r12 delay1
; 2 cycles
; 2 cycles
cycle 2 + DELAY (2 + 3000 + 1 + 2) cycles cycle cycles 300000 2 1 second = 1,200,000 cycles DELAY = ---------0.25 seconds = 1,200,000 / 4 = 300,000 cycles 3005 = 99.833 = 100 300,000 cycles = 2 + (DELAY x 3005) cycles
BYU CS/ECEn Chapter 6 - MSP430 Microarchitecture 52
; 3000 ; ; ;
1 2 1 2
Disassembling Instructions
Instruction Disassembly
Instruction Disassembly
R0 R0
0xf800: 0xf802: 0xf804: 0xf806: 0xf808: 0xf80a: 0xf80c: 0xf80e: 0xf810: 0xf812: 0xf814: 0xf816: 0xf818:
4031 0100 0000 0011 0001 0280 40b2 5a80 0120 d3d2 0022 e3d2 0021 430f 831f 23fe 3ffa
Chapter 05 - MSP430 ISA 55
Instruction Disassembly
R0
0xf800: 0xf802: 0xf804: 0xf806: 0xf808: 0xf80a: 0xf80c: 0xf80e: 0xf810: 0xf812: 0xf814: 0xf816: 0xf818:
4031 0100 0000 0011 0001 0280 40b2 5a80 0120 d3d2 0022 e3d2 0021 430f 831f 23fe 3ffa
Chapter 05 - MSP430 ISA
mov .w
56
Instruction Disassembly
R0 R0
0xf800: 0xf802: 0xf804: 0xf806: 0xf808: 0xf80a: 0xf80c: 0xf80e: 0xf810: 0xf812: 0xf814: 0xf816: 0xf818:
4031 0100 0000 0011 0001 0280 40b2 5a80 0120 d3d2 0022 e3d2 0021 430f 831f 23fe 3ffa
Chapter 05 - MSP430 ISA
57
Instruction Disassembly
R0
0xf800: 0xf802: 0xf804: 0xf806: 0xf808: 0xf80a: 0xf80c: 0xf80e: 0xf810: 0xf812: 0xf814: 0xf816: 0xf818:
4031 0100 0000 0011 0001 0280 40b2 5a80 0120 d3d2 0022 e3d2 0021 430f 831f 23fe 3ffa
Chapter 05 - MSP430 ISA
58
Instruction Disassembly
R0
0xf800: 0xf802: 0xf804: 0xf806: 0xf808: 0xf80a: 0xf80c: 0xf80e: 0xf810: 0xf812: 0xf814: 0xf816: 0xf818:
4031 0100 0000 0011 0001 0280 40b2 0100 0000 1011 0010 5a80 0120 d3d2 0022 e3d2 0021 430f 831f 23fe 3ffa
Chapter 05 - MSP430 ISA
59
Instruction Disassembly
Addressing Modes
Address Mode Register 0 Symbolic Indexed Absolute +1 Indirect +4 +2 Immediate Indirect auto-inc +8 -1 As 00 00 01 01 01 01 10 10 10 11 11 11 11 Registers
R0-R2, R4-R15 R3 R0 R1, R4-R15 R2 R3 R0-R1,R4-R15 R2 R3 R0 R1,R4-R15 R2 R3
Syntax
Rn #0 ADDR X(Rn) &ADDR #1 @Rn #4 #2 #N @Rn+ #8 #-1
Operation
Register Contents. 0 Constant source / bit bucket destination (PC+next word) points to operand. (X(PC)) (Rn+X) points to operand. X is next code word. Next code word is the absolute address. (X(SR)) +1 Constant Rn points to operand. +4 Constant +2 Constant Next word is the constant N. (@PC+) Rn points to operand, Rn is incremented (1 or 2). +8 Constant -1 Constant
Destination
Source
Ad 0 1 1 1 -
Registers
R0-R2, R4-R15 R0 R1, R4-R15 R2 R3
Syntax
Rn ADDR X(Rn) &ADDR -
Operation
Register Contents. (PC+next word) points to operand. (X(PC)) (Rn+X) points to operand. X is next code word. Next code word is the absolute address. (X(SR)) Undefined
60
Instruction Disassembly
Quiz 5.6
Instruction Disassembly
R0 R0 R0
Instruction Disassembly
R0 R0 R0 R0
Instruction Disassembly
R0 R0
Instruction Disassembly
R0 R0
Instruction Disassembly
R0 R0 R0
0=word, 1=byte and instruction mode SR=PC, DR=R1, indirect,register auto-inc mode (immediate)
Chapter 05 - MSP430 ISA 66
Instruction Disassembly
R0 R0 R0
Instruction Disassembly
R0 R0 R0
sub.w #1 ,0(r1)
Instruction Disassembly
sub.w #1 ,0(r1)
jne 0x8026
R0 R0
69
Instruction Disassembly
71