0% found this document useful (0 votes)
50 views

CSC C85 Summer 2004: Assembly Language Programming

The document summarizes Chapter 7 of an assembly language programming course. It discusses assembly language programs, assembler operation, assembly language format, expression evaluation, and assembler directives. The key points are that assembly language corresponds to machine instructions, assemblers translate assembly code to object code, and directives provide instructions to the assembler rather than the microprocessor.

Uploaded by

Ali Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

CSC C85 Summer 2004: Assembly Language Programming

The document summarizes Chapter 7 of an assembly language programming course. It discusses assembly language programs, assembler operation, assembly language format, expression evaluation, and assembler directives. The key points are that assembly language corresponds to machine instructions, assemblers translate assembly code to object code, and directives provide instructions to the assembler rather than the microprocessor.

Uploaded by

Ali Ahmad
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

CSC C85 Summer 2004

Chapter 7 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

Two Pass Assembler allows forward references

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

Assembler Operation con't


Pass Two object and listing files are created mnemonics are converted to opcodes and placed in the output files. operands are evaluated and placed after the instruction opcodes. symbols that appear in the operand field are retrieved from the symbol table

Assembler Operation con't


Object File if absolute, it only contains the binary bytes of the machine language if relocatable, it will also contain a symbol table and other info needed for linking and locating
OBJECT FILE 1 OBJECT FILE 2 LIBRARY LINKER/ LOCATER L51

ABSOLUTE OBJECT MODULE CROSS-REFERENCE MAP FILE

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.

Assembly Language Format


Assembly language programs contain the following: 1) Machine instructions mnemonics of executable instructions 2) Assembler directives instructions to the assembler that define program structure, symbols, data, constants, etc. 3) Assembler controls set assembler modes and direct assembly flow 4) Comments enhance readability of programs General format:
[Label:] mnemonic [operand][,operand][.. .] [;comment]

Assembly Language Format con't


Label Field represents the address of the instruction or data that follows identified by the terminating colon (:) symbols are more general ( a label is one type of symbol) and do not always represent an address. symbols are assigned values using directives such as EQU, SEGMENT, BIT, DATA, etc. and do not terminate with a colon Ex.
PAR EQU 500 A,#0FFH ;PAR IS A SYMBOL WHICH ;REPRESENTS THE VALUE 500 ;START IS A LABEL WHICH ;REPRESENTS THE ADDRESS OF THE ;MOV INSTRUCTION

START: MOV

Assembly Language Format con't


Label Field con't a symbol (or label): must begin with a letter, question mark, or underscore (_) must be followed by letters, digits, ? or _ can contain up to 31 characters may use upper or lower case characters, but they are treated the same reserved words (such as mnemonics, operators, predefined symbols and directives) may not be used

10

Assembly Language Format con't


Operand Field
follows mnemonic field

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

Assembly Language Format con't


Mnemonic Field contains instruction mnemonics or assembler directives follows the label field Examples of instruction mnemonics:
ADD MOV DIV INC

Examples of assembler directives:


ORG EQU DB

12

Assembly Language Format con't


Comment Field
contains remarks that clarify the program

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

Assembly Language Format con't


Indirect Address the commercial at sign (@) indicates address indirection may only be used with R0, R1, DPTR or PC examples: ADD A,@R0 retrieves a byte of data from internal RAM at the address specified in R0 MOVC A,@A+PC retrieves a byte of data from external code memory at the address formed by adding the contents of the ACC to the PC. Note that the value of the PC, when the add takes place, is the address of the instruction following MOVC. 14

Assembly Language Format con't


Immediate Data provides data in the operand field that becomes part of the instruction preceded with a pound sign (#) example:
CONSTANT EQU MOV ORL 100 A,#0FEH 40H,#CONSTANT

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

Assembly Language Format con't


Data Address used if an on-chip data memory address (00H to 7FH) or an SFR address (80H to 0FFH) is required in the operand field predefined symbols may be used for the SFR addresses. examples:
MOV MOV A,45H A,SBUF ;same as MOV A,99H

16

Assembly Language Format con't


Bit Address 3 ways to specify a bit address in an instruction: 1)explicitly by giving the address 2)using the dot operator between the byte address and the bit position 3)using a predefined assembler symbol examples: SETB 0E7H SETB ACC.7 JNB JNB TI,$ 99H,$ ;EXPLICIT BIT ADDRESS ;DOT OPERATOR (SAME AS ABOVE) ;TI IS A PREDEFINED SYMBOL ;(SAME AS ABOVE) 17

Assembly Language Format con't


Code Address used in the operand field for jump instructions, including: relative jumps (SJMP and conditional jumps) absolute jumps and calls (ACALL, AJMP) long jumps and calls (LJMP, LCALL) usually given in the form of a label: HERE: . . . SJMP HERE ASM51 will determine the correct code address and insert the correct 8-bit signed offset, 11-bit page address, or 16 bit long address, as appropriate

18

Assemble-Time Expression Evaluation


Number Bases numeric constants must be followed by the following: B O or Q H MOV MOV MOV MOV MOV - binary - octal - hexadecimal A,#15 A,#1111B A,#0FH A,#17Q A,#15D 19

D or nothing - decimal the following instructions are all equivalent:

Assemble-Time Expression Evaluation


Character Strings strings using 1 or 2 characters may be used as operands in expressions ASCII codes are converted to the binary equivalent by the assembler character constants are enclosed in single quotes:
SUBB A,#'0' MOV MOV DPTR,#'AB' DPTR,#4142H ;same as above line ;convert ASCII digit ;in ACC to binary digit

20

Assemble-Time Expression Evaluation


Arithmetic Operators
arithmetic operators are:

+ * /

addition subtraction multiplication division

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

Assemble-Time Expression Evaluation


Logical Operators
the logical operators are:

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

operator must be separated from operands by a space or tab

Assemble-Time Expression Evaluation


Special Operators
the special operators are:

SHR SHL

shift right shift left

HIGH high-byte LOW low-byte () evaluate first

the following 2 instructions are the same: MOV MOV A,#8 SHL 1 A,#10H 23

Assemble-Time Expression Evaluation


Relational Operators
the relational operators are:

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

Assemble-Time Expression Evaluation


Expression Examples
the following are all examples of expressions and their values: Expression 'B' 'A' 8/3 155 MOD 2 4 * 4 8 AND 7 NOT 1 'A' SHL 8 LOW 65535 (8 + 1) * 2 5 EQ 4 Result 0001H 0002H 0001H 0010H 0000H FFFEH 4100H 00FFH 0012H 0000H

25

Assemble-Time Expression Evaluation


Operator Precedence
the precedence of expression operators from highest to lowest is:

() 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

assigns a numeric value to a specified symbol name.

DB 'This is a message' EQU $ - MESSAGE ;LENGTH = LENGTH OF ;MESSAGE

SET same as EQU except the symbol may be redefined later 32

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)

this buffer could be cleared using the following:


MOV MOV MOV DJNZ R7,#LENGTH R0,#BUFFER @R0,#0 R7,LOOP

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

(continue program) END

45

You might also like