E91 (3) Assembly Language

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

E91

Assembly Language
The basics
Anatomy of a simple program
Anything after “;” is comment Make PROGSTART available to linker.

Share header file with “C” ;********************************************************************


Note: not in column 1. ; MSP-FET430F2013 Demo - Software Toggle P1.0
;********************************************************************
“.text” predefined to be start .cdecls C,LIST, "msp430x20x3.h"
of FLASH (0xf800 in 430F2013) .global PROGSTART
;
;--------------------------------------------------------------------
“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
“bis” (bit set): bis src, dst mov.w #050000,R15 ; Delay to R15
L1 dec.w R15 ; Decrement R15
“MainLoop” and “L1” are labels jnz L1 ; Delay over?

“xor”: xor src, dst jmp Mainloop ; Again

R15 is a “register” ;--------------------------------------------------------------------


; Interrupt Vectors
“d ” (decrement):
“dec” (d t) dec
d dst
d t ;--------------------------------------------------------------------
.sect ".reset" ; RESET Vector
Check to see if last math result .short PROGSTART ;
was 0. Jump if Not Zero: .end
jnz label
JuMP back to main loop:
T ll assembler
Tell bl this
hi is
i the
h end
d
jmp label

“.reset” is defined in header file


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 #0280h,SP ; Initialize stackpointer
mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop WDT

mov.w &myWord,myWordVar1 Data in flash accessed either by 


mov.w &myWord,&myWordVar1
mov.w &myWord,&myWordVar2 “Absolute” (& prefix) or Symbolic

mov.b #33h, &myByteVar1 Data in RAM accessed either by 


mov.b #33h, &myByteVar2 “ b l ” (&
“Absolute” (& prefix) or Symbolic
f ) b l
mov.b #33h, &myByteVar3
mov.b #33h, &myByteVar4 Immediate  Operands
(Source only)
mov.w myWord, myBytes

; Define data in flash (immediately after code) Register  Operands


myBytes .byte 05ah, 03ch
myWord .word 0f2h

; 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
0xF80A: 4290 F83C 09F2 MOV.W &myWord,myWordVar1
mov.w &myWord, myWordVar1 0xF810: 4292 F83C 0200 MOV.W &myWord,&myWordVar1
mov w &myWord, &myWordVar1
mov.w 0xF816: 4292 F83C 0202 MOV.W
MOV W &myWord
&myWord,&myWordVar2
&myWordVar2
mov.w &myWord, &myWordVar2
0xF81C: 40F2 0033 0204 MOV.B #0x0033,&myByteVar1
mov.b #33h, &myByteVar1 0xF822: 40F2 0033 0205 MOV.B #0x0033,&myByteVar2
mov.b #33h, &myByteVar2 0xF828: 40F2 0033 0206 MOV.B #0x0033,&myByteVar3
mov.b #33h, &myByteVar3 0xF82E: 40F2 0033 0208 MOV.B #0x0033,&myByteVar4
y y
mov.b #33h, &myByteVar4
0xF834: 4090 0006 0002 MOV.W myWord,myBytes
mov.w myWord, myBytes
Ruh‐Roh!
; Define data in flash (after code)
myBytes
B t .byte
b t 05ah,
05 h 03ch
03 h myBytes:
B t
myWord .word 0f2h 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 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: 831F DEC.W R15
0xF818: 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 = 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


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)

You might also like