Anda di halaman 1dari 15

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

2
Addressing Modes, Assembly Fundamentals, and 68HC12 Instruction Set Introduction
2.1 Objectives:

The 68HC12 instruction set consists of one byte (sometimes two bytes) of opcode and zero to five bytes of operand addressing information. The opcode specifies the operation to be performed by the CPU with the corresponding addressing mode specified by the operand. The operand determines how the CPU interprets the memory accesses following the opcode. When you complete this lab, you should be able to: Understand operand addressing modes Extract the addressing mode given a complete 68HC12 instruction Understand the fundamentals of assembly programming Recognize and use basic 68HC12 instructions and their corresponding assembly mnemonics Use the MiniIDE editor and write simple assembly programs Related material to read: Introduction to the 68HC12 Microcontroller: Chapter 1 in the text, pages 13 20 (Addressing Modes) Introduction to the 68HC12 Microcontroller: Chapter 1 in the text, pages 20 25 (Instruction Introduction) Introduction to the 68HC12 Microcontroller: Appendix A Instruction Reference

2.2

-1-

Lab 2 2.3

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction Assembly Programming Fundamentals

An assembly language, like many other computer languages, consists of a series of instruction statements that tell the CPU what operations to perform. An assembly language has three types of statements: Assembler Directives, Instructions, and Comments. Assembler directives are interpreted by the assembler at build time and are commonly used for defining program constants and reserving space for dynamic variables, among other things. Assembly instructions are divided into two fields: Operation and Operand fields. The Operation field consists of the mnemonic names for the target machine instructions. The Operand field contains the operand(s) and assembler directive arguments (if necessary). The Comment field is an optional field that allows the programmer to document their code. Comment fields are ignored by the assembler and begin with an asterisk * or semi-colon ;. Finally, there is another optional field called the Label field. A label allows a programmer to use symbols for memory locations, which make assembly programs easier to read and write. At build time, the assembler replaces any labels with the respective memory location. Labels will be used extensively when you begin programming, but for now, lets put this idea on hold. When you begin writing assembly programs, you will follow a specified format; however, each instruction will use the following structure:
LABEL OPCODE OPERAND COMMENTS

As mentioned above, the label field will be discussed later. The opcode field contains the assembly mnemonic for the corresponding machine instruction and cannot start in the first column of the program (only labels can exist in the first column). The most common practice is to separate each field with a tab (or multiple tabs). The operand field contains the arguments required for the machine instruction or assembler directive, with comments following at the end of the instruction. Lets look at an example and clarify some common nomenclature used in this course. First, notice that the label field is not used in this example, so just ignore it for now. Second, the operation (opcode) field contains the mnemonic for the machine instruction: LoaD Accumulator A. The operand field contains the argument (operand) for the instruction: #10. The # symbol refers to a type of addressing mode, which is discussed below, but in laymans terms, this instruction is simply telling the CPU to put the value of 10 (base 10) into accumulator A. The comments field shows this by using some common nomenclature that will be used in this course. The second instruction is telling the CPU to add the value 10 (base 10) to the contents of accumulator A and store the result back in accumulator A. The parentheses around the A in the second comment field indicate the contents of register A and not the register itself, as in the first comment. Note that this type of commenting is NOT appropriate when you are writing your programs, but is used for instructive purposes here and in class.
LABEL OPCODE ldaa adda OPERAND #10 #10 COMMENTS ; 10 A ; (A) + 10 A

-2-

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

