790 likes | 955 Views
Introduction to Micro Controllers & Embedded System Design Assembly Language Programming. Department of Electrical & Computer Engineering Missouri University of Science & Technology hurson@mst.edu.
E N D
Introduction to Micro Controllers&Embedded System DesignAssembly Language Programming Department of Electrical & Computer Engineering Missouri University of Science & Technology hurson@mst.edu A.R. Hurson
An assembly language program is a program written using labels, mnemonics, and so on, in which a statement corresponds to a machine instruction. It is also called source code or symbolic code. • Note: An assembly language program is not executable by a computer, it must undergo translation by “assembler” to machine language. • Note: In this course, in places, I use the terms “assembly program” and “machine program” interchangeably. A.R. Hurson
A machine language program is the binaryrepresentation of assembly language program. It is also called object code and is executableby a computer. A.R. Hurson
An assembler is a compiler that translates assembly language program to an equivalent machine language program. • Machine program may be in “absolute form” or “relocatable form”. In the latter case, “linking” is required to set the absolute addresses for execution. A.R. Hurson
A linker is a program that combines and converts relocatable object program forms into an absolute form. A.R. Hurson
In short an assembler receives a source file as input (e.g., PROGRAM.SRC) and generates an object file (PROGRAM.OBJ) and listing file (PROGRAM.LST) as output. PROGRAM.OBJ Assembler PROGRAM.SRC PROGRAM.LST A.R. Hurson
Aforementioned translation usually takes place in two passes: • In pass1 source file is scanned and a “symbol table” is generated. • In pass, symbol table generated in phase1 is used to generate the object file and the listing file. A.R. Hurson
Pass1 – creation of Symbol table • The location counter defaults to 0 or set by the ORG directive is initialized. • As the source file is scanned, the location counter is incremented by the length of each instruction, length of defined data directives (DB or DW), and length of reserved memory directives (DS). • Each time a label is detected, it is placed in the symbol table along with the content of the location counter. • Symbols defined by equate directives (EQU) are also placed in the symbol table along with the equated value. A.R. Hurson
Pass2 – creation of Object and listing files • To create the object file: • Mnemonics are converted to op.codes. • Symbols appear in the operand field are replaced with their values retrieved from the symbol table and correct data and addresses are calculated. • The listing file consist of ASCII text for both the source program and the machine language program is generated. A.R. Hurson
Assembly Language Program Format • An assembly language program is a collection of assembly language instructions and contains the following: • Machine instructions • Assembler directives • Assembler controls, and • Comments • An assembly instruction has the following general format: [label:] Mnemonic [operand][,operand]…. [:comment] A.R. Hurson
Assembly instructions aremnemonics of executable instructions ( e.g., INC DPTR) • Assembler directives are instructions to the assembler that define program structure: symbols, data, constants, and son (e.g., ORG). Note that no machine instruction will be generated for directives. • Assembly controls set assembler modes and direct assembly flow (e.g., $TITLE). • Comments enhance readability of the program. A.R. Hurson
A Label represents the address of the instruction that follows. Label must be terminated with a colon (:). • In a more general term we can talk about symbols where label is a type of symbol with the requirements that it must terminate with a colon. • Symbols are assigned values or attributes using directives such as EQU, SEGMENT, BIT, DATA, … • Symbols may be addresses, data constants, names of segments, or other constructs conceived by the programmer. A.R. Hurson
Example: • A symbol • 1) must begin with a letter, question mark, or underscore, • 2) must be followed by a letter, digit, ?, or _, • 3) can be of length 31 characters, and • 4) may use either upper or lower case characters (reserve words may not be used). PAR EQU 500 ; “PAR” is a symbol which represents ; the value 500 START: MOV A, #0FFH ; “START” is a label which represents ; the address of the MOV instruction A.R. Hurson
Mnemonic Field includes directives (i.e., ORG, EQU, DB, …) and instruction mnemonics (e.g., ADD, MOV, DIV, …). • Operand Field contains the address or data values used by the instruction. A label nay be used to represent the address of the data or a symbol may be used to represent a data constant. Some operations such as RET do not have any operand, whereas, others may have one or more operand(s) separated by commas. • Comment must begin with a semicolon (;). Subroutines or large segments of a program may begin with a comment block. A.R. Hurson
Special Assembler Symbols includes A, B, R0-R7, DPTR, PC, C, AB, … In addition dollar sign ($) can be used to refer to the current value of the location counter. • Example the last sentence can be written as: SETB C INC DPTR JNB TI, $ HERE: JNB TI, HERE A.R. Hurson
Indirect Address • The “at” sign (@) indicates indirect addressing and may only be used with R0, R1 of active bank, DPTR, or the PC. ADD A, @R0 ; Operand is in internal RAM at ; the location indicated by the ; content of R0 MOV A, @A+PC ; Operand is in external code ; memory at an address which is ; formed by adding content of the ; accumulator to the program counter A.R. Hurson
Immediate data where operand value is in the operand field. Immediate data is designated by a pound sign (#). • All immediate data operations except (MOV DPTR, #data) require 8 bits of data. Immediate data are evaluated as a 16-bit constant and then the low-byte is used. • Note; all bits in the high-byte must be the same (i.e., 00H or FFH), otherwise an error message “will not fit in a byte” is generated. CONSTANT EQU 100 MOV A, #0FFH ORL 40H, #CONSTANT A.R. Hurson
Immediate data MOV A, #0FF00H MOV A, #00FFH Are syntactically correct MOV A, #0FE00H MOV A, #01FFH Are syntactically incorrect A.R. Hurson
Immediate data • In case of signed decimal notation, constant can be in the range of -256 to +255 MOV A, #-256 MOV A, #0FF0H Both instructions put 00H into accumulator A.R. Hurson
Data address • Many instructions use direct addressing to refer to an operand. This could be either an on-chip data memory or an SFR address. MOV A, 45H MOV A, SBUF ; Same as MOV A, 99H A.R. Hurson
BIT address • There are three ways to specify a bit address in an instruction: • Explicitly by giving the address, • Using the “dot operator” between the byte address and the bit position, or • Using a predefined assembly symbol. SETB 0E7H ; Explicit bit address SETB ACC.7 ; Dot operator, same as above JNB TI, $ ; TI is a predefined symbol JNB 99H, $ ; Same as above A.R. Hurson
Code address • A code address (mainly a label) is used in the operand field for various jump instructions. HERE: SJMP HERE A.R. Hurson
Assemble-Time Expression Evaluation: • Values and constants as an operand can be expressed in three ways: • Explicitly (e.g., 0EFH), • With a predefined symbol (e.g., ACC), or • With an expression (e.g., 2 + 3). • When an expression is used, the assembler calculates a value and inserts it into the instruction. • All expression calculations are performed using 16-bit arithmetic, however, either 8 or 16 bits are inserted into the instruction, as needed. A.R. Hurson
Examples: MOV DPTR, #04FFH + 3 MOV DPTR, #0502H ; entire 16-bit result is used A.R. Hurson
Assemble-Time Expression Evaluation • Number representation: Constant must be followed with: • “B” for binary • “O” or “Q” for octal, • “D” for decimal, and “H” for hexadecimal • Note: A digit must be the first character for a hexadecimal constant to be differentiated from a label (i.e., “0A5H” not “A5H”). A.R. Hurson
Examples: MOV A, #15 MOV A, #1111B MOV A, #0FH MOV A, #17Q MOV A, #15DB A.R. Hurson
Assemble-Time Expression Evaluation • Character representation: Strings of one or two characters may also be used. The ASCII code is converted to binary by the assembler. Character constants must be enclosed in single quotes (‘). • Examples: CJNE A, # ‘Q’, AGAIN SUBB A, # ‘0’ ; convert ASCII digit to ; binary digit MOV DPTR, # ‘AB’ MOV DPTR, # 4142H ; as above A.R. Hurson
Assemble-Time Expression Evaluation • Arithmetic operators are: A.R. Hurson
Examples: MOV A, #10 + 10H MOV A, # 1AH ; same as above MOV A, #25 MOD 7 MOV A, # 4 ; same as above A.R. Hurson
Assemble-Time Expression Evaluation • Logical operators are: • Note: NOT operator is a unary operator A.R. Hurson
Examples: MOV A, # ’9’ AND 0FH MOV A, # 9 ; these two are the same THREE EQU 3 MINUS_THREE EQU -3 MOV A, # (NOT THREE) + 1 MOV A, # MINUS_THREE MOV A, # 11111101B ; all these three instructions ; are the same A.R. Hurson
Assemble-Time Expression Evaluation • Special operators are: A.R. Hurson
Examples: MOV A, # 8 SHL 1 MOV A, # 10H ; these two are the same MOV A, # HIGH 1234H MOV A, # 12 ; these two are the same A.R. Hurson
Assemble-Time Expression Evaluation • Relational operators are binary operators with the result of either true or false. They are: A.R. Hurson
Examples: • The result for all these operations is the same as: MOV A, # 0FFH MOV A, # 5 = 5 MOV A, # NE 4 MOV A, # ‘X’ LT ‘Z’ MOV A, # ‘X’ >= ‘X’ MOV A, # $ > 0 MOV A, # 100 GE 50 A.R. Hurson
Operator Precedenceis in the following order from the highest to the lowest: • Operators of the same order are evaluated from left to right. A.R. Hurson
Operator Precedence: • Example: Operation Generated value HIGH (‘A’ SHL 8) 0041H HIGH ‘A’ SHL 8 0000H NOT ‘A’ – 1 FFBFH ‘A’ OR ‘A’ SHL 8 4141H A.R. Hurson
Assembler Directives are instructions to assembler. With the exception of DB and DW, they do not have any direct effect on the contents of memory (i.e., no executable code is generated during the translation). There are several categories of directives: A.R. Hurson
Directives: Assembler State Control • Set Origin (ORG) alters the location counter to set a new program origin for statements to follow. Its general format is: ORG expression Example: ORG 100H ; Set the location counter to 100H • Each program must be started with ORG directive. A.R. Hurson
Directives: Assembler State Control • END should be the last statement in the source file. • Note: END does not have any operand. A.R. Hurson
Directives: Assembler State Control • USING directive informs the current active register bank. Its general format is: USING expression A.R. Hurson
Directives: Assembler State Control Example: USING 3 PUSH AR7 USING 1 PUSH AR7 • The first PUSH instruction assembles to PUSH 1FH (i.e., R7 in bank 3) and the second PUSH instruction translates to PUSH 0FH (R7 in bank 1). A.R. Hurson
Directives: Symbols • Create symbol that represent segments, registers, numbers, and addresses. • SEGMENT gives name to a relocatble segment and determines its type. Its general format is: Symbol SEGMENT segment-type Example EPROM SEGMENT CODE A.R. Hurson
Directives: Symbols • EQU assigns a numeric value to a specific symbol name. Its general format is: Symbol EQU expression Example N27 EQU 27 A.R. Hurson
Directives: Storage initialization/reservation • These directives initialize and reserve space in either word, byte, or bit units. The space reserved starts at the location indicated by the current value of the location counter in the currently active segment. A.R. Hurson
Directives: Storage initialization/reservation • Define Storage (DS) reserves space in byte units. Its general format is: [label:] DS expression Example: This code reserves 40 bytes buffer in the internal data segment • DSEG AT 30H ; put in data segment • LENGTH EQU 40 • BUFFER DS LENGTH ; 40 bytes reserved A.R. Hurson
Directives: Storage initialization/reservation • Define Storage (DS) • ; The following code clears the 40 bytes • ; reserved in the previous example • MOV R7, #LENGTH • MOV R0, #BUFFER • LOOP MOV @R0, # 0 • DJNZ R7, LOOP • continue A.R. Hurson
Directives: Storage initialization/reservation • Define BIT (DBIT) reserves space in bit. Its general format is: [label:] DBIT expression • Define Byte (DB) initializes code memory with byte values. Its general format is: [label:] DB expression [,expression] […] A.R. Hurson
Define Byte (DB): Example: When the aforementioned code is assembled, we will have the following result in external code memory: CSEG AT 0100H SQUARE: DB 0, 1, 4, 9, 16, 25 MESSAGE DB ‘Login:’, 0 A.R. Hurson
Define Byte (DB): Example: Address Contents 0100 00 0101 01 0102 04 0103 09 0104 10 0105 19 0106 4C 0107 6F 0108 67 0109 69 010A 6E 010B 3A 010C 00 A.R. Hurson