CSC C85 Summer 2004: Assembly Language Programming
CSC C85 Summer 2004: Assembly Language Programming
Introduction
assembly language program written using labels, mnemonics, etc. in which each statement corresponds to a machine instruction. source code (or symbolic code), cannot be executed by a computer machine language program binary codes that represent instructions to a computer. also called object code, are executable by a computer assembler program that translates an assembly language program into a machine language program resulting object code may be relocatable requiring a linking stage to set the absolute address for execution. 2
Introduction con't
linker a program that combines relocatable object programs (modules) and produces an absolute object program that is executable by a computer. segment a unit of code or data memory which may be relocatable or absolute. module contains one or more segments or partial segments and has a name assigned by the user. determines the scope of local symbols can be thought of as a file 3
Introduction con't
program consists of a single absolute module, merging all absolute and relocatable segments from all input modules. contains only the binary codes for instructions that are understood by a computer. Note: we will be using A51 a slightly different assembler from ASM51 in the text It is invoked by: A51 source_file [assembler controls]
Assembler Operation
Relocatable Object File 8051 Source File A51
Listing File
uses a location counter as the address of instructions and the values for labels Pass One source file is scanned and symbol table is built location counter is incremented by length of each instruction 5
after binding, the absolute object module can be tested with the dScope-51 simulator or used for PROM programming or run on the Blue EarthTM Micro.
START: MOV
10
contains the address or data used by the instruction a label may be used to represent the address of the data or a symbol may be used to represent a data constant values and constants in this field may be expressed as explicitly i.e. 0FFH with a predefined symbol e.g. ACC, SBUF with an expression e.g. 2 + 3 all expression evaluations are performed using 16 bit arithmetic, however the result is truncated to 8 bits if required 11
12
are placed at the end of the line must begin with a semi-colon (;) Special Assembler Symbols used for the register-specific addressing modes include A, R0-R7, DPTR, PC, C, and AB additionally, a dollar sign ($) can be used to refer to the current value of the location counter examples: SETB C INC JNB DPTR TI,$
13
all immediate data operations (except MOV DPTR,#data) require 8 bits of data immediate data are evaluated as a 16-bit constant, and then then low-byte is used all bits in the high-byte must be the same (00H or FFH) 15
16
18
20
+ * /
MOD modulo (remainder after division) the following 2 instructions are the same: MOV MOV A,#10 + 10H A,#1AH
the MOD operator must be separated from its operands by at least one space or tab or the operands must be enclosed in brackets (in order to avoid confusion with a symbol)
21
OR
logical OR
AND logical AND XOR logical Exclusive OR NOT logical NOT (complement) the operation is applied on the corresponding bits in each operand the following 2 instructions are the same: MOV MOV A,#'9' AND 0FH A,#9 22
SHR SHL
the following 2 instructions are the same: MOV MOV A,#8 SHL 1 A,#10H 23
EQ NE LT LE GT GE
equals not equals less than less than or equal to greater than greater than or equal to
when a relational operator is used between 2 operands, the result is always either false (0000H) or true (FFFFH) 24
25
() HIGH LOW * / MOD SHL SHR + EQ NE LT LE GT GE = <> < <= > >= NOT AND OR XOR when operators of the same precedence are used, they are evaluated left to right 26
Assembler Directives
are instructions to the assembler program only; they are not executable by the target microprocessor they are placed in the mnemonic field of the program ASM51 provides the following categories of directives: Assembler state control (ORG, END, USING) Symbol definition (SEGMENT, EQU, SET, DATA, IDATA, XDATA, BIT, CODE) Storage initialization/reservation (DS, DBIT, DB, DW) Program linkage (PUBLIC, EXTRN, NAME) Segment selection (RSEG, CSEG, DSEG, ISEG, BSEG, XSEG) 27
Assembler Directives
Assembler State Control ORG (Set Origin) format: ORG expression alters the location counter to set a new program origin for statements that follow (A label not not allowed) examples:
ORG 100H ;SET LOCATION COUNTER TO 100H ;SET TO NEXT 4K BOUNDARY ORG ($ + 1000H) AND 0F000H
can be used in any segment type if current segment is absolute, the value is an absolute address in the current segment if current segment is relocatable, the value is treated as an offset from the base address of the current segment 28
Assembler Directives
Assembler State Control End format: END should be the last statement in the source file no label is permitted nothing beyond the END statement is processed by the assembler
29
Assembler Directives
Assembler State Control Using format: USING expression informs the ASM51 of the currently active register bank subsequent uses of the predefined symbolic register addresses AR0-AR7 will convert to the appropriate direct address for the active register bank does not actually switch register banks example:
MOV PUSH PSW,#00011000B ;SELECT REGISTER BANK 3 AR7 ;ASSEMBLE TO PUSH 0FH USING 3
30
Assembler Directives
Symbol Definition Segment format: symbol SEGMENT expression symbol is the name of the relocatable segment or memory space as defined below: CODE (code segment) XDATA (external data segment) DATA (internal data space accessible by direct addressing, 00H-7FH) IDATA (entire internal data space accessible by indirect addressing, 00H-7FH) BIT (bit space; byte locations 20H-2FH of internal data space)
31
Assembler Directives
Symbol Definition EQU (Equate) format: examples:
N27 HERE CR MESSAGE: LENGTH EQU 27 EQU $ EQU 0DH ;SET N27 TO THE VALUE 27 ;SET HERE TO THE VALUE ;OF THE LOCATION COUNTER ;SET CR (CARRIAGE RETURN) TO 0DH
symbol EQU
expression
Assembler Directives
Storage Initialization/Reservation DS (Define Storage) format: [label:] DS expression reserves space in byte units can be used in any segment type except BIT expression must be a valid assemble-time expression with no forward references and no relocatable or external references. when DS statement is encountered, the location counter is incremented by the value in the expression the sum of the location counter and the specified expression should not exceed the current address space 33
Assembler Directives
Storage Initialization/Reservation DS (Define Storage) the following allocates 40 bytes in internal data segment:
DSEG LENGTH: BUFFER: EQU DS AT 30H 40 LENGTH ;40 BYTES RESERVED ;PUT IN DATA SEGMENT ;(ABSOLUTE, INTERNAL)
LOOP:
34
Assembler Directives
Storage Initialization/Reservation DBIT format: [label:] DBIT expression reserves space in bit units can be used only in a BIT segment expression must be a valid assemble-time expression with no forward references when DBIT statement is encountered, the location counter of the current (BIT) segment is incremented by the value in the expression note that the basic unit of the location counter is bits, not bytes 35
Assembler Directives
Storage Initialization/Reservation DBIT Example: the following directives create 3 flags in an absolute bit segment: BSEG ;bit segment (absolute) KBFLAG: DBIT 1 ;keyboard status PRFLAG: DBIT 1 ;printer status DKFLAG: DBIT 1 ;disk status Since an address is not specified with BSEG in the above example, the address of the flags could be determined by examining the .LST file. If BSEG is being used for the first time, KBFLAG would be at bit address 00H
36
Assembler Directives
Storage Initialization/Reservation DB (Define Byte) format:
[...] [label:] DB expression, [,expression]
initializes code memory with byte values since it actually places data constants in code memory, a CODE segment must be active character strings are allowed (enclosed in single quotes) with each character being converted to its ASCII code if a label is used, it is assigned the address of the first byte example:
CSEG AT 0100H SQUARES: DB 0,1,4,9,16,25 MESSAGE: DB 'Login:',0 ;null-terminated
37
Assembler Directives
Storage Initialization/Reservation DW (Define Word) format:
[...] [label:] DW expression, [,expression]
same as DB except code memory is initialized using two memory locations (16 bits) for each data item example:
CSEG DB AT 0200H $,'A',1234H,2,'BC'
38
Assembler Directives
Program Linkage PUBLIC format:
PUBLIC symbol [,symbol] [...]
allows the list of specified symbols to be known and used outside of the currently assembled module a symbol declared PUBLIC must be defined in the current module declaring a symbol to be PUBLIC allows it to be referenced in another module example: PUBLIC INCHAR, OUTCHR, INLINE, OUTSTR
39
Assembler Directives
Program Linkage EXTRN format:
EXTRN segment_type(symbol [,symbol][...], ...)
lists symbols to be referenced in the current module that are defined in other modules list of external symbols must have a segment type associated with each symbol in the list segment types are; CODE, XDATA, DATA, IDATA, BIT, NUMBER (NUMBER is a type-less symbol defined by EQU) segment type indicates the way a symbol may be used PUBLIC and EXTRN directives work together 40
Assembler Directives
Program Linkage NAME format:
NAME module_type
all the rules for symbol names apply to module names if a name is not provided, the module takes on the file name (minus path and file extension) in the absence of any use of the NAME directive, a program will contain one module for each file useful for large programs where it makes sense to partition the problem into modules (eg. a module may contain several files containing routines that are related) 41
Assembler Directives
Segment Selection Directives RSEG (Relocatable Segment) format:
RSEG segment_name
where segment_name is the name of a relocatable segment previously defined with the segment directive assembler diverts subsequent code or data into the named segment until another segment selection derivative is encountered
42
Assembler Directives
Segment Selection Directives Selecting Absolute Segments format:
CSEG DSEG ISEG BSEG XSEG [AT address] [AT address] [AT address] [AT address] [AT address]
where segment_name is the name of a relocatable segment previously defined with the segment directive assembler diverts subsequent code or data into the named segment until another segment selection derivative is encountered
43
Assembler Directives
Segment Selection Directives Selecting Absolute Segments if absolute address is provided (with AT address), the assembler terminates the last absolute address segment, if any, of the specified segment type and creates a new absolute segment starting at that address if absolute address is not specified, the last absolute segment of the specified type is continued otherwise, a new segment is created starting at location 0 forward references are not allowed and start addresses must be absolute
44
Assembler Directives
Segment Selection Directives (example)
ONCHIP SEGMENT DATA EPROM SEGMENT CODE BSEG FLAG1: DBIT FLAG2: DBIT RSEG TOTAL: DS COUNT: DS SUM16: DS RSEG BEGIN: MOV AT 1 2 ONCHIP 1 1 2 EPROM TOTAL,#0 ;begin relocatable code segment ;begin relocatable data segment 70H ;relocatable data segment ; relocatable code segment ;begin absolute bit segment
45