Assembler - BYU Computer Science Students Homepage Index

Download Report

Transcript Assembler - BYU Computer Science Students Homepage Index

S06: Assembler / Linker
Required:
PM: Ch 7, pgs 81-107
Assembler Directives
Recommended:
MSP430 Assembly Tutorial
MSP430 Disassembly.docx
FUG: 3.4
CS 224
Chapter
Lab
Homework
L01: Data Types
L02: FSM
HW01
HW02
L03: Blinky
L04: Microarch
L05b: Traffic Light
L06a: Morse Code
HW03
HW04
HW05
HW06
L07b: Morse II
L08a: Life
L09b: Snake
L10a: Threads
HW07
HW08
HW09
HW10
S00: Introduction
Unit 1: Digital Logic
S01: Data Types
S02: Digital Logic
Unit 2: ISA
S03: ISA
S04: Microarchitecture
S05: Stacks / Interrupts
S06: Assembly
Unit 3: C
S07: C Language
S08: Pointers
S09: Structs
S10: Threads
S11: I/O
BYU CS 224
Assembler / Linker
2
Learning Objectives…
Learning Outcomes
After completing this section, you should be
able to

Explain the difference between a low level and
high level language.

Justify the study/use of assembly code.

Contrast assembler directives with assembler
code.

Describe the assembly/linker process.

Contrast a library with a computer program.

Describe program sections and explain how they
are used by the linker to create an executable.

Give examples of emulated and intrinsic
instructions.

Use systematic decomposition to create an
assembly program.
BYU CS 224
Assembler / Linker
Topics




High Level vs. Assembly
Assembly Instructions
Emulated Instructions
Assembler





Linker




Assembly Code
Assembly Sections
Assembly Process
Assembly Directives
Libraries
Linking multiple files
How to Code in Assembly
Systematic Decomposition
Lab 6a: Morse Code
3
High Level vs. Assembly
High Level vs. Assembly

High Level Languages





More programmer friendly – easy to learn, easy to use, easy to
understand
More ISA independent
Cross-platform compatible (portable)
Each high-level statement translates to several instructions in the
ISA of the computer
Assembly Languages






Lower level, ISA dependent
Fewer data types – no distinction between program and data
No programming restrictions
Each instruction specifies a single ISA instruction
Makes low level programming more user friendly
More efficient code
BYU CS 224
Assembler / Linker
4
High Level vs. Assembly
Why Assembly Code?




Allows us to work at a higher level than machine
language (often called glorified machine code).
Being closer to ISA may allow us to write more efficient
code.
No programming restrictions (lots of rope).
Allows us to use symbolic names for opcodes and
memory locations.






add, call, push,…
SUM, PRODUCT
Don’t need to know every address of every storage location.
Helps to allocate memory locations.
Provides additional error checking.
Calculates addresses for us – really a big deal!
BYU CS 224
Assembler / Linker
5
Assembly Instructions
Double Operand Instructions
Double Operand
Mnemonic
Operation
Description
ADD(.B or .W) src,dst
src+dstdst
Add source to destination
ADDC(.B or .W) src,dst
src+dst+Cdst
Add source and carry to destination
DADD(.B or .W) src,dst
src+dst+Cdst (dec)
Decimal add source and carry to destination
SUB(.B or .W) src,dst
dst+.not.src+1dst
Subtract source from destination
SUBC(.B or .W) src,dst
dst+.not.src+Cdst
Subtract source and carry from destination
Arithmetic instructions
Logical and register control instructions
AND(.B or .W) src,dst
src.and.dstdst
AND source with destination
BIC(.B or .W) src,dst
.not.src.and.dstdst
Clear bits in destination
BIS(.B or .W) src,dst
src.or.dstdst
OR (set) bits in destination
BIT(.B or .W) src,dst
src.and.dst
Test bits in destination
XOR(.B or .W) src,dst
src.xor.dstdst
XOR source with destination
CMP(.B or .W) src,dst
dst-src
Compare source to destination
MOV(.B or .W) src,dst
srcdst
Move source to destination
Data instructions
BYU CS 224
Assembler / Linker
7
Double Operand Instructions
Single Operand
Mnemonic
Operation
Description
RRC(.B or .W) src
C  MSB  MSB−1 .... LSB+1  LSB  C
Rotate right thru carry
SWPB(.W) src
Bits 15 to 8 <−> bits 7 to 0
Swap bytes
RRA(.B or .W) src
MSB  MSB, MSB  MSB−1, ... LSB+1 
LSB, LSB  C
Rotate right arithmetic
SXT(.W) src
Bit 7 → Bit 8 ......... Bit 15
Sign extend byte
PUSH(.B or .W) src
SP-2SP, src@SP
Push byte/word source on stack
CALL(.W) dst
dsttmp ,SP-2SP,
PC@SP, tmpPC
Subroutine call to destination
RETI
TOSSR, SP+2SP
TOSPC, SP+2SP
Return from interrupt
BYU CS 224
Assembler / Linker
8
Double Operand Instructions
Relative Jump Instructions
PC-relative jumps, adding twice the sign-extended offset to
the PC, for a jump range of -1024 to +1022.
Mnemonic
Operation
Description
JNZ/JNE
Jump if Z == 0
Jump if not equal (if !=)
JZ/JEQ
Jump if Z == 1
Jump if equal (if ==)
JNC/JLO
Jump if C == 0
Jump if carry (if unsigned <)
JC/JHS
Jump if C == 1
Jump if no carry (if unsigned >=)
JN
Jump if N == 1
Jump if negative (No JP)
JGE
Jump if N == V
Jump if zero or positive (if signed >=)
JL
Jump if N != V
Jump if less than (if signed <)
JMP
Jump
Jump unconditionally
BYU CS 224
Assembler / Linker
9
Emulated Instructions
Emulated Instructions




