Presentation on theme: "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."— Presentation transcript:
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). 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 C is 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.
Example High-Level Language (BASIC) Assembly Language (HCS12) Machine Language (HCS12) FirstNumber = 30 SecondNumber = 21 Sum = FirstNumber+SecondNumber LDAA #30 LDAB #21 ABA STAA $1022 $86 1E $C6 15 $18 06 $7A 10 22 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.
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: 1.Loads 30 into Accumulator A. 2.Loads 21 into Accumulator B. 3.Adds the two accumulators, placing the sum in Accumulator A. 4.Stores the sum in memory location $1022. 5.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 ABSENTRY Entry ORG $2000 ;Load program at $2000. Entry: LDAA #30 ;Number of apples. LDAB #21 ;Number of oranges. ABA ;Total pieces of fruit. STAA $1022 ;Store the result. BRA * END Note: The line starting with the word “Entry” must not be indented from the left margin. The other lines must be indented.
Comments Any text to the right of a semicolon is a comment. Comments are ignored by the assembler, so they have no effect on your program’s operation. But they are important for documenting your program so that you and other people can understand how it works. The program on the previous slide has five comments, all at the end of a line. If the first symbol in a line is a semicolon, that entire line is a comment.
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 $2000 tells the assembler where in memory it should load the code that follows this line.
Where to Find Explanations of Assembler Directives? Since assembler directives such as ABSENTRY and ORG are not HCS12 instructions, they’re not listed in the Instruction Summary Table on pages 377-380 of the CPU Reference Manual.CPU Reference Manual So where are they explained? CodeWarrior’s Help system gives detailed information on all assembler directives. See next slide…
In CodeWarrior, select Help > CodeWarrior Help from the menus
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 $1022 This 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 ;Number of apples. LDAB #21 ;Number of oranges. ABA ;Total pieces of fruit. 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) Warning: There is some variation between assembler directives from one assembler to another. Not all of the directives listed on page 38 of the textbook are recognized by CodeWarrior’s assembler.
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: LabelOperationOperandsComment Entry:LDAA#30;Number of parking spaces. ABA;Add deposit to balance.
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.
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. 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’ Example: Four Ways to Specify a Number
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.CPU Reference Manual
Instructions that Load, Store, Transfer, Exchange, or Move Data These 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. Transfer instructions 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.)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.)CPU Reference Manual The big-endian convention applies here too for the instructions that store two-byte registers.
Transfer and Exchange Instructions (Table from p. 58 of the HCS12 CPU Reference Manual.)CPU Reference Manual
Move Instructions (Table from p. 58 of the HCS12 CPU Reference Manual.)CPU Reference Manual
A Note About the Book’s Examples The textbook gives lots of good examples of transfer, exchange, and move instructions. But CodeWarrior will give an error if you type the example code exactly as it appears in the book. CodeWarrior’s assembler (unlike some other assemblers) requires a comma between the two registers or memory locations. Example: The first example on page 63 is tfr A B In CodeWarrior, you must type it as tfr A,B
(Table from p. 63 of the HCS12 CPU Reference Manual.)CPU Reference Manual Clear Instructions
Review: Addressing Modes The HCS12’s six addressing modes are: Inherent Immediate Direct Extended Indexed (which has several variations) Relative We’ve studied the first four. Now let’s begin to look at indexed addressing mode.
Indexed Addressing Mode Indexed addressing mode comes in at least five variations: Constant offset indexed addressing Auto pre/post decrement/increment indexed addressing Accumulator offset indexed addressing Constant indirect indexed addressing Accumulator D indirect indexed addressing For now, we’ll just look at the first of these. More detail is available on pages 50-54 in the textbook or the section starting on p. 34 of the HCS12 CPU Reference Manual.HCS12 CPU Reference Manual
The Original Indexed Addressing Mode In older microcontrollers (including the Freescale HC11) “indexed addressing” meant what we’re calling constant offset indexed addressing. The HCS12 added the other variations.
Variation #1: Constant Offset Indexed Addressing Mode In constant offset indexed addressing mode, the operand’s address is found by adding a constant offset to the contents of an index register (usually IX or IY, but possibly also SP or PC). Example: The instruction LDAA 3,X uses Index Register X, with 3 as the constant offset. If Index Register X contains $1500, then this instruction loads Accumulator A with the contents of memory location $1503.
Simple Example of Indexed Addressing ORG $2000 LDX #$1500 LDY #$1600 LDAA 3,X INCA STAA 8,Y BRA * END
Why Is This Useful? It might be hard for you to see at this point why you’d ever want to use indexed addressing. But it turns out to be very useful, as we’ll see in a couple of weeks. (First you need to learn about loops.)
Your consent to our cookies if you continue to use this website.