MCS-51 & Assembly language 17BSEG, CSEG, DSEG, ISEG, XSEG BSEG AT address defines an absolute BIT segment.. MCS-51 & Assembly language 21BIT, DATA, CODE, IDATA, XDATA symbol BIT bit_a
Trang 1MCS-51 & Assembly language 1
L p trình Assembly cho
MCS-51
Plan
¸ T p l nh MCS-51
¸ Ngôn ng Assembly s d ng ph n m m Keil
¸ Gi i thi u Ph n m m mô ph ng m ch đi n Proteus
¸ Ví d
Trang 2MCS-51 & Assembly language 3
T p l nh
¸ Vi x lý đ u có m t t p l nh
¸ Các l nh đ c đ c c t trong b nh ch ng trình
d i d ng các mã l nh nh phân
¸ thu n ti n cho vi c l p trình:
̈ Các mã l nh đ c quy c vi t d ng “g i nh ”
(mnemonic)
̈ C n ch ng trình d ch các mnemonic qua mã máy
(Assembler): H p ng
EDITOR PROGRAM
ASSEMBLER PROGRAM
LINKER PROGRAM
Myfile.asm
Myfile.obj
Other obj file Myfile.lst
Myfile.abs
Trang 3MCS-51 & Assembly language 5
M t s thu t ng
¸ Assembly:
̈ là m t cách th hi n các code l nh c a m t CPU d i d ng các ký
hi u mà con ng i có th d dàng đ c
¸ Assembler:
̈ ph n m m ch y trên máy tính có kh n ng d ch các file vi t b ng Assembly sang d ng object file
¸ Object code:
̈ Ch a các mã l nh d ng binary
¸ Object file:
̈ Object code
̈ Thông tin v “relocation” ph c v cho linhker
¸ Linhker
̈ Ch ng trình có kh n ng t p h p các object file đ t o thành m t ch ng trình duy nh t có th “executable”
¸ Segment
̈ Là m t đ n v b nh code ho c data.
̈ Có nhi u ki u segment trong m t ch ng trình Assembly
¸ Absolute segment: c đ nh không th thay đ i
¸ Relocatable segment: có các thu c tính đi kèm cho phép linker ghép nhi u segments l i v i nhau tr c khi t o ra m t ch ng trình executable
Trang 4MCS-51 & Assembly language 7
T p l nh MCS-51
¸ G m 255 l nh:
̈ 139 l nh 1 byte
̈ 92 l nh 2 byte
̈ 24 l nh 3 byte
¸ c chia thành các nhóm l nh
̈ S h c
̈ Logic
̈ Chuy n data
̈ Ki m tra đi u ki n (Boolean Instructions)
̈ i u khi n ch ng trình
Assembly cho MCS-51 (Standard)
¸ Keil cung c p 4 tools
̈ A51 Macro Assembly (\Keil\C51\Bin\A51.EXE)
̈ BL51 Linker/Locator (\Keil\C51\Bin\BL51.EXE)
̈ LIB51 Library Manager (\Keil\C51\Bin\LIB51.EXE)
̈ OH51 Object-Hex Converter (\Keil\C51\Bin\OH51.EXE)
Trang 5MCS-51 & Assembly language 9
Các b c t o
m t ch ng trình s d ng Keil ASM
PL/M – 51: là ngôn ng b c cao đ c Intel phát tri n vào nh ng n m 1980
¸ Các l nh (statements) ASM g m
̈ Các t khóa đi u khi n ch ng trình assembler (assembler control)
̈ Các t khóa đ nh h ng cho ch ng trình d ch (assembler directive)
̈ Các l nh máy (instruction)
¸ M i dòng ch có th ch a duy nh t m t l nh assembler
control ho c assembler directive ho c mnemonic
¸ M i l nh ph i vi t trên m t dòng
¸ Các tham bi n ho c các toán h ng c a các l nh đ c phân
bi t v i các l nh ít nh t b i m t space
¸ Ch ng trình assembly ph i đ c k t thúc b i asm directive END, các statements sau t khóa END s b b qua
Trang 6MCS-51 & Assembly language 11
$TITLE (Demo Program #1)
CSEG AT 0000h JMP $
END
L nh máy
[;comment] [, operand]
[, operand]
[operand]
mnemonic
[label:]
label: nhãn đ c dùng đ đánh d u đ a ch mà t i đó l nh đ c đ t mnemonic d ng g i nh c a l nh máy
operand toán h ng
comment l i chú thích
Trang 7MCS-51 & Assembly language 13
Directive
¸ nh h ng cho ch ng trình d ch ASM
̈ Cách x lý các l nh mnemonic trong ch ng trình
̈ nh ngh a các ký t , đ t tr c các ô nh ,
¸ Không sinh ra code l nh executable (ngo i tr m t s
directive DB và DW), bao g m các nhóm:
̈ Segment control
̈ Symbol definition
̈ Memory Initialization
̈ Program linkage
̈ Address control
Segment control
¸ Generic Segments
̈ Có tên và m t l p các thu c tính đi kèm
̈ Các Generic Segment có cùng tên các object file khác nhau s đ c ghép l i thành m t segment khi linhker
̈ Các t khóa: SEGMENT, RSEG
¸ Absolute Segments
̈ Dùng đ đ nh ngh a các vùng nh xác đ nh
̈ Các t khóa: CSEG, BSEG, DSEG, ISEG, XSEG
Trang 8MCS-51 & Assembly language 15
SEGMENT
segment SEGMENT class reloctype alloctype
segment tên c a segment
SEGMENT t khóa
class ch ra không gian các l p b nh (code, data, idata, xdata ) reloctype xác đ nh ho t đ ng relocation cho linker
allotype xác đ nh ho t đ ng allocation cho linker
RSEG
RSEG segment
segment tên c a generic segment đã đ c đ nh ngh a tr c đó b i
t khóa SEGMENT
RSEG c dùng đ L a ch n m t generic segment đã đ c đ nh ngh a b i
SEGMENT
Ví d :
MYPROG SEGMENT CODE ; declare a segment
RSEG MYPROG ; select the segment
Trang 9MCS-51 & Assembly language 17
BSEG, CSEG, DSEG, ISEG, XSEG
BSEG AT address defines an absolute BIT segment
CSEG AT address defines an absolute CODE segment
DSEG AT address defines an absolute DATA segment
ISEG AT address defines an absolute IDATA segment
XSEG AT address defines an absolute XDATA segment
Thông báo v i ch ng trình linker m t segment t i m t đ a ch đ nh tr c
inh ngh a các ký hi u (Symbol directive)
¸ Cho phép s d ng các ký hi u đ bi u di n thanh ghi đ a ch , các s
¸ Các ký hi u đã đ c đ nh ngh a s không đ c đ nh ngh a l i, ngo i tr l nh SET
Trang 10MCS-51 & Assembly language 19
EQU, SET
symbol EQU expression symbol EQU register symbol SET expression symbol SET register
symbol tên c a ký hi u
expression bi u th c s , có th ch a các ký hi u nh ng ph i đ c
khai báo tr c đó
register thanh ghi: A, R0-R7
symbol đ c đ nh ngh a v i EQU s không đ c đ nh ngh a l i
Không th s d ng EQU đ đ nh ngh a l i các symbol đã đ nh ngh a b i SET và ng c l i
Ví d :
VALUE EQU LIMIT – 200 + 'A'
VALUE SET VALUE / 2
Trang 11MCS-51 & Assembly language 21
BIT, DATA, CODE, IDATA, XDATA
symbol BIT bit_address defines a BIT symbol
symbol CODE code_address defines a CODE symbol
symbol DATA data_address defines a DATA symbol
symbol IDATA idata_address defines an IDATA symbol
symbol XDATA xdata_address defines a XDATA symbol
symbol is the name of the symbol to define The symbol name can
be used anywhere an address of this memory class is valid
bit_address is the address of a bit in internal data memory in the area
20H 2FH or a bit address of an 8051 bit-addressable SFR
code_address is a code address in the range 0000H 0FFFFH
data_address is a data memory address in the range 0 to 127 or a special
function register (SFR) address in the range 128 255
idata_address is an idata memory address in the range 0 to 255
xdata_address is an xdata memory address in the range 0 to 65535
Ví d :
DATA_SEG SEGMENT BITADDRESSABLE
RSEG DATA_SEG ; a bit-addressable rel_seg
ENABLE_FLAG BIT 60H ; an absolute bit
DONE_FLAG BIT 24H.2 ; an absolute bit
P1_BIT2 EQU 90H.2 ; a SFR bit
RESTART CODE 00H
INTVEC_0 CODE RESTART + 3
INTVEC_1 CODE RESTART + 0BH
INTVEC_2 CODE RESTART + 1BH
SERBUF DATA SBUF ; redfinition of SBUF
RESULT DATA 40H
RESULT2 DATA RESULT + 2
PORT1 DATA 90H ; a SFR symbol
BUFFER IDATA 60H
Trang 12MCS-51 & Assembly language 23
sfr, sbit
sfr sfr_symbol = address;
sbit sfr_symbol = bit-address;
sfr_symbol is the name of the special function register (SFR) symbol to define
address is an SFR address in the range 0x80 – 0xFF
bit-address is address of an SFR bit in the format address ^ bitpos or
sfr_symbol ^ bitpos address or sfr_symbol refers to an bitaddressable
SFR and bitpos specifies the bit position of the SFR bit in the range 0 – 7.
Ví d :
sfr P0 = 0x80;
sfr P1 = 0x90;
sbit P0_0 = P0^0;
sbit P1_1 = 0x90^1;
Kh i t o b nh (Memory initialization)
¸ Kh i t o các giá tr trong b nh code
¸ T khóa
̈ DB define byte (8 bits)
̈ DW define word (16 bits)
Trang 13MCS-51 & Assembly language 25
label: DB expression , expression
label: DW expression , expression …
label: is the symbol that is given the address of the initialized memory
expression is a byte value Each expression may be a symbol,
a character string, or an expression
Ví d :
Reserving memory
¸ Khai báo dung l ng vùng b nh s đ c s d ng
¸ T khóa
̈ DBIT khai báo vùng b nh bit
̈ DS khai báo vùng b nh byte
Trang 14MCS-51 & Assembly language 27
DBIT, DS
label: DBIT expression label: DS expression
label is the symbol that is given the address of the reserved memory The label
is a typeless number and gets the current address value and the memory class of the active segment The label can only be used where a symbol of this type is allowed
expression is the number of bit (DBIT) or bytes (DS) to reserve The expression cannot
contain forward references, relocatable symbols, or external symbols
ON_FLAG: DBIT 1 ; reserve 1 bit OFF_FLAG: DBIT 1
GAP: DS (($ + 16) AND 0FFF0H) – $
DS 20
Ví d :
Liên k t ch ng trình (Program linkage)
¸ S d ng các đinh h ng liên k t ch ng trình cho phép
̈ Phân chia m t ch ng trình thành các module (file) riêng bi t
̈ Các module “trao đ i” v i nhau thông qua tên và các tham chi u
¸ Có 3 ki u đ nh h ng
̈ PUBLICđ a ra danh sách các ký hi u (symbol) đ c dùng bên ngoài module Symbol ph i đ c đ nh ngh a trong module co t khóa
PUBLIC
̈ EXTRN đ a ra danh sách các symbol mà module có th s d ng
nh ng các symbol này đ c đ nh ngh a trong module khác
Trang 15MCS-51 & Assembly language 29
PUBLIC
PUBLIC symbol , symbol …
symbol must be a symbol that was defined somewhere within the source file
Forward references to symbol names are permitted All symbol names, with the exception of register symbols and segment symbols, may be specified with the
PUBLIC directive Multiple symbols must be separated with a comma (,).
Ví d :
PUBLIC PUT_CRLF, PUT_STRING, PUT_EOS
PUBLIC ASCBIN, BINASC PUBLIC GETTOKEN, GETNUMBER
EXTRN
EXTRN class (symbol , symbol … )
class is the memory class where the symbol has been defined and
may be one of the following: BIT, CODE, DATA, IDATA, XDATA, or NUMBER (to specify atypeless symbol).
symbol is an external symbol name
Ví d : EXTRN CODE (PUT_CRLF), DATA (BUFFER) EXTERN CODE (BINASC, ASCBIN)
Trang 16MCS-51 & Assembly language 31
NAME
NAME modulename
modulename is the name to use for the object module and can be up to 40
characters long The modulename must adhere to the rules for symbol names
N u tên c a module không đ c ch ra b i directive NAME thì tên file (không có
ph n m r ng) s đ c s d ng
Ch đ c m t NAME đ c s d ng trong m t file
Address control
¸ nh h ng cho ch ng trình d ch bi t đ a ch n i s
đ t code l nh ho c đ a ch c a các thanh ghi s d ng
¸ Có 2 ki u
̈ ORG thay đ i n i dung c a b đ m đ a ch đ n
m t giá tr m i Các statements ti p theo sau s đ c đ t
b t đ u t đ a ch m i đó
̈ USING ch ra bank thanh ghi nào đ c s d ng cho R0 đ n R7
Trang 17MCS-51 & Assembly language 33
ORG
ORG expression
expression must be an absolute or simple relocatable expression without
any forward references Only absolute addresses or symbol values in the current segment may be used
N u ORG xu t hi n trong m t absolute segment thì giá tr expression là giá tr tuy t đ i
N u ORG xu t hi n trong m t relocatable segment thì giá tr c a expression đ c coi là giá tr offset
Ví d : ORG 100H ORG RESTART
USING
USING expression
expression is the register bank number which must be a value between
0 and 3
USING không t o ra b t k m t code l nh thay đ i register bank, nó ch đ n gi n
là s d ng đ a ch tuy t đ i c a các thanh ghi
USING 3 PUSH AR2 ; Push register 2 in bank 3 USING 1
PUSH AR2 ; Push register 2 in bank 1
Trang 18MCS-51 & Assembly language 35
Ph n m m Proteus
¸ Ph n m m c a hãng Labcenter Electronics Ltd
¸ Gói ph n m m
̈ V m ch nguyên lý, m ch in
̈ VSM (Virtrual System Modelling) cho phép mô ph ng ch đ mixed
gi a t ng t và s (bao g m c các h th ng có s d ng các chip vi
đi u khi n)
¸ Giá ph n m m tùy thu c vào tính n ng mà khách hàng l a
ch n
¸ Tài li u tham kh o có r t ít, ch y u là các file help đi kèm
Trang 19MCS-51 & Assembly language 37
Ví d 1
¸ Vi t ch ng trình assembler ki m tra c ng P1.5, n u
̈ b ng 0 thì thi t l p P1.6 = 1
̈ b ng 1 thì thi t l p P1.6 = 0
¸ Quá trình ki m tra đ cl p l i cho đ n khi t t ngu n
Ví d 2
¸ Vi t ch ng trình assembler phát ra xung vuông có chu k 20ms v i duty cycle 50% t i c ng P1.0 (s
d ng timer) Bi t t n s clock c a MCU là 12MHz