In addition to the 27 instructions defined by the
MSP 430 ISA, there are 24 additional emulated
instructions
The emulated instructions make reading and
writing code more easy, but do not have their
own op-codes
Emulated instructions are replaced automatically
by native MSP 430 instructions
There are no penalties for using emulated
instructions.
BYU CS 224
Assembler / Linker
10
Emulated Instructions
Emulated Instructions
Mnemonic
Operation
Emulation
Description
Arithmetic instructions
ADC(.B or .W) dst
dst+Cdst
ADDC(.B or .W) #0,dst
Add carry to destination
DADC(.B or .W) dst
dst+Cdst (decimally)
DADD(.B or .W) #0,dst
Decimal add carry to destination
DEC(.B or .W) dst
dst-1dst
SUB(.B or .W) #1,dst
Decrement destination
DECD(.B or .W) dst
dst-2dst
SUB(.B or .W) #2,dst
Decrement destination twice
INC(.B or .W) dst
dst+1dst
ADD(.B or .W) #1,dst
Increment destination
INCD(.B or .W) dst
dst+2dst
ADD(.B or .W) #2,dst
Increment destination twice
SBC(.B or .W) dst
dst+0FFFFh+Cdst
dst+0FFhdst
SUBC(.B or .W) #0,dst
Subtract source and borrow
/.NOT. carry from dest.
BR dst
dstPC
MOV dst,PC
Branch to destination
DINT
0GIE
BIC #8,SR
Disable (general) interrupts
EINT
1GIE
BIS #8,SR
Enable (general) interrupts
NOP
None
MOV R3,R3
No operation
RET
@SPPC
SP+2SP
MOV @SP+,PC
Return from subroutine
Program flow control
BYU CS 224
Assembler / Linker
11
Emulated Instructions
Emulated Instructions
Mnemonic
Operation
Emulation
Description
Program flow control
INV(.B or .W) dst
.NOT.dstdst
XOR(.B or .W) #0(FF)FFh,dst
Invert bits in destination
RLA(.B or .W) dst
CMSBMSB-1
LSB+1LSB0
ADD(.B or .W) dst,dst
Rotate left arithmetically
RLC(.B or .W) dst
CMSBMSB-1
LSB+1LSBC
ADDC(.B or .W) dst,dst
Rotate left through carry
NOP
None
MOV R3,R3
No operation
Data instructions
CLR(.B or .W) dst
0dst
MOV(.B or .W) #0,dst
Clear destination
CLRC
0C
BIC #1,SR
Clear carry flag
CLRN
0N
BIC #4,SR
Clear negative flag
CLRZ
0Z
BIC #2,SR
Clear zero flag
POP(.B or .W) dst
@SPtemp
SP+2SP
tempdst
MOV(.B or .W) @SP+,dst
Pop byte/word from stack to
destination
SETC
1C
BIS #1,SR
Set carry flag
SETN
1N
BIS #4,SR
Set negative flag
SETZ
1Z
BIS #2,SR
Set zero flag
TST(.B or .W) dst
dst + 0FFFFh + 1
dst + 0FFh + 1
CMP(.B or .W) #0,dst
Test destination
BYU CS 224
Assembler / Linker
12
How to Code Assembler
Coding Assembler
How To Code Assembler…


Understand the problem (obviously)
Until you are comfortable in assembly, (and
even afterwards), write out your solution in
something familiar





