Chapter 2 HCS12 Assembly Language - TTU CAE Network

2y ago
8 Views
2 Downloads
1.75 MB
104 Pages
Last View : Today
Last Download : 3m ago
Upload by : Farrah Jaffe
Transcription

Chapter 2HCS12 Assembly LanguageECE 3120Dr. Mohamed tech.edu

Outline2.1 Assembly language program structure2.2 Data transfer instructions2.3 Arithmetic instructions2.4 Branch and loop instructions2.5 Shift and rotate instructions2.6 Boolean logic instructions2.7 Bit test and manipulate instructions2.8 Stack2.9 Subroutines

1- Assembler directives- Commands to the assembler- Are not executed by the microcontroller – are not converted tomachine codes- Define program constants and reserve space for variable2-1

1. Org (origin)- Tells the assembler where to place the next instruction/data inmemory- Example:org 1000ldab # FF ;this instruction will be stored inmemory starting from location 1000.2. dc.b (define constant byte), db (define byte), fcb (formconstant byte)- Define the value of a byte or bytes that will be placed at a givenlocation.- Example:org 800array dc.b 11, 22, 33, 44800801802803 11 22 33 442-2

3. dc.w (define constant word), dw (define word), fdb (formdouble bytes)- Define the value of a word or words that will be placed at agiven location.- For example:arrayorg 800dc.w AC11, F122, 33, F44800801802803804805806807 AC 11 F1 22 00 33 0F 444. fcc (form constant character)- Tells the assembler to store a string of characters (a message) inmemory.- The last character must be the same as the first character.- The first and last characters are used as delimiters.- Each character is represented by its ASCII code.2-3

- For example:msgOrg 1000Alpha fcc “def”fcc“Please enter your name:”100010011002 64 65 66- Assembler will convert to Ascii5. fill- Example:Org 800fill 20, 40; fill 40 bytes with 20 starting from the memory location 800.6- ds (define storage), rmb (reserve memory byte), ds.b (definestorage bytes)- Reserves a number of bytes for later use.- Example: buffer ds 100; reserves 100 bytes starting from the location represented by buffer none of these locations is initialized2-4

2-5

7. ds.w (define storage word), rmw (reserve memory word)- Reserve a number of wordsDbuf ds.w 20 ;Reserves 20 words (or 40 bytes) startingfrom the current location counter.8. equ (equate)- Assigns a value to a label.- Makes programs more readable.- Examples:motor speed equ 50The assembler will replace motor speed with the value 50 in thewhole program2-6

Example 1: Array of bytesExample 2: Array of wordsa1a1a2a2a1 800a2 804a3 807a3a380D80E032-7

2- InstructionsA line of an assembly programLabel field- Labels are used to identify memory locations in the programsand data areas.- Optional- Must starts with a letter (A-Z or a-z) and can be followed byletters, digits, or special symbols ( or .)2-8

Label field- Can start from any column if ended with “:”- Must start from column 1 if it is not ended with “:”- Example: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 “:” whenreferring to a label2-9

Comment field- Optional- Explain the function of a single or a group of instructions- For programmer – not for assembler or processor.- Ignored by assembler and are not converted to machine code.- Can improve a program readability - very important in assembly- Any line starts with an * or ; is a comment- Separated from the operand for at least one space2 - 10

InstructionsInstructions- Instruct the processor to do a sequence of operations- Converted to machine code- Operands follow the opcode and is separated from the opcode by atleast 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 onespace2 - 11- Assembler instructions or directives are not case sensitive- Must not start at column 1

3- Addressing modes- Addressing modes specify the operand to be operated on.- The addressing mode may specify an immediate value, a register, ora memory location to be used as an operand.The Basic Addressing Modes1. Inherent5. Relative2. Immediate6. Indexed3. Direct7. Indexed-Indirect4. Extended2 - 12

1- Inherent Mode- Either do not need operands or all operands are CPU registers.The instruction has only an opcode.- Operands can be detected from the opcode.- Examples:INXCLRAABA;Increment X; clear A;A A B2- Immediate Mode- Operands’ values are included in the instruction. The values arefetched from the machine code in the memory.- An immediate value is preceded by # character2 - 13

