290 likes | 408 Views
EET 2261 Unit 3 Assembly Language ; Load, Store, & Move Instructions. Read Almy , Chapters 5 and 6. Homework #3 and Lab #3 due next week. Quiz next week. Assembly Language. In Lab #2 we programmed the HCS12 using machine language (also called machine code ).
E N D
EET 2261 Unit 3Assembly Language; Load, Store, & Move Instructions • Read Almy, Chapters 5 and 6. • Homework #3 and Lab #3 due next week. • Quiz next week.
Assembly Language • In Lab #2 we programmed the HCS12 using machine language (also called machine code). • From here on, we’ll program the HCS12 chip using assembly language, and we’ll use CodeWarrior’s built-in assembler to translate from assembly language to machine language.
C versus Assembly Language • Cis a popular high-level programming language that can be used to program the HCS12, but in this course we’re using assembly language (a low-level language) to program the HCS12. • Our textbook focuses on assembly language, but occasionally refers to how you would do something in a C program. (For example, see page 49.) You can ignore all such references to the C language.
Computer Programming Most programming today is done in high-level languages, which can run on various machines. High-level programs are easier to write and maintain than assembly programs or machine code. Assembly language is more convenient than machine language. Assembly language is used today for many applications because it executes fast and efficiently. But it must be written for a specific processor. Early computers were programmed in machine language. Machine language is tedious to write and prone to errors.
Example • This block of code adds two numbers together and stores the result in memory. • The third column is the only one the microcontroller understands. The BASIC or Assembly programs must be translated to machine code before being executed.
Computer Programming: Compilers & Assemblers High-level languages are machine-independent. The source code is translated to machine code by a compiler for the specific processor being used. High-level language program (Source program) Machine language program (Object program) Compiler Assembly language must be written for the specific processor being used, so the programmer must understand details of how this processor operates. An assembler translates the source code to machine code. Assembly language program (Source program) Machine language program (Object program) Assembler
Review: Mnemonics and Opcodes • In assembly language, an HCS12 instruction contains a mnemonic, possibly followed by one or more numbers. • Example: In the instruction LDAA #05, LDAA is the mnemonic. • When an instruction is translated into machine code, the mnemonic is replaced by an opcode. • Example: The instruction LDAA #05 becomes $8605 in machine code, because $86 is the opcode for LDAA.
Review: Operands • Most HCS12 instructions require the programmer to specify one or two operands, which are the data to be operated on. • Example: • The LDAA instruction loads some number into accumulator A. The number that’s to be loaded is the operand. • In the instruction LDAA #05, the operand is 5.
A Complete Assembly-Language Program • Let’s write a program that: • Loads 30 into Accumulator A. • Loads 21 into Accumulator B. • Adds the two accumulators, placing the sum in Accumulator A. • Stores the sum in memory location $1022. • Sits forever on a self-pointing branch instruction. • In addition to the instructions that do these steps, our program will include several lines called assembler directives.
Our Assembly-Language Program • Note: This code must be indented from the left margin. • ABSENTRY Entry • ORG $2000 ;Load program at $2000 • Entry: LDAA #30 ;Load 30 in Acc A • LDAB #21 ;Load 21 in Acc B • ABA ;Add Acc B to AccA • STAA $1022 ;Store the result • BRA * • END
Assembler Directives • In the previous program, ABSENTRY, ORG, and END are assembler directives. • These tell the assembler something about how to assemble our program. They’re not instructions that the HCS12 executes. • For example, ORG $2000tells the assembler where in memory it should load the code that follows this line.
EQU • Another useful assembler directive, EQU (Equate), lets us name locations in memory so that we can refer to them by name. • Example: MySum: EQU $1022This directive tells the assembler that wherever in our program we type MySum, the assembler should replace MySum with $1022. • Makes your programs easier to read and maintain.
Our Assembly-Language Program, Using EQU • ABSENTRY Entry • MySum: EQU $1022 • ORG $2000 ;Load program at $2000 • Entry: LDAA #30 ;Load 30 in Acc A • LDAB #21 ;Load 21 in Acc B • ABA ;Add Acc B to AccA • STAA MySum ;Store the result • BRA * • END
Some Other Assembler Directives • Another assembler directive that we’ll use in a few weeks: • DC (Define Constant) • CodeWarrior’s Help system gives detailed information on all assembler directives. See next slide…
In CodeWarrior, select Help > CodeWarrior Help from the menus
Fields in a Line • Each line in an assembly-language program has four fields, not all of which are required in every line: • Label • Operation, which may be an HCS12 instruction or an assembler directive. • Operand(s) • Comment • Only labels may appear in a line’s leftmost column. • Examples:
Rules for Labels • You can choose your own labels, but there are a few rules to follow: • A label must start with a letter (but it can contain numbers after the first letter). • Start2 is a valid label, but 2Start is not. • A label cannot contain spaces. (Use underscores instead of spaces.) • Go_here is a valid label, but Go here is not. • A label cannot be the same as instruction mnemonics or assembler directives. • ABA and ORG are not valid labels. • To make your program easier to read and understand, choose meaningful labels. • LED_on is better than Label.
Program Header • I will expect you to start each program with a program header that lists the program’s name, function, author, and date: • ;******************************************; Name: Week03FirstAssembly • ; Function: Adds two numbers and stores the ; result. • ; Author: Nick Reeder • ; Date: 07/15/2013 • ;****************************************** • All lines in a program header are comments, so they don’t affect the program’s operation.
Four Ways to Specify a Number • The HCS12 assembler gives us four ways to represent a number: • Hex, using $ prefix • Binary, using % prefix • Decimal, using no prefix • ASCII, using single quote marks around a character • See next slide for examples.
Example: Four Ways to Specify a Number • The following four statements all load the same number into Accumulator A. • LDAA #$41 • LDAA #%01000001 • LDAA#65 • LDAA#’A’
Review: Categories of Instructions • The Instruction Set Summary table lists instructions alphabetically. Sometimes it’s more useful to have instructions grouped into categories of similar instructions. • Examples of categories: • Load and Store Instructions • Addition and Subtraction Instructions • Boolean Logic Instructions • … • See Section 5 (starting on p. 55) of the HCS12 CPU Reference Manual.
Instructions that Load, Store, Transfer, Exchange, or Move Data • Many instructions simply copy data from one place to another. • Load instructions copy data from memory to a register. • Store instructions copy data from a register to memory. • Transferinstructions and Exchange instructions copy data from one register to another register. • Move instructions copy data from one place in memory to another place in memory.
Load Instructions (Table from p. 56 of the HCS12 CPU Reference Manual.)
Big-Endian Versus Little-Endian • For instructions that load a two-byte register from memory, the HCS12 loads the lower-addressed memory byte into the high-order byte of the register. • This is known as the big-endian convention, and it’s used by all Freescale processors. • Intel processors use the opposite convention, which is called little-endian.
Store Instructions (Table from p. 57 of the HCS12 CPU Reference Manual.)
Transfer and Exchange Instructions (Table from p. 58 of the HCS12 CPU Reference Manual.)
Move Instructions (Table from p. 58 of the HCS12 CPU Reference Manual.)
Clear Instructions • (Table from p. 63 of the HCS12 CPU Reference Manual.)