English
Flowchart
Pseudo-code
Java, C, Ruby – the pseudo-code doesn’t really
matter!
Then, translate to assembler
BYU CS 224
Assembler / Linker
14
Coding Assembler
Three Basic Constructs
Task
True
False
Test
condition
Subtask 1
Test
condition
False
True
Subtask 1
Subtask 2
Subtask 2
Sequential
BYU CS 224
Subtask
Conditional
Assembler / Linker
Iterative
15
Coding Assembler
if-then-else

if-then-else
cmp.w
jne
xor.b
bis.b
jmp
#1,buzzerON
myElse
#0x20,&P4OUT
#0x02,&P1OUT
myNext
myElse:
bic.b
#0x02,&P1OUT
myNext:
BYU CS 224
; if (buzzerON == 1)
;{
;
pulse_buzzer();
;
turn_on_LED();
;}
else
;{
;
turn_off_LED();
;}
;
Assembler / Linker
16
Coding Assembler
switch / case

switch / case
cmp.w
jne
call
jmp
#DOT,myByte
sw_01
#do_dot
sw_end
; switch (myByte)
;{
; case DOT:
;
do_dot();
break;
#DASH,myByte
default
#do_dash
sw_end
default:
; case DASH:
;
do_dash();
;
break;
;
; default:
;}
sw_end:
;
sw_01:
cmp.w
jne
call
jmp
BYU CS 224
Assembler / Linker
17
Coding Assembler
for-loop

for-loop
.bss
mov.w
for_ck: cmp.w
jge
call
call
call
call
add.w
jmp
for_done:
BYU CS 224
i,2
; int i;
#0,i
#10,i
for_done
#do_dot
#delay
#do_dash
#delay
#1,i
for_ck
; for(i=0; i<10; i++)
;{
;
; do_dot();
; delay();
; do_dash();
; delay();
;
;}
;
Assembler / Linker
18
Coding Assembler
while

while loop…
TRUE .equ
.bss
mov.w
while_loop:
cmp.w
jeq
call
call
call
call
jmp
while_done:
BYU CS 224
1
blink,2
#TRUE,blink
#0,blink
while_done
#LED_ON
#delay
#LED_OFF
#delay
while_loop
;
;
;
;
;
;
;
;
;
;
#define TRUE 1
int blink = TRUE;
while (blink)
{
LED_ON();
delay();
LED_OFF();
delay();
}
;
Assembler / Linker
19
Exercise 6.1
1. Code the following C program in assembler:
int i;
void func1(void) { ++i; return; }
void func2(void) { i += 2; return; }
void main(void)
{
for (i = 1; i < 10; i++)
{
if (i < 5)
{
func1();
}
else
{
func2();
}
}
}
BYU CS 224
Assembler / Linker
20
Exercise 6.1 (solution)
1. Code the following C program in assembler:
int i;
void func1(void) { ++i; return; }
void func2(void) { i += 2; return; }
void main(void)
{
for (i = 1; i < 10; i++)
{
if (i < 5)
{
func1();
}
else
{
func2();
}
}
}
BYU CS 224
Assembler / Linker
.bss
i,2
func1:
add.w
ret
#1,i
func2:
add.w
ret
#2,i
main:
mov.w
#1,i
main$1: cmp.w
jge
cmp.w
jge
call
jmp
#10,i
main$4
#5,i
main$2
#func1
main$3
main$2: call
#func2
main$3: add.w
jmp
#1,i
main$1
main$4: ret
21
Systematic Decomposition
Systematic Decomposition
IDEA
Step by Step Procedure

Finiteness


Definiteness


Each step is precisely
stated.
Effective Computability

BYU CS 224
Must terminate.
Each step can be carried
out.
Assembler / Linker
22
Systematic Decomposition
Stepwise Refinement


Also known as systematic decomposition.
Start with problem statement:
“Write an assembler program to play the game of
Simon using the LEDs and push button switches.”



Decompose task into a few simpler subtasks.
Decompose each subtask into smaller subtasks,
and these into even smaller subtasks, etc....
until you get to the machine instruction level.
Incrementally develop program and test, test,
test…
BYU CS 224
Assembler / Linker
23
Systematic Decomposition
Problem Statement

Because problem statements are written in
English, they are sometimes ambiguous and/or
incomplete.


How is the game played? How many LEDs start the
game? Which switches and which LEDs? How long is
an LED on or off? What happens when an error is
made? What happens when a sequence is
successfully reproduced? How is a new sequence
started? …
How do you resolve these issues?


