Presentation is loading. Please wait.

Presentation is loading. Please wait.

EET 2261 Unit 3 Assembly Language; Load, Store, & Move Instructions

Similar presentations


Presentation on theme: "EET 2261 Unit 3 Assembly Language; Load, Store, & Move Instructions"— Presentation transcript:

1 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. Handouts: Quiz #2, Unit 3 practice sheet.

2 Assembly Language to Machine Language
In Lab #2 we programmed the HCS12 the hard way, writing assembly-language instructions and then manually translating each instruction into machine language (also called machine code). From here on, we’ll use CodeWarrior’s built- in assembler to do the translation from assembly language to machine language.

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

4 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. Traditionally, assembly language executed faster and more efficiently than high-level languages. 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.

5 Assembly Language (HCS12) Machine Language (HCS12)
Example These blocks of code add two numbers together and store the result in memory. High-Level Language (C) Assembly Language (HCS12) Machine Language (HCS12) Num1 = 30; Num2 = 21; Sum = Num1 + Num2; LDAA #30 LDAB #21 ABA STAA $1022 $86 1E $C6 15 $18 06 $7A 10 22 The third column is the only one the microcontroller understands. The C or Assembly codes must be translated to machine code before being executed.

6 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

7 Typing Assembly Language in CodeWarrior
Up to now, when we’ve used CodeWarrior we’ve gone directly into the Debugger and used the Debugger’s Memory window to enter machine code in memory. From now on we’ll type the assembly language and then tell CodeWarrior to translate it to machine code and load it in memory.

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

9 Our Assembly-Language Program
ABSENTRY Entry ORG $ ;Load program at $2000. Entry: LDAA # ;Number of apples. LDAB # ;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 at least one space from the margin. -Have them enter and run it. -Intentionally introduce some errors to see error messages or effect on program (misspell ABA; misspell Entry; omit ABSENTRY line; omit # from LDAA #30). Note that labels are case-sensitive, but instructions and directives are not.

10 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 to explain to other people (and yourself!) 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.

11 Assembler Directives In the previous program, ABSENTRY, ORG, and END are assembler directives. These tell CodeWarrior’s assembler something about how to assemble our program. They are 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.

12 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 of the CPU Reference Manual. So where are they explained? CodeWarrior’s Help system gives detailed information on all assembler directives. See next slide…

13 In CodeWarrior, select Help > CodeWarrior Help from the menus

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

15 Our Assembly-Language Program, Using EQU
ABSENTRY Entry MySum: EQU $1022 ORG $2000 ;Load program at $2000. Entry: LDAA # ;Number of apples. LDAB # ;Number of oranges. ABA ;Total pieces of fruit. STAA MySum ;Store the result. BRA * END -Have them enter and run it; note that MySum appears in the Source window, but not in the Assembly window. -Also, the colon after a label is optional.

16 Another Assembler Directive
Another assembler directive that we’ll find useful: DC (Define Constant)—Explained on next slide. So the five assembler directives that we’ll use often are ABSENTRY, ORG, EQU, DC, and END. Warning: There’s some variation between assembler directives from one assembler to another. CodeWarrior’s assembler does not recognize all of the directives listed on page 38 of the textbook.

17 The DC Assembler Directive
The DC (Define Constant) directive lets us set up values in memory when the program is downloaded to the chip, before the program runs. Example: Suppose we want to place the value $A1 into memory location $0600 before our program runs. One way to do it is manually, using CodeWarrior’s Memory window. Another way is to include the following code at the beginning of our program: ORG $ DC $A1 Do this much in CodeWarrior to see that it works.

18 The DC Assembler Directive (Cont’d.)
DC makes it easy to set up the values of many sequential bytes in memory. Example: ORG $0600 DC $A1, $34, $78, $25, $36 This will Place $A1 in memory location $0600 Place $34 in memory location $0601 Place $78 in memory location $0602 And so on.