- Example:LDAA # 55; A 55LDX;X 1000# 1000movw # 10, 100; m[ 100] 00 and m[ 101] 10;Store the hex values 00 and 10 in; the memory locations at 100 and 1013- Direct Mode- The operand is a memory location in the range of 00 to FF.Examples:LDAA 20LDAB 40LDX 20; A [ 20] A the value at memory location 0020; B [ 40]; XH [ 20] XL [ 21]2 - 14

What is the difference between:ldaa 45; A the content of memory location 45ldaa # 45 ; A 454- Extended Mode- Same as Direct mode but with using 16-bit memory address.- Used to access any location in the 64 kB memory from 0000 to FFFFLDAA 4000LDX FE60; A [ 4000]; X [ FE60]:[ FE61] places the firstbyte in the high-order byte2 - 15

5- Relative Mode- Used only by branch instructions.- If a branch is taken PC PC offset- A short branch instruction: offset is a signed 8-bit can specify arange of -128 127.- A long branch instruction: offset is a signed 16-bit can specify arange of -32768 32767.- A programmer uses a label to specify the branch target and theassembler will figure out the offset and add it to the machineinstruction.- Example:minus bmiminus;go to minus if N flag in CCR register 12 - 16

6- Indexed Mode- The operand is a memory location.6.1 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; Store B at memory location X – 8ldd 100,Y; A [100 [Y]], B [101 [Y]]2 - 17

6.2 Indexed with an accumulator register offset- The operand address accumulator base index register.- The accumulator can be A, B, or D- The base index register can be X, Y, SP, or PC.- Examples:ldaa B,Xldy D,X;load A with the content of memory location X B; Y memory locations D X and D X 16.3 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. It is in the ranges -8thru -1 or 1 thru 8.- Post-decrement/increment: Operand address old r- Pre-decrement/increment: Operand address new r2 - 18

Examples: Assume X has the value 1000Pre-decrement (n,-r)Post-decrement (n,r-)ldaa 2,-Xldaa 2,X-X 1000 – 2 FFEA [ 1000]A [ FFE]X 1000 – 2 FFEPre-increment (n, r)Post-increment (n,r )ldaa 2, Xldaa 2,X X 1000 2 1002A [ 1000]A [ 1002]X 1000 2 1002Can be used to read an array.ldaa 1,X ; A [1000]ldaa 1,X ; A [1001]ldaa 1,X ; A [1002]2 - 19