Ask the person who wants the problem solved, or
Make a decision and document it.
BYU CS 224
Assembler / Linker
24
Systematic Decomposition
Simon Example
Problem
Algorithm
Init Simon board.
Setup new game.
“Play the
game of
Simon
using the
LEDs and
push
button
switches.”
BYU CS 224
Output random
sequence of tones
and LEDs.
Reset sequence.
Get and compare
player’s response.
Output results and
restart game
w/new sequence.
Pseudo-code
Incremental Development
start:
while (1)
call
{ new_game:
{ saveRandSeed;
newGame:
success = TRUE;
call
trys = TRYS-1;
call
}
mov.b
while(success)
mov.w
{ doSequence:
{ restoreRandSeed;
tryLoop:
trys++;
tst.b
for (i=0; i<trys; i++)
jeq
{ getRand;
inc.w
doLEDsTone;
}
test:
}
mov.w
doPlayer:
{ restoreRandSeed;
for (i=0; i<trys; i++) testLoop:
cmp.w
{ getSwitch;
jge
doLEDsTone;
call
if (getRand  switch)
and.w
{ success = FALSE;
call
break;
inc.w
}
jmp
}
}
player:
doResults:
call
{ if (success) outSuccess;
...
else outRaspberry;
}
}
}
Assembler / Linker
#init_board
#new_game
#saveRandSeed
#0xff,&success
#TRYS-1,&trys
&success
newGame
&trys
#0,r15
r15,&trys
player
#getRand
#0x0003,r12
#doLEDsTone
r15
testLoop:
#restoreRandSeed
25
Assembler
MSP430 Assembler
Assembler
An assembler outputs
an object file
An assembler
translates a program
into machine code
An assembly program is a text file
containing assembly instructions,
directives, macros, and comments
BYU CS 224
Assembler Symbol Table
input to a linker program
Assembler / Linker
27
MSP430 Assembler
Assembler Coding Format
Assembler directives
begin with a period (.)
The ".cdecls" directive inserts a
header file into your program.
;*************************************************************************
;
CS 224 Lab 1 - blinky.asm: Software Toggle P1.0
;
Description: Toggle P1.0 by xor'ing P1.0 inside of a software loop.
Labels start in ;
;*************************************************************************
column 1
Begin writing your
.equ
0
(case sensitive)DELAY
.cdecls C,"msp430.h"
; MSP430 assembly code after
the ".text"
directive. code
.text
; beginning
of executable
start:
mov.w
#0x0400,SP
; init stack pointer
mov.w
#WDTPW|WDTHOLD,&WDTCTL ; stop WDT
bis.b
#0x01,&P1DIR
; set P1.0 as output
Instructions are lower
mainloop:
xor.b
#0x01,&P1OUT
; toggle
P1.0are
case
and macros
mov.w
#DELAY,r15
;
use
R15
UPPER CASE. as delay counter
delayloop:
Label
BYU CS 224
sub.w
jnz
jmp
#1,r15
delayloop
mainloop
; delay over?
; n
; y, toggle led
.sect
.word
.end
".reset"
start
; MSP430 RESET Vector
The ".end" directive is the
; start address
last line of your program.
Operation
Operands
Assembler / Linker
Comments
28
Assembly Code
Symbols / Labels

Symbols








Name/value pairs
Stored in a symbol table.
Up to 200 alphanumeric characters (A-Z, a-z, 0-9, $, and _)
No embedded blanks
Case sensitive
First character cannot be a number.
A symbol value is a label, constant, or substitution value.
Labels





Labels are symbols.
Labels begins in column 1 and is optionally followed by a colon.
The value of a label is the current value of the Location Counter
(address within program).
A label on a line by itself is a valid statement.
Labels used locally within a file must be unique.
BYU CS 224
Assembler / Linker
29
Assembly Code
Mnemonics / Operands

Mnemonic Field


The mnemonic field cannot start in column 1; if it does, it is
interpreted as a label.
The mnemonic field contains one of the following items:





MSP430 instruction mnemonic (ie. ADD, MOV, JMP)
Assembler directive (ie. .data, .list, .equ)
Macro directive (ie. .macro, .var, .mexit)
Macro invocation
Operand Field



The operand field follows the mnemonic field and optionally
contains one or more operands.
An operand may consist of:
 Symbols
 Constants
 Expressions (combination of constants and symbols)
Operands are separated by commas
BYU CS 224
Assembler / Linker
30
Assembly Code
Constants / Expressions

Constants are maintained internally as a 32-bit, signed
(2’s complement) or unsigned numbers.



Constants are not sign extended.
The pound sign precedes a constant in an instruction
 Decimal: decimal digits ranging from -2147483648 to
4294967295 (ie, 1000, -32768)
 Hexadecimal: up to 8 hexadecimal digits followed by ‘H’ (or
‘h’) or preceded by ‘0x’ (ie, 78h, 0x78)
 Binary: up to 32 binary digits followed by suffix B (or b) (ie.
0000b, 11110000B)
An expression is a constant, a symbol, or a series of
constants and symbols separated by arithmetic
operators that evaluates to a single 32-bit number.


