1 / 76

Introduction to Micro Controllers & Embedded System Design Assembly Language Programming

Introduction to Micro Controllers & Embedded System Design Assembly Language Programming. Department of Electrical & Computer Engineering Missouri University of Science & Technology hurson@mst.edu. Introduction to Micro Controllers & Embedded System Design.

jimmybutler
Télécharger la présentation

Introduction to Micro Controllers & Embedded System Design Assembly Language Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Introduction to Micro Controllers & Embedded System Design Note, this unit will be covered in three lectures. In case you pre-test it, then you have the following options: 1) Take the early test and start module7 2) Study the supplement module (supplement.module6) 3) Act as a helper to help other students in studying module6 Note, options 2 and 3 have extra credits as noted in course outline.

  3. Introduction to Micro Controllers & Embedded System Design  Enforcement of background Glossary of prerequisite topics No Familiar with the topics? Review background for this module Yes Take Test At the end: take exam, record the score, impose remedial action if not successful No Pass? Remedial action  Yes Glossary of topics Current Module No Familiar with the topics? Take the Module Yes Take Test No Pass? Yes Options Lead a group of students in this module (extra credits)? Study next module? Study more advanced related topics (extra credits)? Extra Curricular activities

  4. 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

  5. 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

  6. 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

  7. A linker is a program that combines and converts relocatable object program forms into an absolute form. A.R. Hurson

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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 soon (e.g., ORG). Note that no machine instruction will be generated for directives. • Assembler controls set assembler modes and direct assembly flow (e.g., $TITLE). • Comments enhance readability of the program. A.R. Hurson

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. Immediate data MOV A, #0FF00H MOV A, #00FFH Are syntactically correct MOV A, #0FE00H MOV A, #01FFH Are syntactically incorrect A.R. Hurson

  21. Immediate data • In case of signed decimal notation, constant can be in the range of -256 to +255 MOV A, #-256 MOV A, #0FF00H Both instructions put 00H into accumulator A.R. Hurson

  22. 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 SBUF is a predefined symbol A.R. Hurson

  23. 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

  24. Code address • A code address (mainly a label) is used in the operand field for various jump instructions. HERE:    SJMP HERE A.R. Hurson

  25. 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

  26. Examples: MOV DPTR, #04FFH + 3 MOV DPTR, #0502H ; entire 16-bit result is used A.R. Hurson

  27. 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

  28. Examples: MOV A, #15 MOV A, #1111B MOV A, #0FH MOV A, #17Q MOV A, #15D A.R. Hurson

  29. 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

  30. Assemble-Time Expression Evaluation • Arithmetic operators are: A.R. Hurson

  31. 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

  32. Assemble-Time Expression Evaluation • Logical operators are: • Note: NOT operator is a unary operator • Note: The result is either 0000H (FALSE) or FFFFH (TRUE). A.R. Hurson

  33. 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

  34. Assemble-Time Expression Evaluation • Special operators are: A.R. Hurson

  35. Examples: MOV A, # 8 SHL 1 MOV A, # 10H ; these two are the same MOV A, # HIGH 1234H MOV A, # 12H ; these two are the same A.R. Hurson

  36. Assemble-Time Expression Evaluation • Relational operators are binary operators with the result of either true or false. They are: A.R. Hurson

  37. Examples: • The result for all these operations is the same as: MOV A, # 0FFH MOV A, # 5 = 5 MOV A, # 5 NE 4 MOV A, # ‘X’ LT ‘Z’ MOV A, # ‘X’ >= ‘X’ MOV A, # $ > 0 MOV A, # 100 GE 50 A.R. Hurson

  38. 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

  39. 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

  40. 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

  41. 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

  42. Directives: Assembler State Control • END should be the last statement in the source file. • Note: END does not have any operand. A.R. Hurson

  43. Directives: Assembler State Control • USING directive informs the current active register bank. Its general format is: USING expression A.R. Hurson

  44. Directives: Assembler State Control Example: USING 3 PUSH R7 USING 1 PUSH R7 • 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

  45. Directives: Symbols • Creates 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

  46. 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

  47. 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

  48. 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

  49. 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 • INC R0 • DJNZ R7, LOOP • continue A.R. Hurson

  50. 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

More Related