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

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

  • Upload
    javen

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

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 ). - PowerPoint PPT Presentation

Citation preview

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

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.

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

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.

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

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.

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

© 2009 Pearson Education, Upper Saddle River, NJ 07458. All Rights ReservedFloyd, Digital Fundamentals, 10th ed © 2009 Pearson Education, Upper Saddle River, NJ 07458. All Rights ReservedFloyd, Digital Fundamentals, 10th ed

Computer Programming

Computer hardwar e (the “machine”)

Machine language

Assembly language• English-like terms representing

binary code• Machine dependent

High-level language• Closer to human language• Portable

• CPU• Memory (RAM, ROM)• Disk drives• Input/Output

• Binary code (1s and 0s)• Machine dependent

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.

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.

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

Example

High-Level Language(BASIC)

Assembly Language (HCS12)

Machine Language (HCS12)

FirstNumber = 30SecondNumber = 21Sum = FirstNumber+SecondNumber

LDAA #30LDAB #21ABASTAA $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.

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

© 2009 Pearson Education, Upper Saddle River, NJ 07458. All Rights ReservedFloyd, Digital Fundamentals, 10th ed © 2009 Pearson Education, Upper Saddle River, NJ 07458. All Rights ReservedFloyd, Digital Fundamentals, 10th ed

Computer Programming: Compilers & AssemblersHigh-level languages are machine-independent. The source code is translated to machine code by a compiler for the specific processor being used.

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.

High-level language program

(Source program)

Machine language program

(Object program)

Machine language program

(Object program)

Assembly language program

(Source program)Assembler

Compiler

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

Review: Registers Inside the HCS12’s CPU

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

• So where are they explained? CodeWarrior’s Help system gives detailed information on all assembler directives. See next slide…

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

In CodeWarrior, select Help > CodeWarrior Help from the menus

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

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.

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

Our Assembly-Language Program, Using EQU

ABSENTRY EntryMySum: 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

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

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.

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

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:

Label Operation Operands CommentEntry: LDAA #30 ;Number of parking spaces.

ABA ;Add deposit to balance.

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

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.

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

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.

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

• 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

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

• 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

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

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.

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

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.

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

Load Instructions

(Table from p. 56 of the HCS12 CPU Reference Manual.)

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

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.

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

Store Instructions

(Table from p. 57 of the HCS12 CPU Reference Manual.)

• The big-endian convention applies here too for the instructions that store two-byte registers.

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

Transfer and Exchange Instructions

(Table from p. 58 of the HCS12 CPU Reference Manual.)

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

Move Instructions

(Table from p. 58 of the HCS12 CPU Reference Manual.)

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

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

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

(Table from p. 63 of the HCS12 CPU Reference Manual.)

Clear Instructions

Page 33: EET 2261 Unit 3 Assembly Language ; Load, Store, & Move 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.

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

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.

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

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.

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

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.

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

Simple Example of Indexed Addressing

ORG $2000

LDX #$1500LDY #$1600

LDAA 3,XINCASTAA 8,Y

BRA *END

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

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