Post on 15-Oct-2019
transcript
E91Assembly Language
The basics
Anatomy of a simple programAnything after “;” is comment Make PROGSTART available to linker.
;********************************************************************; MSP-FET430F2013 Demo - Software Toggle P1.0;********************************************************************.cdecls C,LIST, "msp430x20x3.h".global PROGSTART
;
“.text” predefined to be startof FLASH (0xf800 in 430F2013)
Share header file with “C”Note: not in column 1.
;--------------------------------------------------------------------.text ; Progam Start
;--------------------------------------------------------------------PROGSTART mov.w #0280h,SP ; Initialize stackpointer
mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDTbis.b #001h,&P1DIR ; P1.0 output
“PROGSTART” is label (starts in column 1)
“mov” instruction, format:mov src, dst , ; p
;Mainloop xor.b #001h,&P1OUT ; Toggle P1.0
mov.w #050000,R15 ; Delay to R15L1 dec.w R15 ; Decrement R15
jnz L1 ; Delay over?
“.w”=word (default), “.b” =byte
“bis” (bit set): bis src, dst
“MainLoop” and “L1” are labels
jmp Mainloop ; Again
;--------------------------------------------------------------------; Interrupt Vectors;--------------------------------------------------------------------“d ” (d t) d d t
R15 is a “register”
“xor”: xor src, dst
;--------------------------------------------------------------------.sect ".reset" ; RESET Vector.short PROGSTART ;.end
“dec” (decrement): dec dst
Check to see if last math result was 0. Jump if Not Zero: jnz labelJuMP back to main loop: T ll bl hi i h d
Address of “PROGSTART” is placed here
JuMP back to main loop: jmp label
“.reset” is defined in header file
Tell assembler this is the end
Registersg
PC, SP, SR, and R15 used in our program
Status Register
Recall Memory AddressesP1IN : 0x20P1OUT: 0x21P1DIR: 0x22P1DIR: 0x22
DEMO…
Instruction SetDual Operand InstructionsDual Operand Instructions
Instruction SetSingle Operand InstructionsSingle Operand Instructions
Instruction SetJumpsJumps
Note, there are 8 possible jump instructions. This will come up later.
Sources & DestinationsAddressing 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
fi d i h “#”prefixed with “#” :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.
DestinationsAddressing Modes Introduction (2)
The Destination Operand• “Register” – the destination operand is in a register:0xF81E: 4F0E MOV.W R15,R14
• “Absolute” – the address of the destination operand is stored immediately afterAbsolute the address of the destination operand is stored immediately after the instruction:0xF822: 40F2 00F3 0021 MOV.B #0x00f3,&P1OUT
• “Symbolic” Similar to “Absolute” but uses offset from Program Counter• Symbolic , Similar to Absolute 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 ExamplesPertinent section of codePROGSTART mov.w #0280h,SP ; Initialize stackpointer
mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDT
mov.w &myWord,myWordVar1mov.w &myWord,&myWordVar1mov.w &myWord,&myWordVar2
mov.b #33h, &myByteVar1
Data in flash accessed either by “Absolute” (& prefix) or Symbolic
Data in RAM accessed either by “ b l ” (& f ) b lmov.b #33h, &myByteVar2
mov.b #33h, &myByteVar3mov.b #33h, &myByteVar4
mov.w myWord, myBytes
Immediate Operands(Source only)
“Absolute” (& prefix) or Symbolic
; Define data in flash (immediately after code)myBytes .byte 05ah, 03chmyWord .word 0f2h
; Define (unintialized) data in RAM at 0200h. (.bss symbol, size, alignment)
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 ExamplesDetails Absolute SymbolicImmediateDetails
Code Detail
mov.w &myWord, myWordVar1mov w &myWord, &myWordVar1
0xF80A: 4290 F83C 09F2 MOV.W &myWord,myWordVar10xF810: 4292 F83C 0200 MOV.W &myWord,&myWordVar10xF816: 4292 F83C 0202 MOV W &myWord &myWordVar2
y
mov.w &myWord, &myWordVar1mov.w &myWord, &myWordVar2
mov.b #33h, &myByteVar1mov.b #33h, &myByteVar2mov.b #33h, &myByteVar3
0xF816: 4292 F83C 0202 MOV.W &myWord,&myWordVar2
0xF81C: 40F2 0033 0204 MOV.B #0x0033,&myByteVar10xF822: 40F2 0033 0205 MOV.B #0x0033,&myByteVar20xF828: 40F2 0033 0206 MOV.B #0x0033,&myByteVar30xF82E: 40F2 0033 0208 MOV.B #0x0033,&myByteVar4
mov.b #33h, &myByteVar4
mov.w myWord, myBytes
; Define data in flash (after code)B t b t 05 h 03 h
y y
0xF834: 4090 0006 0002 MOV.W myWord,myBytes
B t
Ruh‐Roh!
myBytes .byte 05ah, 03chmyWord .word 0f2h
; Define data in RAM at 0200h.
myBytes:0xF83A: 3C5A JMP (0xf8f0)
myWord:0xF83C: 00F2 .word 0x00F2
; Define data in RAM at 0200h. .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
Aside (1): Machine CodeAside (1): Machine CodeLinked code (w/ addresses)
.text, text, PROGSTART, $../asmonly.asm:8:22$:, _ , , $ / y $0xF800: 4031 0280 MOV.W #0x0280,SP0xF804: 40B2 5A80 0120 MOV.W #0x5a80,&Watchdog_Timer_WDTCTL0xF80A: D3D2 0022 BIS.B #1,&Port_1_2_P1DIR
Mainloop:0xF80E: E3D2 0021 XOR.B #1,&Port_1_2_P1OUT0xF812: 403F 5000 MOV.W #0x5000,R15
L1:0xF816: 831F DEC.W R150xF818: 23FE JNE (L1)0xF81A: 3FF9 JMP (Mainloop)
Let’s see how we get the machine code from the assembly language.
Note: Some instructions are longer than others (these will be slower)
Aside (2): Machine CodeAside (2): Machine Code
Let’s look at JNE
ASIDE (3): Machine CodeASIDE (3): Machine CodeThings to know:
.text, _text, PROGSTART, $../asmonly.asm:8:22$:0xF800: 4031 0280 MOV.W #0x0280,SP0xF804: 40B2 5A80 0120 MOV.W #0x5a80,&Watchdog_Timer_WDTCTL0xF80A: D3D2 0022 BIS.B #1,&Port 1 2 P1DIR0xF80A: D3D2 0022 BIS.B #1,&Port_1_2_P1DIR
Mainloop:0xF80E: E3D2 0021 XOR.B #1,&Port_1_2_P1OUT0xF812: 403F 5000 MOV.W #0x5000,R15
L1:0xF816: 831F DEC.W R150xF818: 23FE JNE (L1)0xF81A: 3FF9 JMP (Mainloop)
ASIDE (4): Machine CodeASIDE (4): Machine CodeL1:0xF816: 831F DEC.W R150xF818: 23FE JNE (L1)
Opcode C OFFSET
0x23FE= 0 0 1 0 0 0 1 1 1 1 1 1 1 1 1 0
Offset = 11 1111 1110binary = ‐2decimal.
PCnew = PCold + 2 + PCoffsetx2 = 0xf818 + 2 + (‐2)x2PCnew PCold 2 PCoffsetx2 0xf818 2 ( 2)x2= 0xf818‐2 = 0xf816
We could do the same for 0xF81A: 3FF9 JMP (Mainloop)We could do the same for 0xF81A: 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
Resources UsedResources 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 (Example code MSP430X2XX) • 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)