Transcription of Chapter 2 HCS12 Assembly Language - TTU CAE Network
1 Dr. Mohamed 2 HCS12 Assembly LanguageECE Assembly Language program Data transfer Arithmetic Branch and loop Shift and rotate Boolean logic Bit test and manipulate Subroutines 2 - 1- Commands to the assembler- Are not executed by the microcontroller are not converted to machine codes- Define program constants and reserve space for variable1- Assembler directives 1. Org (origin)- Tells the assembler where to place the next instruction/data in memory-Example: org $1000ldab #$FF ;this instruction will be stored inmemory starting from location $ (define constant byte), db (define byte), fcb (form constant byte)- Define the value of a byte or bytes that will be placed at a given location.
2 -Example:org $800array $11,$22,$33,$44$11$22$33$448008018028032 - 23. (define constant word), dw (define word), fdb (form double bytes)- Define the value of a word or words that will be placed at a given For example:org $800array $AC11,$F122,$33,$F44$11$22$33$44$0F80080 1802803$F1$00$AC8048058068074. fcc (form constant character)- Tells the assembler to store a string of characters (a message) in The last character must be the same as the first The first and last characters are used as Each character is represented by its ASCII - 3- For example: msgfcc Please enter your name: Org $1000 Alpha fcc def - Assembler will convert to Ascii$64$65$661000100110025.
3 Fill- Example: Org $800fill $20, 40 ; fill 40 bytes with $20 starting from the memory location $ ds (define storage), rmb (reserve memory byte), (define storage bytes)- Reserves a number of bytes for later Example: buffer ds 100; reserves 100 bytes starting from the location represented by buffer -none of these locations is initialized 2 - 42 - 57. (define storage word), rmw (reserve memory word)- Reserve a number of wordsDbuf 20 ;Reserves 20 words (or 40 bytes) starting from the current location equ (equate)- Assigns a value to a Makes programs more :motor_speed equ 50 The assembler will replace motor_speed with the value 50 in the whole program2 - 6 Example 1: Array of bytesExample 2: Array of words2 - 7a1a2a3a2a1a30380D80Ea1 = $800a2 = $804a3 = $8072 - 8A line of an Assembly programLabel field- Labels are used to identify memory locations in the programs and data areas.
4 -Optional- Must starts with a letter (A-Z or a-z) and can be followed by letters, digits, or special symbols (_ or .)2- InstructionsLabel field- Can start from any column if ended with : - Must start from column 1 if it is not ended with : -Example:2 - 9 Begin: ldaa #10 ; Begin is a valid labelPrint jsr hexout; jump to hexout subroutine, Print is a valid labeljmp begin ; jump to begin label, do not put : when referring to a label-Optional- Explain the function of a single or a group of instructions- For programmer not for assembler or Ignored by assembler and are not converted to machine improve a program readability - very important in Assembly - Any line starts with an *or.
5 Is a comment- Separated from the operand for at least one spaceComment field2 - 10- Instruct the processor to do a sequence of operations- Converted to machine code- Operands follow the opcode and is separated from the opcode by at least one space- Operands are separated by commas (if there is more than one operand)- Opcode is the operation and separated from the label by at least one space- Assembler instructions or directives are not case sensitive-Must notstart at column 1 InstructionsInstructions2 - 11-Addressing modes specify the operand to be operated The addressing mode may specify an immediate value, a register, ora memory location to be used as an Basic Addressing Modes1.
6 Inherent2. Immediate3. Direct4. Extended5. Relative6. Indexed7. Indexed-Indirect2 - 123- Addressing modes1- Inherent Mode- Either do not need operands or all operands are CPU instruction has only an Operands can be detected from the :-INX;Increment XCLRA; clear AABA;A=A+B2- Immediate Mode- Operands values are included in the instruction. The values are fetched from the machine code in the An immediate value is preceded by # character2 - 13- Example:LDAA #$55 ; A $55 LDX #$1000 ;X $1000movw #$10, $100 ; m[$100] $00 and m[$101] $10;Store the hex values $00 and $10 in ; the memory locations at $100 and $101 3- Direct Mode- The operand is a memory location in the range of $00 to $ - 14 Examples:LDAA $20 ; A [$20] A = the value at memory location $0020 LDAB $40 ; B [$40]LDX $20.
7 XH [$20] XL [$21]4- Extended Mode- Same as Direct mode but with using 16-bit memory Used to access any location in the 64 kB memory from 0000 to FFFFLDAA $4000 ; A [$4000]LDX $FE60; X [$FE60]:[$FE61] places the firstbyte in the high-order byteWhat is the difference between: ldaa $45 ; A = the content of memory location 45ldaa #$45 ; A = 452 - 15- A programmer uses a label to specify the branch target and theassembler will figure out the offset and add it to the ;go to minus if N flag in CCR register= 12 - 165- Relative Mode- Used only by branch If a branch is taken PC = PC + offset- A short branch instruction: offset is a signed 8-bit can specify arange of -128 ~ + A long branch instruction: offset is a signed 16-bit can specify arange of -32768 ~ + Indexed with constant offset- The address of the operand = a base register + a constant offset- The base register can be (X, Y, PC, or SP)Examplesldaa 4,X ; A [4+[X]]Load A with the content of the memory location at address X+4ldaa 0,X ; A = [0 + [X]]stab -8,X.
8 Store B at memory location X 8ldd 100,Y ; A = [100+[Y]], B = [101+[Y]]6- Indexed Mode2 - 17- The operand is a memory - Indexed with an accumulator register offset- The operand address = accumulator + base index The accumulator can be A, B, or D- The base index register can be X, Y, SP, or : ldaa B,X ;load A with the content of memory location X+Bldy D,X ; Y = memory locations D + X and D + X + Indexed with auto pre-/post-increment/decrement ofindex register- The base register r can be X, Y, or SP. (No PC)- New r = old r + (or -) n - n is the amount of decrement or increment.
9 It is in the ranges -8 thru -1 or 1 thru Post-decrement/increment: Operand address = old r- Pre-decrement/increment: Operand address = new r2 - 19 Examples: Assume X has the value $1000ldaa 2,-XX = $1000 2 = $FFEA = [$FFE]Pre-decrement (n,-r)ldaa 2,X-A = [$1000]X = 1000 2 = $FFEPost-decrement (n,r-)ldaa 2,+XX = 1000 + 2 = $1002A = [$1002]Pre-increment (n,+r)ldaa 2,X+A = [$1000]X = 1000 + 2 = $1002 Post-increment (n,r+)Can be used to read an 1,X+ ; A = [1000]ldaa 1,X+ ; A = [1001] ldaa 1,X+ ; A = [1002] Indexed-Indirect- The sum of the base register and offset does not point at theoperand address but to the address of memory location where theoperand address can be found (address of address)
10 16-bit Offset Indirect Indexed Addressing- Syntax of the addressing mode is [n,r]- n is 16 bit offset- r is base register X, Y, SP, PC- The operand address = the content of the memory location at n + r- The square brackets distinguish this addressing mode from the 16-bit constant offset [10, X]- If X = $1000, then X + 10 = $100A- It reads 16 bits in the locations $100A and next one $100B. Assume this value is $2000- A = the value stored in location $20002 - 20ldaa #$10 A=10ldaa 10,X A = [X+10] = 20ldy 10,X Y = [X+10]: [X+11] = 2000ldaa [10,X] A = [[X+10]] = [2000] = $F0ldy [10,X] y = [[X+10]]: [[X+11]] = [2000]: [2001] = F03 AExample2 - 21$ Accumulator D Indirect Indexed Addressing- The syntax of this addressing mode is [D,r]- r is base register, X, Y, SP, or PC- The operand address is stored in the memory location D + r2 - 22 The possible addressing modes of each instruction are given in the instruction set file Machine code2 - 23 Summary of important addressing modes1.