1 / 89

ENG3640 Microcomputer Interfacing

ENG3640 Microcomputer Interfacing. Week #2 Assembly Language Programming. Resources. Huang, Chapter 1, Sections 1.9 68HC12 Addressing Modes 1.11 Sample of HCS12 Instructions Huang, Chapter 2, Sections 2.2 Assembly Language Program Structure 2.3 Assembler Directives

bao
Télécharger la présentation

ENG3640 Microcomputer Interfacing

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. ENG3640 Microcomputer Interfacing Week #2 Assembly Language Programming ENG3640 Fall 2012

  2. Resources • Huang, Chapter 1, Sections • 1.9 68HC12 Addressing Modes • 1.11 Sample of HCS12 Instructions • Huang, Chapter 2, Sections • 2.2 Assembly Language Program Structure • 2.3 Assembler Directives • 2.4 Software Development Issues ENG3640 Fall 2012

  3. Topics • Programming Languages • Assemblers, Compilers • Assembler Syntax • Directives • Addressing Modes in detail. • Software Development Issues • Examples ENG3640 Fall 2012

  4. Programming Languages • Programming is the • Design • Coding • Debugging of a sequence of instructions or algorithms • Three language types used to bridge the CPU-human language gap: • Machine Language • Assembly Language • High Level Languages i.e C, C++ ENG3640 Fall 2012

  5. Computer Words • The fundamental unit of information in a computer is • the ``Word” • A Word is made up of several bits ‘10001100’ stored • in a specific location in memory. • The word size of the 68HC11 controller is 8 bits • The word size of the 68HC12 controller is 16 bits • Words stored in a computer’s memory unit can • represent several types of information: • Binary numerical data • Coded data • Instruction code • Addresses that are part of an instruction. ENG3640 Fall 2012

  6. (1) Binary Data Words • These are words that simply represent a numerical quantity in the binary system e.g., 01110011 could represent temp (115)10 • Signed data words • Signed magnitude 0/1 000 1111 • 1’s complement • 2’s complement ENG3640 Fall 2012

  7. (2) Coded Data Words • Data processed by a computer DO NOT have to be pure binary numbers. e.g., BCD • In BCD each group of 4 bits represent a decimal digit. • Many computers can perform arithmetic operations on BCD-coded numbers as part of their normal instruction repertoire. • Others (i.e., microcontrollers) require special effort on the part of the programmer in order to do BCD arithmetic. ENG3640 Fall 2012

  8. Cont .. coded data words • Data words are not restricted to representing only numbers! • Data words can be used to represent alphanumeric codes (ASCII) • Address Location Binary HEX 012A 11001001 C9 012B 10111101 BD 012C 01010110 56 012D 10101111 AF ASCII ENG3640 Fall 2012

  9. (3) Instruction Words Memory A program consists of a sequence of binary coded instructions that the CPU must fetch from memory and then decode and execute. Typical Instruction opcode Operand Address 4-bit 16-bits Address of location in memory where Data (operand) will be taken from or Stored (65,536 possible addresses) Indicates the operation (16 different operations) ENG3640 Fall 2012

  10. (4) Address Words Assume that 0100 represents ADD operation 0100 0101101001110010 $4 (Opcode) $5A72 (Address) The instruction can be decoded to mean the following: Fetch data word stored in address location 5A72, send it to the ALU, add it to the ACC and keep the result in the ACC. ENG3640 Fall 2012

  11. Machine Language • Machine language: binary encoding of instructions that are executed by a CPU • Ex: %10000110; %01011010 • The machine language consists of CPU opcodes and operands. • An opcode is one or more bytes that make up a single machine instruction i.e. LDDA => $86 • The operands are the “arguments” of the binary instruction. ENG3640 Fall 2012

  12. Machine Language • Machine language can directly control the microcontroller’s resources • Ex: LDAA #$5A • Code must be stored in memory • $E000: $86 • $E001: $5A Fetch/Execute Cycle ENG3640 Fall 2012

  13. Programming Languages • The higher the level of the language, the more abstract it is from the actual CPU operation. • What is Source Code? The term source is used to represent the original programming code generated by the programmer • What is Object Code? The object code is the resulting code from the software build process. ENG3640 Fall 2012

  14. Assembly Language Assembly language: machine instructions are represented into a mnemonic form • mnemonic form is then converted into actual processor instructions and associated data • Ex: LDAA #$5A 01011010 10000110 $86 ENG3640 Fall 2012

  15. The Assembler • The computer program that performs the assembly language to machine language translation is called an assembler. • If the computer that runs the assembler is not the same machine that will run the machine code, it is called cross-assembler. • For embedded systems programming we normally use a cross-assembler. ENG3640 Fall 2012

  16. Assembler Flow Diagram 14 00008000 f60801 ldab var2 15 00008003 fb0800 addb var1 16 00008006 7b0802 stab var3 Assembly Language Cross Assembler Program Listing Machine Code ENG3640 Fall 2012

  17. Assembly Language: • Disadvantages of Assembly Language! • require knowledge of the processor architecture and instruction set • many instructions are required to achieve small tasks • source programs tend to be large and difficult to follow • programs are machine dependent => requiring complete rewriting if the hardware is changed ENG3640 Fall 2012

  18. High Level Languages • Human like languages • Ex: Basic, Pascal, FORTRAN, Ada, Cobol, C, Java • C: most common high-level language for microcontroller development. • Example: var3 = var1 + var2; (single line code!) • A compiler is required to translate the high level language to machine code. • Two steps are required: High level language translation into assembly code and then to machine code. ENG3640 Fall 2012

  19. HLL Program Translation Process High Level Code Var3 = var1+var2; Cross Compiler Assembly Code Ldab var2 Addb var1 Stab var3 Machine Code 111101100 000100000 110110010 010101001 Cross Assembler ENG3640 Fall 2012

  20. Advantages of High Level Languages • It is CPU independent (so it is portable) • The program is shorter • The program is easier to read (Human friendly) • The source program is translated into machine code for each type of CPU • What is different is the translator not the program ENG3640 Fall 2012

  21. Disadvantages of High Level Languages • There is no longer a one-to-one correspondence between an instruction and the actual CPU operation. • The compiler and not the programmer generates the assembly code and therefore plays a large role in the determining the actual CPU operation. • Because compilers are not as smart as programmers, the machine code generated by a compiler is typically larger and less efficient than the machine code generated from assembly source. ENG3640 Fall 2012

  22. Coolant Temperature μ controller Examples • PROGRAM: • read input • subtract an offset • store the result * Assembly language program ORG $E000 LDAA $1031 SUBA #$20 STAA $D004 ENG3640 Fall 2012

  23. Examples The labels are assigned using assembler directives: COOLANT_TEMP EQU $1031 CT_OFFSET EQU $20 STORE_TEMPEQU $D004 LDAA COOLANT_TEMP SUBA #CT_OFFSET STAA STORE_TEMP ENG3640 Fall 2012

  24. Examples Comment Directive * Listing 2.2 * Illustrate Listing 2.1 without labels and with fewer * comments ORG $E000 ;specify start address * ;of program code LDAA $1031 ;load ACCA with contents * ;of address $1031 SUBA #$20 ;and subtract 32, hex 20 STAA $D004 ;store result in * ;address $D004 * This sample is source code only. * Corresponding machine code is not shown. ENG3640 Fall 2012

  25. Clear Accumulator A Load Accumulator A CLRA LDAA #$5C E000: 4F E001: 86 5C Fetch/Execute Operation of CPU CPU operations Fetching CLRA AR: address reg. ENG3640 Fall 2012

  26. Fetch/Execute Operation of CPU CPU operation Executing the first instruction CLRA ENG3640 Fall 2012

  27. Fetch/Execute Operation of CPU CPU Operation Fetching the second instruction opcode LDAA# ENG3640 Fall 2012

  28. Fetch/Execute Operation of CPU CPU operation Fetching the second instruction operand ($5C) and executing the instruction Note that PC increments after each clock cycle ENG3640 Fall 2012

  29. Instruction Set References Programming model of the 68HC12 ENG3640 Fall 2012

  30. Instruction Set References The instruction set summary can give enough information for using the assembly language ENG3640 Fall 2012

  31. Instruction Set References dd = 8-bit direct address ($0000-$00FF) (High byte assumed to be $00) ff = 8-bit positive offset $00 (0) to $FF (256) (Is added to the index) hh = high order byte of 16-bit extended address ii = one byte of immediate data jj = high order byte of 16-bit immediate data kk = low order byte of 16-bit immediate data ll = low order byte of 16-bit extended address mm = 8-bit bit mask (Set bits to be affected) rr = signed relative offset $80(-128) to $7F(+128) (Offset relative to the address following the machine code offset byte Instruction Set Summary. Machine coding field, operand notations ENG3640 Fall 2012

  32. Assembler Syntax • First the assembler expects its input to be a text only (ASCII) file (use a text editor to generate the file). • Source Line Syntax: Three possible types of lines in a source file • Blank Lines: provide spacing for readability • Comment Lines: If a line begins with an * or ; everything that follows in the line is a comment. Comments may also be placed at the end of a source line. • Source Lines: May contain a source codeinstruction, a directivefor the assembler,or alabel. ENG3640 Fall 2012

  33. Assembler Syntax • Each source line must have the following format: label:operation, operand, operand; comment To separate the fields either space (s) or tab (s) are used as white space. • Label: is an identifier to mark a line in a program (i.e. symbol that is defined in the first column). • The value of a label is set equal to the address of the first byte defined on that line. • Labels are used as references for jump, call, and branch instructions. ENG3640 Fall 2012

  34. Assembler Syntax (Label) Example: pulse: bset PORTJ, %00000001 bset DDRJ, %00000001 …………… …………… bne pulse swi ENG3640 Fall 2012

  35. Assembler Syntax (Operation) 2. Operation: The operation can be either • An Assembly Mnemonic (i.e. LDAA STAA) • An Assembler Directive (which is an instruction for the assembler i.e. is not part of the instruction set of the microcontroller, i.e. ORG, EQU, FDB). Following the operation is the operand or operands separated by commas. ENG3640 Fall 2012

  36. Assembler Syntax (Symbols) 2. Symbols: are the names given toequated constants and labels. • Different assemblers have different rules governing legal symbols. • Things to watch out for include: • The maximum length of a symbol • Whether the assembler is case sensitive. ENG3640 Fall 2012

  37. Assembler Directives • Assembler directives are instructions directed to the assembler, not the microcontroller. • Examples: • Origin, ORG • Equate, EQU • Form Constant, FCB • Form Double, FDB • Form Constant Character, FCC • Reserve Memory Byte, RMB ENG3640 Fall 2012

  38. Assembler Directives (ORG) • ORG: defines a starting address for the assembler. It tells it where to place the following code. • The syntax is: ORG abs_expr i.e. ORG $0800 • There should be a few orgstatement in your program. For example one for the start of the variables and another for the start of the program. ENG3640 Fall 2012

  39. Assembler Directives (EQU) • EQU: Equates are used to set a symbol equal to a value. The syntax issymbol EQU abs_expr Example: PORTJ EQU $29 • The assembler will replace every occurrence of PORTJ with $29 • Equates are important for both code readability and revisability. ENG3640 Fall 2012

  40. Assembler Directives (FCB) FCB: Form Constant Byte • Is used to define one or more bytes of constant storage • Syntax: symbol FCB expr • Example:MyConst FCB $10 • Example: NUM EQU 10 ORG $0100 TABLE FCB $10,17,NUM+6 Another term used frequently is db (define byte) dc.b (define constant byte) ENG3640 Fall 2012

  41. Assembler Directives (FDB) FDB: Form Double Byte • Is used to define one or more two-bytes of constant storage • Syntax: symbol FDB expr{,expr} • Example: ORG $2000 LIST FDB $C13,$1000,LIST+$FF,50 Explain the contents of the Table? Another term used frequently is dw (define word) dc.w (define constant word) ENG3640 Fall 2012

  42. Multiple Byte Storage • Notice the order in which the bytes are stored by the FDB directive. • The most significant byte is always stored in the lower address location. • This is called big-endian storage. ENG3640 Fall 2012

  43. Multiple Byte Storage • Intel-type processors use little-endianstorage in which the byte order is reversed. • If you were using a little-endian processor, the constants would be stored as follows: ENG3640 Fall 2012

  44. Assembler Directives (FCC) FCC: Form Constant Character • Is used to define one byte of constant storage for each ASCII character in a string. • Syntax: symbol FCC string • Example: ORG $1000 STRING FCC /Hello/ Delimiting characters can be backslash (/) or (‘) or (“) ENG3640 Fall 2012

  45. Assembler Directives (RMB) RMB: Reserve Memory Byte • Is used to reserve the number of bytes indicated for a variable (IMPORTANT: it does not assign data to the address) • Syntax: [symbol] RMB abs_expr • Example: ORG $0000 RAM RMB 4 RAM will occupy mem loc 0000,0001,0002,0003 Another term used frequently is ds (define storage) ds.b (define storage bytes) ENG3640 Fall 2012

  46. Other Assembler Directives • ds, ds.b == rmb • ds.w == rmw (reserve memory word) • Fill (fill memory) • fill value, count • Example: Space_line fill $20, 40 ENG3640 Fall 2012

  47. Addressing Modes • An addressing mode refers to the process used by the CPU to determine the location of an argument! • The location of the argument is called the Effective Address(EA). • The CPU12 addressing modes are a superset of the CPU11 addressing modes. • There are six addressing modes in common with both the CPU11 and CPU12 ENG3640 Fall 2012

  48. Addressing Modes • Inherent • Immediate • Direct • Extended • Relative • Indexed (8 different types) ENG3640 Fall 2012

  49. Addressing Modes • ADDA (opr) • Operation: Add Memory to ACC A • Boolean Expression: A + M  A • Addressing modes • IMM (Immediate) Opcode: 8B (2 Bytes) (2 Cycles) • DIR (Direct) Opcode: 9B (2 Bytes) (3 Cycles) • EXT (Extended) Opcode: BB (3 Bytes) (4 Cycles) • IND,X (Indexed) Opcode: AB (2 Bytes) (4 Cycles) • IND,Y (Indexed) Opcode: 18EB (3 Bytes) (5 Cycles) S X H I N Z V C CCR ENG3640 Fall 2012

  50. Prebyte • Notice that ADDA,Y uses two bytes as opcode! • Opcodes based on the earlier 6801 microcontroller had a single byte • New instructions + any instruction dealing with index register Y have 2-byte opcodes • A few hex numbers were reserved for the first opcode to specify that the following byte is also part of the opcode ENG3640 Fall 2012

More Related