BYU CS 224
-2147483648 to 2147483647 for signed values
0 to 4294967295 for unsigned values
Assembler / Linker
31
Assembly Code
Expressions / Operators

The precedence order of expression evaluation is
1. Evaluate parenthesized expressions
2. Evaluate operators according to precedence groups
3. When parentheses and precedence groups do not determine the
order of expression evaluation, the expressions are evaluated from
left to right
Group
1
2
3
4
5
6
7
8
9
BYU CS 224
Operator
+, -, ~, !
*, /, %
+, <<, >>
<, <=, >, >=
=[=], !=
&
^
|
Description
Unary plus, minus, 1’s complement, logical NOT
Multiplication, Division, Modulo
Addition, Subtraction
Shift left, Shift right
Less than, Less than or equal to, Greater than, Greater than
or Equal to
Equal to, Not equal to
Bitwise AND
Bitwise exclusive OR (XOR)
Bitwise OR
Assembler / Linker
32
Assembly Code
Assembler Directives

Assembly directives are used to:




Create symbol table entries (.equ, .set, .cdecls).
Select assembler sections (.sect, .bss, .text).
Define values for memory locations (.byte, .word, .string).
Specify the end of program (.end).
;*******************************************************************************
;
CS/ECEn 124 Example Code
;*******************************************************************************
.cdecls
C,"msp430x22x4.h"
; include C header
COUNT
Directives
.equ
;-----------------------------------------------------------------------------.bss
cnt,2
; ISR counter
;-----------------------------------------------------------------------------.text
; Program reset
start:
mov.w
#0x0400,SP
; Initialize stack pointer
mov.w
#WDT_MDLY_0_5,&WDTCTL
; Set Watchdog interval to ~0.5ms
bis.w
#LPM0+GIE,SR
; Enter LPM0 w/ interrupt
jmp
$
; Loop forever; interrupts do all
.sect
.word
.end
BYU CS 224
2000
".reset"
start
; MSP430 RESET Vector
; Power Up ISR
Current Location Counter
Assembler / Linker
33
Assembly Code
Assembly Style Guidelines





Provide a program header, with author’s name, date, etc.,
and purpose of program.
Start labels, opcode, operands, and comments in same
column for each line. (Unless entire line is a comment.)
Use comments to explain what each register does.
Labels, symbols are case sensitive.
Use meaningful symbolic names.
 Mixed upper and lower case for readability.
 ASCIItoBinary, InputRoutine, SaveR1


Provide comments between program sections.
Each line must fit on the page -- no wraparound or
truncations.
BYU CS 224
Assembler / Linker
34
Exercise 6.2
1. What is an expression?
2. What is the difference between a symbol and a label?
3. Can the name “add” be used as a label?
4. What is the difference between a directive and a
mnemonic?
BYU CS 224
Assembler / Linker
35
Exercise 6.2 (answers)
1. What is an expression?
A combination of values, constants, variables, operators, and
functions that are interpreted according to the particular rules of
precedence and of association that produces another value.
2. What is the difference between a symbol and a label?
A label is a symbol whose value is an address (offset). A symbol
may or may not be a label, but does have a value (if defined).
3. Can the name “add” be used as a label?
Yes (but definitely NOT recommended!)
4. What is the difference between a directive and a
mnemonic?
A directive tells the assembler to do something. A mnemonic is a
symbolic representation of a computer instruction opcode.
BYU CS 224
Assembler / Linker
36
Assembler Sections
Assembler Sections
Assembler Sections
 A section is a block of code or data that occupies
contiguous space in the memory map.
 Each section has its own Location Counter.
 The assembler assembles into the current section.
 There are two types of sections:


Initialized sections containing data
or code (modal)
Object File

.sect
.bss var,2

.text
.usect "mySection"
Uninitialized sections reserving
space in the memory map for
.text
uninitialized data (temporary)

.bss
.sect "reset"

.usect
BYU CS 224
Assembler / Linker
Target Memory
RAM
ROM (Flash)
38
Assembler Sections
Location Counter

The Location Counter holds the relative memory
position of an instruction within the current section.




Each section has a location counter used to assign storage
addresses to your program's statements.
As the instructions of a source module are being assembled, the
location counter keeps track of the current location in storage.
A $ (dollar sign) can be used as an operand to an instruction to
refer to the current value of the location counter.
The assembler assembles into the current section.