Now that we are familiar with some assembly language fundamentals, lets get some more detailed information on addressing modes, which determines how the CPU accesses memory locations to be operated upon (like # example above). 2.4 Addressing Modes INHERENT (INH) The inherent addressing mode is a single byte opcode instruction with no operand (i.e. the opcode contains all information necessary to execute the instruction). Example(s):
LABEL OPCODE psha inca deca OPERAND COMMENTS ; Put (A) on stack ; (A) + 1 ; (A) 1 A A

IMMEDIATE (IMM) The immediate addressing mode is a single byte opcode instruction followed by 1 or 2 bytes of operand(s). Immediate mode results in the operand(s) being included in the instruction stream, eliminating the need for memory access. An immediate operand is preceded with the # symbol. Example(s):
LABEL OPCODE ldaa ldd addd OPERAND #$A1 #1000 #500 COMMENTS ; $A1 ; 1000 A D D

; (D) + 500

-3-

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction DIRECT (DIR)

The direct addressing mode (a.k.a zero-page addressing) is used to access operands in the address range of $0000 - $00FF. Due to the address range restriction, only the lower byte is required as an operand. This results in a shorter instruction and therefore saves time and application memory space. Example(s):
LABEL OPCODE ldaa ldd addd OPERAND $C0 $00FE $18 COMMENTS ; ($00C0) A

; ($00FE:$00FF) D ; Preceding $00 is not needed ; (D) + ($0018:$0019) D

EXTENDED (EXT) The extended addressing mode uses the full 16-bit address memory location as the operand. This mode allows memory access in the full 64 KB address space. Example(s):
LABEL OPCODE ldaa ldd OPERAND $A100 1000 COMMENTS ; ($A100) A

; ($03E8:$03E9) D ; Use hexadecimal notation when ; accessing memory locations ; (D) + ($0800:$0801) D

addd

$0800

INDEX (IDX) The index addressing uses a post-byte plus zero to two operand bytes to determine an effective address. The effective address is found by adding the contents of an index register, X, Y, SP, or PC, with the preceding offset. Offsets can be 5, 9, or 16bit 2s complement signed numbers. The 5-bit constant offset is used most often and gives the address range of -16 to +15 from the respective index register contents. The 9bit constant offset gives an address range of -256 to +255 and the 16-bit constant offset gives the full 64 KB address space. Example(s):
LABEL OPCODE ldaa ldab staa OPERAND -2,X $100,Y 6,SP COMMENTS ; ((X) 2) ; ((Y) + $100) ; (A) (SP) + 6 A B

INDEX INDIRECT ([IDX])

-4-

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

The index indirect addressing is similar to the index addressing mode; however, instead of addressing an effective address, the index indirect is used as a pointer to the desired data address location. This addressing mode will not be used much in this course, but it is important you understand how it works. This could be on a test! Example(s):
LABEL OPCODE ldaa OPERAND [2,X] COMMENTS ; (((X) + 2)) A

Lets start with the first example from the Index addressing mode (above) and apply the indirect addressing mode instead. If youve already noticed, the comments are the same; however, an extra set of parentheses has been added around the left-hand side of the instruction description. What does this mean? In words, this simply is saying take the contents of index register X and subtract two. This will give you an address; however, instead of loading the data in this address into register A, use it as a pointer to the actual data address location. Simply stated, the contents of index register X minus two, is an address that points to the data location address. This may seem a little confusing, but see the illustration below and read page 18 in the text.

Index Register X Register A $1000 $1001 $1002 $1003 . $2000 $2001

$1000 $AA $20 $00 $AA $BB

Contents of index register X + 2 points to address $1002. 16-bit address located in $1002 to $1003 contains the address of the data location $2000, whose contents will be loaded into register A.

AUTO-PRE/POST DECREMENT/INCREMENT INDEX (IDX1) The auto-pre/post decrement/increment indexed addressing mode is similar to the index addressing mode; however, the base index register is pre/post incremented/decremented based on the operand(s) configuration. The increment/decrement magnitude is the first byte in the operand and is limited to a range of 1 to 8. This value is ALWAYS positive! The pre/post and increment/decrement is located in the configuration of the last byte of the operand which refers to the base index register to be operated upon. Lets look at a few examples below.

-5-

Lab 2 Example(s):
LABEL

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

OPCODE ldaa ldaa staa

OPERAND 2,X+ 2,-X 1,Y+

COMMENTS ; ((X)) ; (X) 2 ; (A) A, (X) + 2 X, ((X)) X A Y

(Y), (Y) + 1

For clarity, lets decode the comments in the first example above. The machine instruction is LoaD Accumulator A (ldaa) that uses the Auto-Pre/Post Decrement/Increment Index addressing mode, with operands: 2,X+. The + after the base index register X means post-increment. This instruction will first place the contents of the address location pointed to by index register X and put the result into register A. It will then add two to the value in register X and put the result back into the X register. RELATIVE (REL) The relative addressing mode is used for unconditional and conditional branching instructions. This addressing mode calculates an effective address by adding a signed relative offset to the PC (Program Counter) register. These instructions will be discussed in the next few labs. Example(s):
LABEL OPCODE bne beq bra OPERAND label label label COMMENTS ; Z not equal 0, go to label ; Z equal 0, go to label ; Always go to label

2.5

Instruction Set Introduction:

Now that youve seen several 68HC12 instruction examples, lets describe some of the basic instructions in more detail. LOAD/STORE INSTRUCTIONS The load and store instructions are used to copy contents of memory locations and registers to other registers and memory locations. Load/Store instructions only change the destination memory location contents and not the source memory contents.

Load Instructions: Function Mnemonic Operation

-6-

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction ldaa ldab ldd ldx ldy lds leas leax leay (M) (M) (M:M+1) (M:M+1) (M:M+1) (M:M+1) A B D X Y SP SP X Y

LoaD Accumulator A LoaD Accumulator B LoaD Accumulator D LoaD Index Register X LoaD Index Register Y LoaD Stack Register SP Load Effective Address SP Load Effective Address X Load Effective Address Y Store Instructions: Function STore Accumulator A STore Accumulator B STore Accumulator D STore Index Register X STore Index Register Y STore Stack Register SP

Effective Address Effective Address Effective Address

Mnemonic staa stab std stx sty sts MOVE INSTRUCTIONS

Operation (A) (B) (D) (X) (Y) (SP) M M M:M+1 M:M+1 M:M+1 M:M+1

Move instructions move data bytes (8-bits) or words (16-bits) from the source memory location to the destination memory location. There are a total of six combinations of addressing modes that can be used to specify the source and destination addresses (refer to page 23 of the text for more information). The most common used combinations are typically the IMM EXT and EXT EXT. Move Instructions: Function MOVe Byte MOVe Word Mnemonic movb movw Operation (M1) (M1:M1+1) M2 M2:M2+1

ADD/SUBTRACT INSTRUCTIONS

-7-

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

Add and subtract instructions allow for basic arithmetic operations and will be used extensively throughout the course. Add Instructions: Function Add B to A Add B to X Add B to Y ADd with Carry to A ADd with Carry to B ADD to A ADD to B ADD to D Subtract Instructions: Function Subtract B from A Subtract with Borrow from A Subtract with Borrow from B SUBtract memory from A SUBtract memory from B SUBtract memory from D Mnemonic sba sbca sbcb suba subb subd Operation (A) (B) (B) (M) C (A) (M) (B) (M) (D) (M:M+1) A B D A A B (A) (M) C Mnemonic aba abx aby adca adcb adda addb addd Operation (A) + (B) (B) + (X) (B) + (Y) (A) + (M) + C (B) + (M) + C (A) + (M) (B) + (M) (D) + (M:M+1) A X Y A B A A D

There are also some transfer and exchange instructions that will be used periodically throughout the semester. Please refer to page 22 of the text for a description of these instructions. 2.6 MiniIDE Assembly Editor:

Now that weve seen several instructions and examples, lets write a simple assembly language program, but first we need to download and get a little familiar with the MiniIDE assembly editor. To find MiniIDE for Windows or Unix free on the web, search the web for Download MinIDE and download the appropriate file to your computer. MiniIDE has already been loaded to the CSU ECE lab computers. The following instructions apply to these lab machines.

-8-

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

1. Locate and open the MiniIDE application by clicking on the desktop icon or go to Start Programs MGTEK MiniIDE (if you have trouble locating the program, contact your TA). 2. When the program first opens, typically the output and terminal windows will be visible. You can toggle the visibility of these windows by going to View Windows (Output or Terminal). Since we will not be using these windows for now, go ahead and close these windows.

OUTPUT WINDOW

TERMINAL WINDOW

3. Next, open a new file in the MiniIDE editor by going to File New. This should open a new file called Untitled with a caret blinking in the top left corner of the editor window.

-9-

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

ASSEMBLY EDITOR WINDOW

4. The MiniIDE will color code instruction mnemonics blue and comments green, but first we need to save the current file with the .asm file extension. Go to File Save As and save the current empty file as Lab2AssemblyProgram.asm.

5. Copy the following program into your Lab2AssemblyProgram.asm file (the code can be found on the last page of this lab if you want to use the copy/paste commands). If MiniIDE does not automatically color the opcode and comment fields, save and close the file and then open it back up.

- 10 -

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

6. You may have noticed that the program above is not complete. Now fill in the program section in part 6 of the procedure section below. When you complete the program section, you need to assemble the code and check for any errors. To assemble your code, save your complete file and then open the Output window (refer to step 2 above). Next, click on the Build toolbar button, which will assemble your program.

Build Current File

7. If your program assembles successfully, you will see the following in the Output window:

8. If you see warnings or errors, the output window will return a warning or error code with a brief description of the issue. All errors must be corrected before you can assemble your program.

(22) refers to the line number the error originated

- 11 -

Lab 2 2.7 Procedure:

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

1. Write an instruction sequence to add 10 to the memory location $00AA and store the result in $AA00. Use the same format used in the instruction examples above (i.e. label, opcode, operand, and comments fields). In the comments field, make sure you explain the instruction in words and do not use the same comment syntax as shown in the instruction examples above. 2. Write an instruction sequence to add with carry the contents of memory locations $0800 and $0900 and store the result in $0A00. 3. Write an instruction sequence to subtract the contents of memory location $0800:$0801 from $0900:$0901 and store the result in $0A00:$0A01. 4. Write an instruction sequence to move the contents of memory location $00FF to $0055. 5. Write an instruction sequence to move the contents of memory locations $0C00:$0C01 to $0D00:$0D01. 6. Using the Lab2AssemblyProgram.asm file you created above, fill in the program instructions that are incomplete (i.e. any fields that contain *OPCODE* and *OPERAND* see comments for the desired operation). Can you tell what the program is supposed to do by reading through the comments? When you are done, assemble your program and correct any errors. Show the TA when you have successfully assembled the program and be prepared to explain what the program is doing. Make sure you keep this file, as it will be used again in Lab 3 next week. 2.8 Questions: 1. Explain the following instruction sequence. Can you think of a faster and better way to complete this operation, given that ($0B00) does not need to be in register A? What addressing mode is being used for both instructions?
ldaa staa $0B00 $0C00 ; ($0B00) -> A ; (A) -> $0C00

2. Copy the following instructions and use the comments field to label the addressing mode that is being used in the instruction. Example:
ldaa staa ldab ldd staa std movb subb adca ldaa $0B00 $0C00 #$00 $0900 1,SP $55 #$09 1,Y 2,X 3,Y+ $0B00 ; Extended (EXT) ; ; ; ; ; ; ; This one is a little tricky ; ;

- 12 -

Lab 2
adcb staa bne

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction


1,-Y [0,X] label ; ; ;

3. Is the following instruction valid? If not, explain any errors and provide a correct version of the instruction.
ldaa -1,X; ((X)) -> A, (X) - 1 -> X

4. Complete the following operation in a single instruction, assuming the contents of register A do not need to be saved in $0C00 (Hint: The instruction is not listed in this lab).
staa ldab $0C00 $0C00 ; (A) -> $0C00 ; ($0C00) -> B

5. Explain two applications where you would want to use the auto pre/post decrement/increment addressing mode. 6. Using the template on the last page of this lab, fill in the registers and memory location contents after the given instruction sequence is executed (assume all registers and memory locations have default values of 0). Attach this completed template to the back of your lab report.

2.9

Lab Report:

Your lab report is due at the beginning of the next lab. For the lab write up, include the following: 1. A brief discussion of the objectives of the lab and the procedures performed in the lab. 2. Answers to all the questions asked in the Procedure and Questions sections. 3. A copy of your assembly program from Procedure 6.

- 13 -

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

Question 6 Template
Question: Using the following template, fill in the registers and memory location contents after the following instruction sequence is executed, assuming all memory locations and registers have default values of 0. Use hexadecimal format (ex: $00) and fill in all memory locations and registers with default values if not changed by the instruction sequence. Instruction Sequence:
LABEL OPCODE ldx movw movb ldaa OPERAND #$1000 #$1005 $1002 #$FF $1005 [2,X] COMMENTS ; ; ; ; $1000 X $1005 $1002:$1003 $FF $1005 (((X) + 2)) A

Index Register X (16-bit) Register A (8-bit) Memory Locations (8-bit) $1000 $1001 $1002 $1003 $1004 $1005 $1006

- 14 -

Lab 2

Addressing Modes, Assembly Fundamentals, and 68HC12 ISA Introduction

Procedure 6 Code
*DATA STORAGE* ************** *LABEL loopCount memoryAddr *MAIN PROGRAM* ************** *LABEL DIRECTIVE org dc.b dc.w VALUE $0800 10 $0900 COMMENT* ; Begin data store at address $0800 ; Define 1 byte of memory for loop count @ $0800 ; Define constant for memory location @ $0801:$0802

clearMem

OPCODE org ldx ldab *OPCODE* *OPCODE* *OPCODE* bne *OPCODE* *OPCODE* *OPCODE* *OPCODE* cmpa bne swi end

OPERAND $0A00 memoryAddr loopCount *OPERAND* *OPERAND* *OPERAND* clearMem *OPERAND* *OPERAND* *OPERAND* *OPERAND* loopCount storeMem

COMMENT* ; Begin program at address $0A00 ; Put starting memory address in X ; Put the loopCount value in B ; Put 0 in register A ; Store value of A at address in X, then increment X by 1 ; Subtract 1 from B ; If B is not equal to zero, go back to clearMem ; ; ; ; ; ; ; ; Put starting memory address in X Put 0 in register A Add 1 to A Store value of A at address in X, then increment X by 1 Compare value of A to the loopCount value If A is not equal to zero, go back to storeMem Halt program execution END OF PROGRAM

storeMem

- 15 -

Anda mungkin juga menyukai