19 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: Do Practice Question 1. Label Operation Operands Comment Entry: LDAA #30 ;Number of parking spaces. ABA ;Add deposit to balance.

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

21 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: /15/2013 ;****************************************** All lines in a program header are comments, so they don’t affect the program’s operation.

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

23 Example: Four Ways to Specify a Number
The following four statements all load the same number into Accumulator A. LDAA #$41 LDAA #% LDAA #65 LDAA #’A’ Have them enter it and debug it. Note that in the Assembly window, all four instructions are given as LDAA #65.

24 Viewing the Machine Code For a Program
After you’ve typed an assembly-language program, you can see the machine code for it and other useful information by right- clicking and choosing Disassemble. CodeWarrior will open a new window containing a program listing that shows, among other things, the machine code for each instruction and the location in memory of each instruction. (See next slide.)

25 Viewing the Machine Code For a Program (Cont’d.)
Here is part of the program listing for the program shown on the previous slide. Machine code for the LDAA instruction. Location in memory of the LDAA instruction.

26 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. The manual’s categories are very similar to, but not exactly the same as, the textbook’s categories on pages and in Chapters 6 – 11.

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

28 Load Instructions (From memory to register)
(Table from p. 56 of the HCS12 CPU Reference Manual.)

29 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. In CodeWarrior, do an example of LDS, setting up the contents of memory first.

30 Store Instructions (From register to memory)
(Table from p. 57 of the HCS12 CPU Reference Manual.) The big-endian convention applies here again for the instructions that store two-byte registers.

31 Transfer and Exchange Instructions (From register to register)
TFR can do the work of all the other Transfer Instructions and of SEX. EXG can do the work of the two other Exchange Instructions. Do Practice Questions 2, 3. (Table from p. 58 of the HCS12 CPU Reference Manual.)

32 Sign Extension (SEX!) Transfers between two 8-bit registers or between two 16-bit registers are easy. But what about transfers between an 8-bit register and a 16-bit register? When you transfer a 16-bit register to an 8-bit register, only the lower byte gets transferred. When you transfer an 8-bit register to a 16-bit register, the 8 bits get transferred to the lower byte of the 16-bit register, and all the bits in the upper byte of the 16-bit register are given the value of the 8-bit register’s sign bit (MSB). This is called a sign-extended transfer.

33 All Possible Transfers
This table from p. 296 of the CPU Reference Manual shows all of the possibilities using TFR.

34 No SEX with EXG Exchanges between two 8-bit registers or between two 16-bit registers are easy. But what about exchanges between an 8-bit register and a 16-bit register? The 8-bit register receives only the lower byte of the 16-bit register. The lower byte of the 16-bit register receives the contents of the 8-bit register, and the upper byte of the 16-bit register receives 0s—no sign extension.

35 All Possible Exchanges
This table from p. 182 of the CPU Reference Manual shows all of the possibilities using EXG. Do Practice Question 4.

36 Move Instructions (From memory to memory)
(Table from p. 58 of the HCS12 CPU Reference Manual.)

37 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

38 Clear Instructions (Table from p. 63 of the HCS12 CPU Reference Manual.) I’m listing the Clear instruction here because our textbook does so.

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

40 Indexed Addressing Mode
Indexed addressing mode has 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 in the textbook or the section starting on p. 34 of the HCS12 CPU Reference Manual. We’ll return to indexed addressing mode in two weeks.

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

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

43 Simple Example of Indexed Addressing
ABSENTRY Entry ORG $2000 Entry: LDX #$1500 LDY #$1600 LDAA 3,X ;Loads A from $1503. INCA STAA 8,Y ;Stores A to $1608. BRA * END Do it in CodeWarrior, first setting up a value in memory location $1503. -Then do Practice Question 5.

44 Why Is This Useful? It might be hard 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.)


Download ppt "EET 2261 Unit 3 Assembly Language; Load, Store, & Move Instructions"

Similar presentations


Ads by Google