An initialized section directive instructs the assembler to stop
assembling in the current section and begin assembling in the
indicated section (modal).
An uninitialized section directive does not end the current
section, but simply escape from the current section temporarily.
(Thus uninitialized directives .bss and .usect can appear
anywhere in an initialized section without affecting its contents.)
BYU CS 224
Assembler / Linker
39
Exercise 6.3
List the Location Counter values for the following:
start:
wdt_isr:
BYU CS 224
.cdecls
.bss
.bss
.text
mov.w
mov.w
mov.w
mov.b
bis.b
bis.w
.bss
xor.b
reti
.sect
.word
.sect
.word
.end
C,"msp430.h"
cnt,2
cat,4
#0x0400,SP
#0x5a18,&WDTCTL
#0x00fa,&cnt
#0x01,&IE1
#0x01,&P1DIR
#0x0018,SR
dog,2
#0x01,&P1OUT
".int10"
wdt_isr
".reset"
start
Assembler / Linker
; include c header
; WDT second counter
; program section
; set stack pointer
; set WD timer interval
; 1 sec WD counter
; enable WDT interrupt
; P1.0 output
; enable interrupts
; toggle P1.0
; return from interrupt
; WDT vector section
; Watchdog ISR
; PUC vector section
; RESET ISR
40
Exercise 6.3 (answers)
List the Location Counter values for the following:
0x0000
0x0002
0x0000
0x0004
0x000a
0x0010
0x0014
0x0018
0x0006
0x001c
0x0020
0x0000
0x0000
BYU CS 224
start:
wdt_isr:
.cdecls
.bss
.bss
.text
mov.w
mov.w
mov.w
mov.b
bis.b
bis.w
.bss
xor.b
reti
.sect
.word
.sect
.word
.end
C,"msp430.h"
cnt,2
cat,4
#0x0400,SP
#0x5a18,&WDTCTL
#0x00fa,&cnt
#0x01,&IE1
#0x01,&P1DIR
#0x0018,SR
dog,2
#0x01,&P1OUT
".int10"
wdt_isr
".reset"
start
Assembler / Linker
Memory
.reset
0xfffe
0xc000
0xfffe .reset
.int10
0xfff4
0xc01c
0xfff4 .int10
0xc020
reti
xor.b #01,&P1OUT
bis.w #0x0018,sr
bis.b #0x01,&P1DIR
mov.b #0x01,&IE1
mov.w #0x00fa,&cnt
mov.w #0x5a18,&WDTCTL
mov.w #0x0280,sp
wdt_isr 0xc01c
start
0xc018
0xc014
0xc010
0xc00a
0xc004
0xc000
dog
cat
cnt
0x0206
0x0202
0x0200
0xc000 .text
0x0200 .bss
41
Assembly Process
Assembly Process

The assembler translates 1-to-1 assembly language instructions
(.asm) into the machine language of the ISA (.obj)

1st Pass: store all labels/constants and their corresponding
addresses/values in the symbol table


Zero all Location Counters ($)
For each non-empty line in the .text section:




if line contains a label, add label and current LC to the symbol table
if line contains an instruction, increment the LC accordingly
Stop when .end directive is found.
2nd Pass: convert instructions to machine language, using information
from symbol table


Find the .text assembly directive and zero all Location Counters ($)
For each executable assembly language statement:





BYU CS 224
generate the corresponding machine language instruction
resolve labels referenced in instructions using the symbol table
increment LC for each instruction as in pass 1
output resulting machine code and program listing to output files
Stop when .end directive is found.
Assembler / Linker
42
Assembler Directives
Common Assembler Directives
Mnemonic and Syntax
.bss symbol, size in bytes[, alignment]
.sect "section name"
Reserves uninitialized
bytes in RAM section
Reserves size bytes in the .bss (uninitialized data) section
(Does NOT change
Assembles into a named (initialized) section
current section)
Description
.text
Assembles into the .text (executable code) section
.byte value1[, ..., valuen]
Initializes one or more successive bytes in the current section
.string "string1"[, ..., "stringn"]
Initializes one or more text strings
Changes
.word value1[, ... , valuen]
to new section
Initializes one or more 16-bit integers
(New Location Counter)
.align [size in bytes]
Aligns the LC on a boundary specified by size in bytes; must be a
power of 2; defaults to word (2 byte)
.def symbol1[, ... , symboln]
Identifies one or more symbols that are defined in current module
Fills (initializes) bytes
and that can be used in other modules
.include ["]filename["]
Includes source statements from another file
.ref symbol1[, ... , symboln]
Identifies one or more symbols used in the current module that are
defined in another module
symbol .equ value
Equates value with symbol
symbol .set value
Equates value with symbol
symbol
.cdecls [options,] "filename"
.end
BYU CS 224
in CURRENT section
(Does NOT change section)
Creates Name/Value
table entry
Share C headers between
C and
assembly
codeany
(Does
NOT
effect
Ends program
section Location Counter)
Assembler / Linker
43
Linker
Linker

