The Basics of ASM

advertisement
E91
Assembly Language
The basics
Anatomy of a simple program
Make PROGSTART available to linker.
Anything after “;” is comment
Share header file with “C”
Note: not in column 1.
;********************************************************************
;
MSP-FET430F2013 Demo - Software Toggle P1.0
;********************************************************************
.cdecls C,LIST, "msp430x20x3.h"
“.text” predefined to be start
.global PROGSTART
of FLASH (0xf800 in 430F2013)
;
;-------------------------------------------------------------------“PROGSTART” is label (starts in
.text
; Progam Start
column 1)
;-------------------------------------------------------------------PROGSTART
mov.w
#0280h,SP
; Initialize stackpointer
“mov” instruction, format:
mov.w
#WDTPW+WDTHOLD,&WDTCTL ; Stop WDT
mov src, dst
bis.b
#001h,&P1DIR
,
; P1.0 output
p
“.w”=word (default), “.b” =byte
;
Mainloop
xor.b
#001h,&P1OUT
; Toggle P1.0
mov.w
#050000,R15
; Delay to R15
“bis” (bit set): bis src, dst
L1
dec.w
R15
; Decrement R15
jnz
L1
; Delay over?
“MainLoop” and “L1” are labels
“xor”: xor src, dst
R15 is a “register”
“d ” (decrement):
“dec”
(d
t) dec
d dst
d t
Check to see if last math result
was 0. Jump if Not Zero:
jnz label
jmp
Mainloop
;-------------------------------------------------------------------;
Interrupt Vectors
;-------------------------------------------------------------------.sect
".reset"
; RESET Vector
.short PROGSTART
;
.end
JuMP back to main loop:
jmp label
“.reset” is defined in header file
; Again
T ll assembler
Tell
bl this
hi is
i the
h end
d
Address of “PROGSTART” is
placed here
Registers
g
PC, SP, SR, and R15 used in our program
Status Register
Recall Memory Addresses
P1IN : 0x20
P1OUT: 0x21
P1DIR: 0x22
DEMO…
Instruction Set
Dual Operand Instructions
Instruction Set
Single Operand Instructions
Instruction Set
Jumps
Note, there are 8 possible jump instructions. This will come up later.
Sources & Destinations
Addressing Modes Introduction (1)
What are some of the things we can use for sources and destinations?
The Source Operand
• “Immediate” – the value of the source operand is stored immediately after the instruction (i.e., the source operand is a constant). The immediate value is prefixed with “#” :
fi d i h “#”
0xF81C:
403F 123A
MOV.W
#0x123a,R15
• “Register” – the source operand is in a register:
0xF81E:
4F0E
MOV.W
R15,R14
• “Absolute” – the address of the source operand is stored immediately after the instruction. The absolute value (address) is prefixed by “&”:
instruction. The absolute value (address) is prefixed by & :
0xF820:
425F 0020
MOV.B
&P1IN,R15
• “Symbolic” , Similar to “Absolute” but uses offset from Program Counter.
• Also … “Indexed”, “Indirect Register”, “Indirect Autoincrement” … we’ll do these next week.
Destinations
Addressing Modes Introduction (2)
The Destination Operand
• “Register” – the destination operand is in a register:
0xF81E: 4F0E
MOV.W
R15,R14
• “Absolute”
Absolute – the address of the destination operand is stored immediately after the address of the destination operand is stored immediately after
the instruction:
0xF822: 40F2 00F3 0021
MOV.B
#0x00f3,&P1OUT
• “Symbolic”
Symbolic , Similar to Similar to “Absolute”
Absolute but uses offset from Program Counter. but uses offset from Program Counter
• It makes no sense to have an “Immediate” mode for the destination (you can’t change a constant value) – so this address mode isn’t available.
•Also … “Symbolic” , “Indexed”, are available for destination… we’ll do these next week.
• “Indirect Register”, “Indirect Autoincrement” are not available for destination.
Addressing Mode Examples
Pertinent section of code
PROGSTART
mov.w
mov.w
#0280h,SP
#WDTPW+WDTHOLD,&WDTCTL
mov.w
mov.w
mov.w
&myWord,myWordVar1
&myWord,&myWordVar1
&myWord,&myWordVar2
mov.b
mov.b
mov.b
mov.b
#33h,
#33h,
#33h,
#33h,
mov.w
myWord, myBytes
&myByteVar1
&myByteVar2
&myByteVar3
&myByteVar4
; Define data in flash (immediately after code)
myBytes
.byte 05ah, 03ch
myWord
.word 0f2h
; Initialize stackpointer
; Stop WDT
Data in flash accessed either by “Absolute” (& prefix) or Symbolic
Data in RAM accessed either by “ b l ” (&
“Absolute” (& prefix) or Symbolic
f )
b l
Immediate Operands
(Source only)
Register Operands
; Define (unintialized) data in RAM at 0200h. (.bss symbol, size, alignment)
.bss
myWordVar1,2,2 ;location 0200h
.bss
myWordVar2,2,2;location 0202h
%
.bss
myByteVar1,1,1;location
0204h
.bss
myByteVar2,1,1;location 0205h
.bss
myByteVar3,1,1;location 0206h
.bss
myByteVar4,1,2;location 0208h
Addressing Mode Examples
Details Immediate Absolute
Symbolic
y
Code Detail
mov.w &myWord, myWordVar1
mov w &myWord, &myWordVar1
mov.w
mov.w &myWord, &myWordVar2
mov.b
mov.b
mov.b
mov.b
#33h,
#33h,
#33h,
#33h,
&myByteVar1
&myByteVar2
&myByteVar3
&myByteVar4
0xF80A:
0xF810:
0xF816:
4290 F83C 09F2
4292 F83C 0200
4292 F83C 0202
MOV.W
MOV.W
MOV.W
MOV W
&myWord,myWordVar1
&myWord,&myWordVar1
&myWord
&myWord,&myWordVar2
&myWordVar2
0xF81C:
0xF822:
0xF828:
0xF82E:
40F2
40F2
40F2
40F2
0204
0205
0206
0208
MOV.B
MOV.B
MOV.B
MOV.B
#0x0033,&myByteVar1
#0x0033,&myByteVar2
#0x0033,&myByteVar3
#0x0033,&myByteVar4
y y
0xF834:
4090 0006 0002
MOV.W
myWord,myBytes
JMP
(0xf8f0)
.word
0x00F2
mov.w myWord, myBytes
; Define data in flash (after code)
myBytes
B t
.byte
b t 05ah,
05 h 03ch
03 h
myWord
.word 0f2h
; Define data in RAM at 0200h.
.bss myWordVar1,2,2 ;location
.bss myWordVar2,2,2 ;location
.bss myByteVar1,1,1 ;location
.bss myByteVar2,1,1 ;location
.bss myByteVar3,1,1 ;location
.bss myByteVar4,1,2 ;location
0033
0033
0033
0033
Ruh‐Roh!
myBytes:
B t
0xF83A:
3C5A
myWord:
0xF83C:
00F2
0200h
0202h
0204h
0205h
0206h
0208h
Aside (1): Machine Code
Linked code (w/ addresses)
.text,
, _text,
, PROGSTART,
, $
$../asmonly.asm:8:22$:
/
y
$
0xF800:
4031 0280
MOV.W
#0x0280,SP
0xF804:
40B2 5A80 0120
MOV.W
#0x5a80,&Watchdog_Timer_WDTCTL
0xF80A:
D3D2 0022
BIS.B
#1,&Port_1_2_P1DIR
Mainloop:
0xF80E:
E3D2 0021
XOR.B
#1,&Port_1_2_P1OUT
0xF812:
403F 5000
MOV.W
#0x5000,R15
L1:
0xF816:
831F
DEC.W
R15
0xF818:
23FE
JNE
(L1)
0xF81A:
3FF9
JMP
(Mainloop)
Note: Some instructions are longer than others (these will be slower)
Let’s see how we get the machine code from the assembly language.
Aside (2): Machine Code
Let’s look at JNE
ASIDE (3): Machine Code
Things to know:
.text, _text, PROGSTART, $../asmonly.asm:8:22$:
0xF800:
4031 0280
MOV.W
#0x0280,SP
0xF804:
40B2 5A80 0120
MOV.W
#0x5a80,&Watchdog_Timer_WDTCTL
0xF80A:
D3D2 0022
BIS.B
#1,&Port
#1,&Port_1_2_P1DIR
1 2 P1DIR
Mainloop:
0xF80E:
E3D2 0021
XOR.B
#1,&Port_1_2_P1OUT
0xF812:
403F 5000
MOV.W
#0x5000,R15
L1:
0xF816:
831F
DEC.W
R15
0xF818:
23FE
JNE
(L1)
0xF81A:
3FF9
JMP
(Mainloop)
ASIDE (4): Machine Code
L1:
0xF816:
0xF818:
831F
23FE
DEC.W
JNE
Opcode
0x23FE=
0
0
R15
(L1)
C
1
0
0
OFFSET
0
1
1
1
1
1
1
1
1
Offset = 11 1111 1110binary = ‐2decimal.
PCnew = PC
PCold + 2 2 + PC
PCoffsetx2 x2 = 0xf818 0xf818 + 2 2 + ((‐2)x2
2)x2
= 0xf818‐2 = 0xf816
We could do the same for 0xF81A:
We could do the same for 3FF9
JMP
(Mainloop)
What if we need to jump farther than allowed with a 10 bit offset?
An “emulated” instruction
Branch to destination
BR dst
mov dst,PC
1
0
Resources Used
•
•
•
•
http://focus.ti.com/lit/ug/slau144e/slau144e.pdf (MSP430X2XX Family User’s Guide)
http://www ti com/lit/zip/slac080 (Example code MSP430X2XX)
http://www.ti.com/lit/zip/slac080
http://en.wikipedia.org/wiki/TI_MSP430 (Good description of assembly language at hardware level)
http://focus.ti.com/lit/ug/slau131e/slau131e.pdf (MSP430 Assembly Language Tools User's Guide)
Download