6.4 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)6.4.1 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 the16-bit constant offset indexing.ldaa [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 20002 - 20- A the value stored in location 2000

Example 3Aldaa # 10 A 10ldaa 10,X A [X 10] 20ldy 10,X Y [X 10]: [X 11] 2000ldaa [10,X] A [[X 10]] [2000] F02 - 21ldy [10,X] y [[X 10]]: [[X 11]] [2000]: [2001] F03A

6.4.2 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 rThe possible addressing modes of each instruction are given in theinstruction set file2 - 22

Summary of important addressing modesMachine code2 - 23

4- Software Development Process1. Problem definition: Identify what should be done2. Identify the inputs and outputs3. Develop the algorithm (or a flowchart):- The overall plan for solving the problem at hand.- A sequence of operations that transform inputs to output expressedin the following format (pseudo code):Step 1: Step i: read a value and store in variable X N X 5 4. Programming: Convert the algorithm into programs.5. Program Testing:- Testing for anomalies.- Test for the max. and min. values of inputs- Enter values that can test all branches2 - 24

Outline2.1 Assembly language program structure2.2 Data transfer instructions2.3 Arithmetic instructions2.4 Branch and loop instructions2.5 Shift and rotate instructions2.6 Boolean logic instructions2.7 Bit test and manipulate instructions2.8 Stack2.9 Subroutines

Load and Store / Move / TransferLoad: Copies the contents of a memory location (or immediate value)into a register. The memory location does not change but theregister changes.Store: Copies the contents of a register into a memory location. Theregister does not change but the memory location changes.Move: Copies the content of a memory location into other memorylocation.Transfer: Copies the content of a register into another register.2 - 25

What to keep in mind to learn how the instructions work- How does the instruction affect registers and/or memory?- How does the instruction affect the flags?- Is it clear where the input numbers are and where the results(destination) should go? 8 or 16 bits?- The instruction is for signed or unsigned numbers?- What kind of addressing modes are available?1- The LOAD and STORE Instructions- The LOAD instruction copies the content of a memory location oran immediate value to an accumulator or a CPU register.- Memory contents are not changed.- STORE instructions copies a CPU register into a memorylocation. The register contents are not changed- N and Z flags of the CCR register are automatically updated, theV flag is cleared, and C does not change.2 - 26

2 - 27

- All except for the relative mode can be used to select thememory location.- Examples:ldaa 0,X;A the content of the memory location pointed by X 0staa 20 ;Store the content of A in the memory location 20stx 8000 ;Store the content of register X in memory location at 8000 and 8001ldd #100ldab 1004;d 0100;B the content of 1004ldx # 6000 ;X 6000 this can be the beginning address of anarrayldd 0,X;read the fist two bytes in the arrayldd 2,X;read the second two bytes in the arrayleax 2,X; X the address (not the content) X 2leay d,y;Y D Y2 - 28

2- Transfer Instructions- Copy the contents of a register into another register. Sourceregister is not changed- TAB copies A to B and TBA copies B to A- TAB and TBA change N and Z, V 0, and does not change C.-The TFR instruction does not affect any condition code bits.- For example:TFR D,X;X DTFR A,B;B ATFR X,A; X[7:0] A, lower 8 bits of X are copied to ATFR A,X; A is extended to an unsigned 16-bit numberand stored in X. X 00: contents of A- When transferring 8-bit register to 16-bit register, the content ofthe 8-bit register is extended as unsigned 16-bit number by2 - 29adding zeroes on left.

The exchange instruction- The EXG instruction swaps the contents of a pair of registers.The contents of the two registers change.- For example:exg A,B ; if A 1 and B 2, after executing the instructionA 2 and B 1exg D,XUnsigned Exchange 8exg A,X; A X[7:0], X 00:[A]bits register with 16exg X,B; X 00:[B], B X[7:0]bits one- Signed Exchange:SEX A,X- A X[0:7] the lowest 8 bits in X- X 00:[A] if the number in A is positive- X FF:[A] if the number in A is negative2 - 30- To extend 8-bit positive number to 16 bits, add zeroes on the left- To extend 8-bit negative number to 16 bits, add ones on the left

2 - 31

3- Move Instructions- Copy a byte or a word from a memory location (or immediatevalue) to other memory location- Do not change all the flags.- Example:movb 1000, 2000; Copies the byte at memory location 1000to the memory location at 2000movw 0,X,0,Y; Copy 16 bit word pointed by X 0 to thelocation pointed by Y 0movb #3A, 0F; the memory location 0F 3A2 - 32

Outline2.1 Assembly language program structure2.2 Data transfer instructions2.3 Arithmetic instructions2.4 Branch and loop instructions2.5 Shift and rotate instructions2.6 Boolean logic instructions2.7 Bit test and manipulate instructions2.8 Stack2.9 Subroutines

1- Add and Subtract Instructions- The destinations are always a CPU register.- Update N, Z, N, and C flags.- Examples:adda 1000; A [A] [ 1000]adca 1000; A [A] [ 1000] Csuba 1002; A [A] - [ 1002]sbca 1000; A [A] - [ 1000] – C(A - B) is done by adding A to the two’s complement of B2 - 33

opr field is specified using one of the addressing modes.All addressing modes except inherent and relative can be used2 - 34

- Zero flag (Z): set when the result is zero- Negative flag (N): set whenever the result is negative, i.e., mostsignificant bit of the result is 1.- Carry/borrow flag (C): set when addition/subtraction generates acarry/borrow.- Overflow flag (V): Set when:the addition of two positive numbers results in anegative numberorthe addition of two negative numbers results in apositive number.2 - 35

OverflowProblem: fixed width registers have limited rangeOverflow occurs when two numbers are added or subtracted and thecorrect result is outside the range that can a register hold thegiven result is not correctAddition:C 1 there is an overflow if the numbers are unsigned.V 1 there is an overflow if the numbers are signed.Overflow cannot occur when adding numbers of opposite sign why?Subtraction: A - BThere is no unsigned overflow but there is signed overflowC 1, when there is a borrow or B A, C is called borrow flagV 1, when(-ve) - ( ve) ( ve) this is equivalent to (–ve) (-ve) ( ve)( ve) - (-ve) (-ve) this is equivalent to ( ve) ( ve) (-ve)2 - 36

1111 1111 0000 00011C 1, V 0, Z 1, N 00000 0000Signed numbers: -1 1 0 , no overflow and the result is correctUnsigned numbers: 255 1 0, incorrect, the correct result is 256, overflowbecause the max. unsigned number for 8 bit number is 2550 1111 1111 0 0000 00011 0000 00001010 1010 0101 0101By using more bits (9 bits or more) instead of 8 bits,the result is correct, no overflowC 0, V 0, Z 0, N 11111 1111Signed numbers: -86 85 -1 , no overflow and the result is correctUnsigned numbers: 170 85 255, no overflow and the result is correct2 - 37

0111 1111 0000 0001C 0, V 1, Z 0, N 11000 0000Unsigned numbers: 127 1 128, no overflow and the result is correctSigned numbers: 127 1 -128 , there is overflow, the result is incorrect, themax. positive number in 8 bits is 127 that is less than thecorrect answer 128. If we use 9 bit addition, the result willbe correct because 128 can be represented in 9 bits.0 0111 1111 0 0000 0001C 0, V 0, Z 0, N 00 1000 00001010 1100 1000 1010C 1, V 1, Z 0, N 01 0011 01102 - 38Unsigned numbers: 172 138 54 should be 310, overflow, 255 is the max.number for 8 bit numberSigned numbers: -84 (-118) 54, should be -202, overflow, the max. negativenumber in 8 bits is -128 that is less than the correct answer

If we use 9 bit addition, the result will be correct.Unsigned numbers:0 1010 1100 0 1000 1010C 01 0011 0110Signed numbers:1 1010 1100 1 1000 1010V 01 0011 0110Subtraction: A – B A the two’s complement of B0111 1010- 0101 11000111 1010 1010 0100V 0, C 0, N 0, Z 0C is called borrow flag and it is set when weneed to borrow from the most significant byte1 0001 1110Unsigned numbers: 122 - 92 30 correctSigned numbers: 122 - 92 30 correctV 0 because ( ve) – ( ve) no overflow2 - 39

0101 1100- 1000 1010V 1, C 1, N 1, Z 00101 1100 0111 01101101 0010Unsigned numbers:- There is borrow, 92 – 138 210- What happened is (92 256) -138 210, where 256 is the borrow- If we do muti-byte subtraction, the result (210) is right and we shouldsubtract one from the next byte.- If you want to get the absolute difference 46, subtract the small numberfrom the bigger one or 1000 1010 - 0101 1100 46Signed numbers:92 – (-118) -46 should be 210, V 1 means the numbers should berepresented in more bits0 0101 1100- 1 1000 10100 0101 1100 0 0111 01100 1101 0010V 02 - 40

Write a code to subtract the contents of the memory location at 1005from the sum of the memory locations at 1000 and 1002, and storethe difference at 1100.ldaaaddasubastaa 1000 1002 1005 1100;;;;A [ 1000]A A [ 1002]A A – [ 1005][ 1100] AWrite a code to add the byte pointed by register X and the followingbyte and store the sum at the memory location pointed by register Y.ldaaaddastaa0,X1,X0,Y; store the byte pointed by X in A; add the following byte to A; store the sum at location pointed by YWrite a code to swap the 2 bytes at 100 and 200. 1002copycopy1 2003Acopy2 - 41

ldaamovbstaa 100; A [ 100] 200, 100 ; store [ 200] in memory location 100 200 ; store A in the memory location 200Write a code to add 3 to the memory locations at 10 and 15.A memory location cannot be the destination in ADD instructions.We need to copy the memory content into register A or B, add 3 to it,and then return the sum back to the same memory location.ldaaaddastaa 10#3 10; copy the contents of memory location 10 to A; add 3 to A; store the sum to memory location at 10ldaaaddastaa 15#3 15; copy the contents of memory location 15 to A; add 3 to A; store the sum to memory location at 152 - 42

Multi-precision arithmetic- HCS12 can add/sub at most 16-bit numbers using one instruction- To add/sub numbers that are larger than 16 bits, we need to consider thecarry or borrow resulted from 16-bit operation.How to add (or subtract) two 32-bit numbersMost significant byteC8-bit numberAdd with carry(or sub with borrow)8-bit numberC8-bit numberAdd with carry(or Sub with borrow)8-bit numberLeast significant word16-bit numberAdd (or sub)16-bit number- Carry flag is set to 1 when the addition operation produces a carry. Thiscarry should be added to the next addition operation2 - 43- Carry flag is set to 1 when the subtraction operation produces a borrow.This borrow should be subtracted from the next subtraction operation

Write a program to add two 4-byte numbers that are stored at 1000 1003 and 1004- 1007, and store the sum at 1010- 1013.The addition starts from the lease significant byte and proceeds toward themost significant number.carrycarry[ 1000]Add with carry[ 1001]Add with carry[ 1002][ 1003]add[ 1004][ 1005][ 1006][ 1007][ 1010][ 1011][ 1012][ 1013]Notice there is no instruction for addition with carry for 16 bits.2 - 44

; Add and save the least significant two bytesldd 1002; D [ 1002]:[ 1003]addd 1006; D [D] [ 1006]:[ 1007]std 1012; [ 1012]:[ 1013] [D]C; Add and save the second most significant bytesldaa 1001; A [ 1001]adca 1005; A [A] [ 1005] Cstaa 1011; 1011 [A]; Add and save the most significant bytesldaa 1000; A [ 1000]adca 1004; A [A] [ 1004] Cstaa 1010; 1010 [A]std and ldaa do not changethe carry so C is the carryresulted from addd 1006For subtraction: The same code can be used but usesubd instead of adddsbca instead of adca2 - 45

2- Decrementing and incrementing instructionsThese instructions are faster than using Add/sub instructions.ldaa 10adda #1staa 10Updated flags opr NZV Noneinc 10Z opr NZVNoneZ opr can be direct, extended, or indexed addressing modes.2 - 46

3- Clear, Complement and Negate instructionsUpdated flagsC 0I 0NZVC 0100V 0NZVC YY01NZVC YYYY- Clear command stores 0 in registers/memory locations. Used forinitialization.- Complement command computes the one’s complement.- Negate command computes the two’s complement.2 - 47

4- Multiplication and Division instructionsThe upper 16 bits in Yand the lower ones in DNZVC YYNYNZVC NNNYNZVC YYYYNZVC NYYYNZVC NY0YNZVC YYYY2 - 48

Write an instruction sequence to multiply the 16-bit numbers stored at 1000- 1001 and 1002- 1003 and store the product at 1100- 1103.lddldyemulstystd;load first word;load second word;[D] x [Y] Y:D use emuls if the numbers are signed 1100; store most significant 16 bits in 1100 and 1101 1102; store least significant 16 bits in 1102 and 1103 1000 1002Write an instruction sequence to divide the signed 16-bit number stored at 1020- 1021 by the signed 16-bit number stored at 1005- 1006 and storethe quotient and remainder at 1100 and 1102, respectively.ldd 1005ldx 1020idivsstxstd 1100 1102; D/XX qutient, D remainder, use idiv if numbers areunsigned; store the quotient (16 bits) at 1100 and 1101; store the remainder (16 bits);To compute the squared value of Atab;B Amul;A:B A x B2 - 49

Converting binary number to decimal11000000111001 ( 12345 in decimal)Input: Binary numberBinary to decimalconversionOutput: equivalent decimal numberBinary to decimalconversion12345Can be sent to the LCD2 - 50

- Using repeated division by 10.- The largest 16-bit number is 65,535 which has five decimal digits.- The first division by 10 generates the least significant digit (in theremainder).QuotientRemainder1234510 1234123410 123412310 1231210 12 011105Least significantMost significant2 - 51

Write a program to convert the 16-bit number stored at register D todecimal store the result at memory locations 1010 to 1014.ldy # 1010ldx #10idivstab 4,Yxgdxldx #10idivstab 3,Yxgdx;Y points at the first address of the decimal result;X 10;D/XQuotient X, Remainder D;save the least significant digit (5); D the quotient required for the next division; X 10; D/10 Quotient X, Remainder D;save the second number (4); D the quotient required for theAssume: D 12345Y 1010 1011next division 1012 1013Y 4 1014123452 - 52

ldx #10idivstab 2,Yxgdx; X 10; D/10 Quotient X, Remainder D;save the third number (3); D the quotientldx #10idivstab 1,Yxgdx; X 10; D/10 Quotient X, Remainder D;save the second number (2); D the quotientaddb # 30stab 0,Y ; save the most significant digit (1)Y 1010 1011 1012 1013Y 4 1014123452 - 53

Converting decimal number to binaryMost significantnumberInput: decimal numberDecimal to binaryconversionN1 1N2 2N3 3N4 4N5 5Can come from thekeypadDecimal to binaryconversionOutput: equivalent binary number11000000111001 ( 12345 in decimal)2 - 54

Binary ((((N1x 10) N2) x 10 N3) x 10 N4) x 10 N512345678 N1 x 10000 N2 x 1000 N3 x 100 N4 x 10 N5Write a program to convert a 5-digit number stored at memorylocations 1010 to 1014 into its 16-bit equivalent binary number.Store the result in memory locations 2000 and 2001.; processing N1ldaa 1010; A N1 (the most significant digit)ldab #10; B 10mul; operation 1 D N1 x 10std 2000; store the result in 2000 and 2001 1010 1011 1012 1013; processing N2 1014ldab 1011; B N2clra; A 0 so D A:B 00: N2addd 2000;operation 2 d [ 2000] D (N1 x 10) N2123452 - 55

; process N3ldy #10; Y 10emul; Y:D D x Y operation 3std 2000;d ((N1 x 10) N2) x 10ldab 1012; B N3clra; A 0 so D A:B 00: N3addd 2000;operation 4 d ((N1 x 10) N2) x 10 N3; process N4ldy #10; Y 10emul; Y:D D x Y operation 5std 2000;d (((N1 x 10) N2) x 10 N3) x 10ldab 1013; B N3clra; A 0 so D A:B 00: N4addd 2000;operation 6 d ((((N1 x 10) N2) x 10 N3)x 10) N42 - 56

; process N5ldy #10; Y 10emul; Y:D D x Y operation 7std 2000;d d (((((N1 x 10) N2) x 10 N3) x 10) N4)x 10ldab 1014; B N5clra; A 0 so D A:B 00: N5addd 2000;operation 8 d ((((((N1 x 10) N2) x 10 N3) x10) N4)x 10 ) N52 - 57

Outline2.1 Assembly language program structure2.2 Data transfer instructions2.3 Arithmetic instructions2.4 Branch and loop instructions2.5 Shift and rotate instructions2.6 Boolean logic instructions2.7 Bit test and manipulate instructions2.8 Stack2.9 Subroutines

1. Branch instructionsConditional orunconditionalShort or longSigned orunsignedUnconditional branches- Branches are always takenConditional branches- A branch is taken if a condition is satisfied.- A condition is satisfied if certain flags are set.- Usually we use a comparison or arithmetic command to set up theflags before the branch instruction.CBALBHI next; compare A to B - used to set the flags; branch to next if A B – LBHI tests the flags2 - 58

1. Branch instructionsConditional orunconditionalShort or longShort branches- The range of the branch is -128 and 127 bytes.Signed orunsignedNext1:------------------------------BRA Next1BRA Next2-------------------------------Long branches- Can branch to anywhere in the memory 128 127Next2:For peace of mind, always use long branches2 - 59

1. Branch instructionsConditional orunconditionalShort or longSigned orunsignedUnsigned branches- The numbers of the condition are unsigned- Use instructions: branch if higher (LBHI), branch if higher or same(LBHS), branch if lower (LBLO), and branch if lower and same (LBLS).Signed branches- The numbers of the condition are Signed- Use instructions: branch if greater (LBGT), branch if greater or equal(LBGE), branch if less (LBLE), and branch if less and equal (LBLE).; A 1111 1111 B 0000 0001CPA ; compare A and B . Used to set the flagsLBHI next ; unsigned the branch is taken because A 225 B 1LBGT next ; Signed the branch is not taken because A -1 is notgreater than B 12 - 60

Unconditionalbranchbranch is takenwhen a specific flagis 0 or 12 - 61

2. Compare and Test instructions- Flags should be set up before using conditional branch instructions.- The compare and test instructions perform subtraction, set the flagsbased on the result, and does not store the result. ONLY change flags.The memory and registerdoes not change opr can be an immediatevalue or a memory location2 - 62

3. Loop instructions- Repeat a sequence of instructions several times.- Either decrement or increment a count to determine if the loop shouldcontinue.- The range of the branch is from -128 to 127.Note: rel is the relative branch offset and usually a label2 - 63

4. Bit condition branch instructions- Make branch decision based on the value of few bits in a memory location.brclr opr ,msk,rel ;Branch is taken when the tested bits are zeroesbrset opr ,msk,rel ;Branch is taken when the tested bits are ones opr : The memory location to be checked.msk: 8 bits that specifies the bits of to be checked. The bits to be checkedcorrespond to those that are 1s in msk.rel : if a branch is taken, it branches to the label relloop: brset 66, E0,loopbrclr 66, 80,here here: The branch is taken if the last three bits atmemory location 66 are all ones.Notice: E0 %1110 0000The branch is taken if the most significantbit at the memory location 66 is zero.Notice: 80 %1000 00002 - 64

How brclr and brset work?1 and Bi Bi put 1 at the bits you test0 and Bi 0 put 0 at the bits you do not testI wanna test These bitsB7B6B5B4B3B2B1B0 opr 00000011mask000000B1B0ANDThis number is zero ifB0 and B1 are zeros,otherwise it is not zero2 - 65

Looping mechanisms1. Endless loopdo a sequence of instructions (S) forever.Loop: ---------------LBRA Loop2. For loopsRepeat theseinstructions foreverFor (i n1, i n2, i ){a sequence of instructions (S) }- i is loop counter that can be incremented in each iteration.- Sequence S is repeated n2-n1 1 timesSteps:1- Initialize loop counter2- Compare the loop counter with n2. If it is not equal, do the loopotherwise exit3- increment the loop and go to step 22 - 66

Implementation of for (i 1, i 20, i ) {S}; i is the loop counteri ds.b1movb#1,i;initialize i to 1ldaaicmpa#2LBHI Next; A [i]; check index i; if i n2, exit the loopLoop: inc ilbra LoopNext:12?; performs S;“;increment loop index;go back to the loop body Since i is a byte, the max. number of iterations is 256.For more iterations, use two loops (outer and inner loops)2 - 67

For loop using dbequp to 65,535 iterationsldx #6000; number ofiterationsLoopf: dbeq x,next .; performs S . lbra Loopfnext: May be a good idea to use a memory location as a loop counterbecause you may need to use X to perform the sequence S2 - 68

While LoopWhile (condition) { Sequence S; }- S is executed as long as the condition is true- Unlike for loop, the number of iterations may not be knownbeforehandWhile (A 0) {Sequence S;}Wloop: cmpa #0lbeq Next . ; perform S .lbra WloopNext: A is updated in the instruction sequence S2 - 69

If (I 1){Sequence S;}I ds.b 1 I ds.b 1ldaa I ; A Icmpa #1lbne elseldaa Icmpa #1lbne end if . .If ( I 1) {Sequence S1;}else {Sequence S2;}; perform S; ”end if: ; perform S1; “lbra end ifelse: . .; perform S2;“end if:If I does not equal 1, skip theSequence S2 - 70

If ( A 1 and B 8){Sequence S;}cmpa #1lbne end if; the first condition issatisfied test the second onecmpb #8lbls end if . .end if:; perform S; ”If ( A 1 or B 8){Sequence S;}cmpa #1lbeq perform S; the first condition is not satisfied. Try thesecond onecmpb 8lbhi perform S;the two conditions are not satisfied, go to end iflbra end ifperform S: . .; perform S; ”end if:Sequence S is executed onlywhen the two conditions aresatisfied, i.e., if one condition isnot satisfied, do not execute SSequence S should be e

HCS12 Assembly Language ECE 3120. Outline 2.1 Assembly language program structure 2.2 Data transfer instructions 2.3 Arithmetic instructions 2.4 Branch and loop instructions 2.5 Shift and rotate instructions . Step 1: read a value and store in variable X .

Related Documents:

Part One: Heir of Ash Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Chapter 19 Chapter 20 Chapter 21 Chapter 22 Chapter 23 Chapter 24 Chapter 25 Chapter 26 Chapter 27 Chapter 28 Chapter 29 Chapter 30 .

Online Instructor’s Manual . to accompany . HCS12 Microcontroller and Embedded Systems: Using Assembly and C with CodeWarrior . 1. st. Edition . Muhammad Ali Mazidi . Danny Causey . Prentice Hall . Boston Columbus Indianapolis New York San Francisco Upper Saddle River

TO KILL A MOCKINGBIRD. Contents Dedication Epigraph Part One Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Part Two Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18. Chapter 19 Chapter 20 Chapter 21 Chapter 22 Chapter 23 Chapter 24 Chapter 25 Chapter 26

DEDICATION PART ONE Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 PART TWO Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Chapter 19 Chapter 20 Chapter 21 Chapter 22 Chapter 23 .

Version Specific Information for HCS12 and HCS12X 109 3.5. Code Generation and Usage 111 3.5.1. Code Generation 112 . Processor Expert User Manual Introduction. Figure 1.1 -CodeWarrior IDE with Processor Expert active . PE based tool solution offers the following advantages to Freescale CPU customers:

About the husband’s secret. Dedication Epigraph Pandora Monday Chapter One Chapter Two Chapter Three Chapter Four Chapter Five Tuesday Chapter Six Chapter Seven. Chapter Eight Chapter Nine Chapter Ten Chapter Eleven Chapter Twelve Chapter Thirteen Chapter Fourteen Chapter Fifteen Chapter Sixteen Chapter Seventeen Chapter Eighteen

18.4 35 18.5 35 I Solutions to Applying the Concepts Questions II Answers to End-of-chapter Conceptual Questions Chapter 1 37 Chapter 2 38 Chapter 3 39 Chapter 4 40 Chapter 5 43 Chapter 6 45 Chapter 7 46 Chapter 8 47 Chapter 9 50 Chapter 10 52 Chapter 11 55 Chapter 12 56 Chapter 13 57 Chapter 14 61 Chapter 15 62 Chapter 16 63 Chapter 17 65 .

This textbook is designed for use on ten- or twelve-week introductory courses on English phonology of the sort taught in the first year of many English Language and Linguistics degrees, in British and American universities. Students on such courses can struggle with phonetics and phonology; it is sometimes difficult to see past the new .