The Linker program "links" two files together according
to their declared sections:
BYU CS 224
Assembler / Linker
44
Libraries
Library Routines

Library




A set of routines for a specific domain application.
Example: math, graphics, GUI, etc.
Use the .ref directive to reference symbols defined
outside a program.
Library routine invocation




Labels for the routines are defined as .def
Each library routine contains its own symbol table.
A linker resolves the external addresses before
creating the executable image.
Reports and unresolved symbols.
BYU CS 224
Assembler / Linker
45
Libraries
Linking Multiple Files
Source Module A
.ref myFunc
.ref sqrt
.text
…
call #myFunc
call #sqrt
…
.end
Source Module B
.def myFunc
.text
myFunc:
…
ret
.end
Module A
Object
Symbol
Table
Linker
Executable
Image
Module A
Object
Module B
Object
Module B
Object
Symbol
Table
Math
Library
Math
Library
Symbol
Table
BYU CS 224
Assembler / Linker
46
Exercise 6.4

Create assembler and linker symbol table values for the
following program: (Note: the linker loads the .text section
at memory address 0xc000.)
DELAY
Symbol
Name
BYU CS 224
Assembler Resolved
Symbol
Linker
Value
Value
.equ
.text
reset: mov.w
mov.w
bis.b
0
mloop: xor.b
mov.w
#0x01,&0x0021
#DELAY,r15
dloop: dec.w
jnz
r15
dloop
dlp2:
dec.w
jnz
jmp
r15
dlp2
mloop
.sect
.word
.end
".reset"
reset
Assembler / Linker
#0x0400,SP
#0x5a80,&0x0120
#0x01,&0x0022
47
Exercise 6.4 (answers)

Create assembler and linker symbol table values for the
following program: (Note: the linker loads the .text section
at memory address 0xc000.)
DELAY
Symbol
Name
DELAY
reset
mloop
dloop
dlp2
BYU CS 224
Assembler Resolved
Symbol
Linker
Value
Value
0x0000
0x0000
0x000E
0x0014
0x0018
0x0000
0xc000
0xc00E
0xc014
0xc018
.equ
.text
reset: mov.w
mov.w
bis.b
0
mloop: xor.b
mov.w
#0x01,&0x0021
#DELAY,r15
dloop: dec.w
jnz
r15
dloop
dlp2:
dec.w
jnz
jmp
r15
dlp2
mloop
.sect
.word
.end
".reset"
reset
Assembler / Linker
#0x0400,SP
#0x5a80,&0x0120
#0x01,&0x0022
48
Exercise 6.5
.cdecls C,LIST,"msp430x22x4.h"

.bss
RESET:

loop:
cnt,2
; WDT second counter
.text
mov.w
mov.w
mov.w
mov.b
bis.b
bis.w
#STACK,SP
#CTL,&WDTCL
#IPS,&cnt
#WDTIE,&IE1
#0x01,&P1DIR
#GIE,SR
; program section
; initialize stack pointer
; set WD timer interval
; init 1 sec WD counter
; enable WDT interrupt
; P1.0 output
; enable interrupts
;
jmp
<< add your code here >>
loop
; repeat message
WDT_ISR: xor.b
reti
.sect
.word
.sect
.word
.end

BYU CS 224





#0x01,&P1OUT
; toggle P1.0
; return from interrupt
".int10“
WDT_ISR
".reset“
RESET
; Watchdog Vector
; Watchdog ISR
; PUC Vector
; RESET ISR
Assembler / Linker
Memory
Interrupt Vector Table
Program Code
Stack
Data
16-bit Peripherals
0xFFFF
0xFFC0
0xFFBF
|
|
|
|
|
0x8000
0x05FF
|
|
|
0x0200
0x01FF
|
0x0100
8-bit Peripherals
0x00FF
|
0x0010
8-bit SFR’s
0x000F
|
0x0000
49
Exercise 6.5 (answers)
.cdecls C,LIST,"msp430x22x4.h"

.bss
RESET:

loop:
cnt,2
; WDT second counter
.text
mov.w
mov.w
mov.w
mov.b
bis.b
bis.w
#STACK,SP
#CTL,&WDTCL
#IPS,&cnt
#WDTIE,&IE1
#0x01,&P1DIR
#GIE,SR
; program section
; initialize stack pointer
; set WD timer interval
; init 1 sec WD counter
; enable WDT interrupt
; P1.0 output
; enable interrupts
;
jmp
<< add your code here >>
loop
; repeat message
WDT_ISR: xor.b
reti
.sect
.word
.sect
.word
.end

BYU CS 224





