lecture 8:lecture 8: assembly...
TRANSCRIPT
Lecture 8:Lecture 8:Assembly Languageg g
Today’s Topics
R i th t f i d i t ( l t )• Review the concept of memories and registers (accumulators)
• Generating machine code manually. You are expected to convert assembly code lines to machine
codes.
Fil d i t d ith ti bl• Files and processes associated with converting assembly source code to machine code
T l bl di i• To learn assembler directives
A Short Story
W ld i i i• World is converging again. No pure EE, CE, and CS.
F d t l l i t t• Fundamentals are always important.
• A short story about hiring interviews. Very simple but fundamental questions are asked even to Senior
engineers.
G d GPA i i t t b t• Grade or GPA is important but…
• Last but not least, you are expected to study outside classroom. Lectures cannot cover all topics.
Microcontrollers (or Microcomputers)
Mi t ll
Basic ideas
• Microcontroller CPU core + I/O ports + Memories (RAM and ROM) + …
M i• Memories We only use RAM area to learn assembly language and test
programs. No need to worry about burning your program into ROM.
• Registers (accumulators) vs. memoriesg Registers are small read/write memory cells inside CPU core. Memories are located outside CPU. To get a value from a location in a memory the value should To get a value from a location in a memory, the value should
travel through data bus. (Remember memory modules are separated from CPU core)
• This takes time (much longer than getting from/setting to Registers)• This takes time (much longer than getting from/setting to Registers)
Microcontrollers (or Microcomputers)Load and Store / Move / Transfer
MemoryCPU Core
Register ALoad
Register BStore
TransferTransfer
MoveMove
Before arithmetic operations including comparisons, the microcontroller
l d hrequires a value on a register to do the operations.
Microcontrollers (or Microcomputers)Load and Store / Move / Transfer
MemoryCPU Core
Register ARegister ALoad
Register BRegister BStore
TransferTransfer
As a programmer, you may think yourself as a master of a small world. You can do whatever you want as long as the environment supports for you.
MoveMoveWhen it comes to the environment, you have two main tools for now (bunch of other things will come soon); a storage to save data; and a process unit to manipulate the data/to
Before arithmetic operations including comparisons, the microcontroller
l d h
conduct arithmetic operations and logical decisions.
To control the processor and manipulate the memory, we write programs.
requires a value on a register to do the operations.
Code Line and Program Counter
Wh C d Li i L b i t i d• When we say Code Line in Lab assignments, quizzes, and exams, the instruction line is completed (executed). So registers are supposed to be affected by the execution.
• Program Counter always points the NEXT instruction!! Caution on Branches. PC depends on whether the branch is taken
or not.
• Example:
Trace Line PC A B X N Z V C
1 1 1503 ‐ ‐ 2000 0 0 0 ‐
2 2 1508 ‐ ‐ 2000 0 0 0 ‐
3 3 150A ‐ 02 2000 0 0 0 ‐
4 4 150C ‐ 02 2000 0 0 0 ‐
5 5 150E 40 02 2000 0 0 0 ‐
1: 1: 1500 1500 CE 2000 LDX #$2000 CE 2000 LDX #$2000 2: 2: 1503 1503 180B FF 1000 MOVB #$FF,$1000180B FF 1000 MOVB #$FF,$10003: 3: 1508 1508 C6 02 LDAB #2C6 02 LDAB #24:4: 150A150A 27 0E27 0E BEQ 14BEQ 14
6 6 1511 40 02 2000 0 0 0 1
7 7 1513 40 02 2000 0 0 0 1
8 8 1516 40 02 2000 0 0 0 1
4: 4: 150A 150A 27 0E 27 0E BEQ 14BEQ 145: 5: 150C 150C A6 00 LDAA 0,XA6 00 LDAA 0,X6: 6: 150E 150E B1 1000 CMPA $1000B1 1000 CMPA $10007: 7: 1511 1511 24 03 BHS 3 24 03 BHS 3 8: 8: 1513 1513 7A 1000 STAA $10007A 1000 STAA $10009:9: 15161516 08 INX08 INX
9 9 1517 40 02 2001 0 0 0 1
10 10 1518 40 01 2001 0 0 0 1
9: 9: 1516 1516 08 INX08 INX10: 10: 1517 1517 53 DECB53 DECB11: 11: 1518 1518 20 F0 BRA 20 F0 BRA ‐‐161612: 12: 151A 151A 3F SWI3F SWI
Machine CodeManually generate machine code*
Add M hi C d S C dAddress Machine Code Source Code1500 LDX #$2000
MOVB #$FF, $10001500 CE 20001503 180B FF 1000 MOVB #$FF, $1000
LDAB #2BEQ 14
1503 180B FF 10001508 C6 02150A 27 0E
LDAA 0,XCMPA $1000BHS 3
150C A6 00150E B1 10001511 24 03 BHS 3
STAA $1000INX
1511 24 031513 7A 10001516 08 INX
DECBBRA -16
1516 081517 531518 20 F0
SWI151A 3F
Assembly ProcessGeneral case
Assembly source code: ASCII text fileyprog.asm .asm extension
Assembler Cross-assembler:Translate assembly code into object code.
Object file:prog obj
Listing file:prog lst
Object code:Mix of machine code and additional information
Listing file:d bl l f l h lprog.obj prog.lst Human readable log file containing the original
assembly and the machine code.
LinkerLinker:Combine multiple object files into a single piece
Machine
Linker Combine multiple object files into a single piece of machine code
Machine code file Loader Microcontroller
Assembly ProcessIn the lab
Assembly source code: ASCII text fileyprog.asm .asm extension
Assembler Cross-assembler:Translate assembly code into object code.
Listing file:prog lst
S19 file:prog s19
Listing file:Human readable log file containing the original assembly and the machine code.
prog.lstprog.s19S19 file: Motorola S-record format An ASCII encoding for binary data.http://en.wikipedia.org/wiki/S19_(file_format)
Loader Loader:D-Bug12 is used to load .s19 file to the memory of the microcontroller. Load instruction is used.
Microcontroller
Proper Assembly Code
1 Separate the source code into constant section data and• 1. Separate the source code into constant section, data and variable sections, and code section.
• 2 Do not use numbers within the code2. Do not use numbers within the code Except for possibly 0 or 1 in obvious situation
• Always begin with a comment block statingy g g Purpose of the program Inputs Outputsp Programmer Anything else useful
• Comment within the code
• Assume that a reader understands the processor’s assembly code so do not use comments to simply rephrase the assemblycode, so do not use comments to simply rephrase the assembly code.
Assembly Language Syntax
Th bl l i t f li f t t i th f• The assembly language consists of lines of text in the form: [label:] [command] [operand(s)] [;comment]
or ; comment
where ‘:’ indicates the end of label and ‘;’ defines the start of a where : indicates the end of label and ; defines the start of a comment. The command field may be an instruction, a directive or a macro call.
Assembler Directives
• Memory allocation
Only small fractions…
• Memory allocation org: puts an address into the assembler location counter
• org $1000 ds.[size] (Define Storage)
; allocate specified number of storage spaces; allocate specified number of storage spacesds.b (Define Storage Bytes)ds.w (Define Storage Words)
• buffer ds.b 100• dbuffer ds.w 100dbuffer ds.w 100
• Data formation dc[.size] (Define Constant)
; allocate and initialize storage for variables; allocate and initialize storage for variablesdc.b (Define Constant Byte)dc.w (Define Constant Word)
• array dc.b $11, $12,$22• Initialize a 3-byte constant with the datay
• Note: need to know a difference between buffer ds.b $10
• allocate 16 byte memory space for ‘buffer’allocate 16 byte memory space for buffer buffer dc.b $10
• allocate 1 byte for ‘buffer’ and the value of ‘buffer’ will be initialized to $10
Assembler Directives
• Symbol Definition• Symbol Definition equ
• TRUE equ $FF
• Numbers $xx or xxh: hexadecimal %xxxx.. : binary Otherwise: decimal
Some More Instructions
L d ff ti dd i t ti
Load Effective Address Instructions
• Load effective address instructions LEAX: Load effective address into X
• LEAX 10,X LEAY: Load effective address into Y
• LEAY B, Y LEAS: Load effective address into SPLEAS: Load effective address into SP
• LEAS 0,PC
• Can you tell a difference between “LEAX 10,X” and “LDX 10,X”?Can you tell a difference between LEAX 10,X and LDX 10,X ? Assuming (X) = 1200, the content at 120A is 34h, and at 120B is
56h• “LEAX 10 X” makes X be 120A• LEAX 10,X makes X be 120A• “LDX 10,X” makes X be the content at memory (120A+120B) which is
3456.
Some More Instructions
• 8 bit addition
Addition and Subtraction
• 8 bit addition ABA: (A) + (B) A; Note that there is no AAB instruction! ADDA: (A) + (M) A
• ADDA $1000ADDB (B) (M) B ADDB: (B) + (M) B
• ADDB #10 ADCA: (A) + (M) + C A ADCB: (B) + (M) + C B
• 8 bit subtraction SBA: (A) – (B) A; Subtract B from A (Note: not SAB instruction!) SUBA: (A) – (M) A; Subtract M from ASUBA: (A) (M) A; Subtract M from A SUBB: (B) – (M) B SBCA: (A) – (M) – C A SBCB: (B) – (M) – C B There is a pattern that make you
be easy to remember the• 16 bit addition and subtraction
ADDD: (A:B) + (M:M+1) A:B SUBD: (A:B) – (M:M+1) A:B
ABX (B) (X) X
be easy to remember the instructions!!!
1. The last letter in these instructions is the destination!
ABX: (B) + (X) X ABY: (B) + (Y) Y 2. Also it comes to the first in the
operation
Some More Instructions
• Increments
Increments, Decrements, and Negate
• Increments INC: (M) + 1 M INCA: (A) + 1 A INCB
Note that we don’t have IND and DED!
INS INX INY
• Decrements DEC DECA DECBDECB DES DEX DEY
• Negate NEG: negate a memory byte NEGA NEGB
Assembly Code Example
W it t t bl f b t l
Requirements
• Write a program to copy a table of one-byte values.
• Our table will be defined by a starting address, supplied at $1000 d b b t b f l t i th t bl$1000, and by a one-byte number of elements in the table, supplied at $1002.
Th bl ill b i d i di f h i i l• The table will be copied a given distance from the original table, and this two-byte offset will be supplied at address $1003.
Just one example
1: =00001000 ORG $10002: 1000 +0002 table ds.w 13 1002 +0001 l th d b 1
……
10001000 20
10011001 003: 1002 +0001 length ds.b 14: 1003 +0002 offset ds.w 15: 6: =00001800 ORG $18007: 1800 FE 1000 LDX table
10021002 40
10031003 05
10041004 008: 1803 B7 54 TFR X,D9: 1805 F3 1003 ADDD offset
10: 1808 B7 46 TFR D,Y11: 180A F6 1002 LDAB length12: 180D 27 09 loop BEQ done
……
20002000 12
20012001 3412: 180D 27 09 loop BEQ done13: 180F 180A 00 40 MOVB 0,X,0,Y14: 1813 08 INX15: 1814 02 INY16: 1815 53 DECB17: 1816 20 F5 BRA loop
20022002 56
20032003 78
20042004 5517: 1816 20 F5 BRA loop18: 1818 3F done SWI
Symbols:done *00001818length *00001002
20042004 55
……
25002500
25012501length 00001002loop *0000180doffset *00001003table *00001000
25012501
25022502
25032503
25042504
……
Modification of the Example
Wh t h i d t h dl t bl f t b t• What changes are required to handle a table of two-byte numbers? Need to copy two bytes instead of one byte.
• What changes are required to handle a two-byte length? The length should represent two-byte numbers!g p y
1: =00001000 ORG $10002: 1000 +0002 table ds.w 13: 1002 +0001 length ds.bds.b 14: 1003 +0002 offset ds.w 15:
length dds.ws.w 1
5: 6: =00001800 ORG $18007: 1800 FE 1000 LDX table8: 1803 B7 54 TFR X,D9: 1805 F3 1003 ADDD offset0 808 610: 1808 B7 46 TFR D,Y
11: 180A F6 1002 LDAB LDAB lengthlength12: 180D 27 09 loop BEQ done13: 180F 180A 00 40 MOVBMOVB 0,X,0,Y14: 1813 08 INX
MOVWMOVW 0,X,0,Y
LDD LDD lengthlength
Add dditi l INX15: 1814 02 INY16: 1815 53 DECB17: 1816 20 F5 BRA loop18: 1818 3F done SWI
Add additional INXinstruction
Add additional INYinstruction
Symbols:done *00001818length *00001002loop *0000180dff t *00001003offset *00001003
table *00001000
SUBD #1
Questions?
Wrap-up
R i t d i
What we’ve learned
• Registers and memories
• Generating machine code manually.
• Concept of assembly language
• Assembler directives• Assembler directives
What to Come
Fl h t• Flowcharts
• Some assembly programming examples