#0x01,&P1OUT
; toggle P1.0
; return from interrupt
".int10“
WDT_ISR
".reset“
RESET
; Watchdog Vector
; Watchdog ISR
; PUC Vector
; RESET ISR
Assembler / Linker
Memory
Interrupt Vector Table
Program Code
Stack
Data
16-bit Peripherals
0xFFFF
0xFFC0
0xFFBF
|
|
|
|
|
0x8000
0x05FF
|
|
|
0x0200
0x01FF
|
0x0100
8-bit Peripherals
0x00FF
|
0x0010
8-bit SFR’s
0x000F
|
0x0000
50
Morse Steps 1 & 2
1. Validate provided code:
a. Create an "Empty Assembly-only Project" for the MSP430F2274
using Code Composer Studio. (Delete main.asm if defined.)
b. Download the lab assembly files morse.asm and
morse_codes.asm. Add them to your lab project.
c. Assemble and run the program on your development board.
Verify that the letter 'A' (DOT DASH) is output by the speaker
before proceeding.
2. Modify the Watchdog ISR to:
a. Turn the red LED on during a tone and off when not pulse width
modulating (PWM) the speaker.
b. Add code to toggle the green LED on and off at one second
intervals. Test!
BYU CS 224
Assembler / Linker
51
Morse Lab: Step 3
3. Replace the "output DOT" lines of code with a call to a
subroutine doDOT and the "output DASH" lines of code
with a call to a subroutine doDASH. Write the
subroutines doDot and doDASH. Assemble and test.
;
output 'A' in morse code
loop:
mov.w
#ELEMENT,r15
call
#beep
mov.w
#ELEMENT,r15
call
#delay
;
output 'A' in morse code
loop:
call
#doDot
call
#doDash
. . .
doDot:
mov.w
call
mov.w
call
. . .
#ELEMENT*3,r15
#beep
#ELEMENT,r15
#delay
push
mov.w
call
mov.w
call
pop
ret
r15
#ELEMENT,r15
#beep
#ELEMENT,r15
#delay
r15
Remember, that all subroutines must observe a calleesave protocol. Save and restore all registers used by
the subroutine on the stack.
BYU CS 224
Assembler / Linker
52
Step 4
4. Re-write the main loop to access the message characters one at a
time using the indirect auto-increment source addressing mode
(@Rn+).
Use the indexed source addressing mode to index into the tables
of letter word pointers to get a pointer to the Morse Code element
bytes (xxxx(Rn)).
Compare code bytes with DOTs and DASHes and output the
corresponding Morse Code elements for each letter of the
message.
.ref
letters
; codes for A-Z
loop:
mov.w
#message,r4
; point to message
loop02:
mov.b
sub.b
...
add.w
mov.w
@r4+,r5
#'A',r5
; get character
; make 0-25
r5,r5
letters(r5),r5
; make word index
; get pointer to codes
mov.b
cmp.b
...
jmp
@r5+,r6
#DOT,r6
; get DOT, DASH, or END
; dot?
loop10:
BYU CS 224
loop10
Assembler / Linker
53
Step 4…
char message[ ] = "DOG";
message: .string
.byte
D O G 
"DOG"
0
char* letters[26];
letters:
Instructions
mov.w #message,r4
mov.b @r4+,r5
sub.b #'A',r5
add.w r5,r5
mov.w letters(r5),r5
mov.b @r5+,r6
BYU CS 224
r4
r5
r6
'D'
3
6
2
Assembler / Linker
...
1
2
2
2
1
1
2
1
1
1
0
1
0
1
2
1
1
1
0
0
0
2
1
0
54
Quiz 6.6
Given the memory contents to the right, what are the values for
registers r4 and r5 after each instruction to the left is executed?
Instructions
mov.w #tab1,r4
mov.b @r4+,r5
mov.w #3,r4
mov.b tab1(r4),r5
mov.w tab2(r4),r5
add.w r4,r4
mov.w tab2(r4),r5
mov.b @r5,r5
BYU CS 224
r4
0x8100
0x8101
3
r5
1
4
0x8101
6
0x8103
4
Address
Memory
0x8100
1
2
tab1: .byte 1,2
0x8102
3
4
.byte 3,4
0x8104
5
6
.byte 5,6
0x8106
7
8
.byte 7,8
0x8108
0x8100
tab2: .word tab1+0
0x810a
0x8101
.word tab1+1
0x810c
0x8102
.word tab1+2
0x810e
0x8103
.word tab1+3
0x8110
0x8104
.word tab1+4
0x8112
0x8105
.word tab1+5
0x8114
0x8106
.word tab1+6
0x8116
0x8107
.word tab1+7
The C Language
Assembly
55
BYU CS 224
Assembler / Linker
56