250 likes | 355 Views
ME 4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics Instructor: Professor Charles Ume Lecture #10. Relative Addressing. Relative addressing is used for branch instructions only. Three types of branch instructions:
E N D
ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics Instructor: Professor Charles Ume Lecture #10
Relative addressing is used for branch instructions only. Three types of branch instructions: Short Branches – offset signed 8-bit number (-128 to 127) Long Branches – offset signed 16-bit number (-32,768 to 32,767) Bit Condition Branches – offset signed 8-bit number (-128 to 127) Short Branch instructions are 2 bytes in length. First byte contains opcode Second byte contains address offset BEQ Loop 27 offset (I byte) Relative Addressing
Long branch instructions are 4 bytes in length First two bytes contain opcode and postbyte Second byte contains 16-bit signed address offset LBEQ Loop 18 27 MSBLSB offset value Bit condition branch instructions are 4 to 6 bytes in length depending on addressing mode BRSET $0800 #$05 Loop 1E 08 00 05 1 byte offset value BRSET $15, Y #$EA Label 0E 4F EA 1byte offset value Offset is calculated by subtracting contents of Program Counter from destination address. (Note: The Program Counter contains the memory location of the next instruction) If branch condition is true, then add offset to program counter Relative Addressing
ORG $1000 Address Opcode Postbyte Operand ABA $1000 18 06 : : BACK LDAA #$30 $106A 86 30 NEGA $106C 40 ADDA $1A00 $106D BB 1A00 BEQ FRONT $1070 27 ADDA $31 $1072 9B 31 BNE BACK $1074 26 : : FRONT NOP $1080 A7 : : SWI 3F END Example Program for Relative Addressing Mode • Example: #1 Offset ? Offset ?
ORG $1000 Address Opcode Postbyte Operand ABA $1000 18 06 : : BACK LDAA #$30 $106A 86 30 NEGA $106C 40 ADDA $1A00 $106D BB 1A00 BEQ FRONT $1070 27 ADDA $31 $1072 9B 31 BNE BACK $1074 26 : : FRONT NOP $1080 A7 : : SWI 3F END Example 1 • First calculate address offset for forward branch Offset ? Offset ?
Example 1 Branching Forward • Offset is positive if branching forward
ORG $1000 Address Opcode Postbyte Operand ABA $1000 18 06 : : BACK LDAA #$30 $106A 86 30 NEGA $106C 40 ADDA $1A00 $106D BB 1A00 BEQ FRONT 27 ADDA $31 $1072 9B 31 BNE BACK $1074 26 : : FRONT NOP A7 : : SWI 3F END Example 1 • Address of BEQ instruction and Address of where to jump to (NOP) $1070 Offset ? Offset ? $1080
$1070 0001 0000 0111 0000 + $02 0000 0000 0000 0010 $1072 0001 0000 0111 0010 1’s COMP 1110 1111 1000 1101 2’s COMP 1110 1111 1000 1110 +$1080 0001 0000 1000 0000 0000 0000 0000 1110 = $0E Example 1 Branching Forward Continued In Hexadecimal Step1: Add $02 to starting address $1070+$02 = $1072 to determine contents of Program Counter Step2: Subtract result from destination address $1080-$1072 = $0E Address Offset is : $0E
ORG $1000 Address Opcode Postbyte Operand ABA $1000 18 06 : : BACK LDAA #$30 $106A 86 30 NEGA $106C 40 ADDA $1A00 $106D BB 1A00 BEQ FRONT $1070 27 0E ADDA $31 $1072 9B 31 BNE BACK $1074 26 : : FRONT NOP $1080 A7 : : SWI 3F END Example 1 • Calculate address offset for backward branch Offset ?
Example 1 Branching Backward • Offset negative if branching backward
ORG $1000 Address Opcode Postbyte Operand ABA $1000 18 06 : : BACK LDAA #$30 86 30 NEGA $106C 40 ADDA $1A00 $106D BB 1A00 BEQ FRONT $1070 27 ADDA $31 $1072 9B 31 BNE BACK 26 : : FRONT NOP $1080 A7 : : SWI 3F END Example 1 • Address of BNE instruction and Address of where to jump to (LDAA) $106A $1074 Offset ?
$106A-$1076 Note: In Binary $1074 0001 0000 0111 0100 + $02 0000 0000 0000 0010 $1076 0001 0000 0111 0110 1’s COMP 1110 1111 1000 1001 2’s COMP 1110 1111 1000 1010 +$106A 0001 0000 0110 1010 1111 1111 1111 0100 = $F4 Example 1 Branching Forward Continued
Example 1 Branching Backward Continued In Hexadecimal Step1: Add $02 to starting address $1074+$02 = $1076 to determine contents of Program Counter Step2: Subtract result from destination address $106A-$1076 But result will be negative so: Step 3: Switch order and subtract $1076-$106A = $0C Step 4: Take 1’s complement $0C -> $F3 Step5: Take 2’s complement $F3 + $01 = $F4 Address offset will be: $F4
ORG $1000 Address Opcode Postbyte Operand ABA $1000 18 06 : : BACK LDAA #$30 $106A 86 30 NEGA $106C 40 ADDA $1A00 $106D BB 1A00 BEQ FRONT $1070 27 0E ADDA $31 $1072 9B 31 BNE BACK $1074 26 F4 : : FRONT NOP $1080 A7 : : SWI 3F END Example 1 • Example Program assembled completely
Long branch instructions are equivalent to short branch instructions, except offset is signed 16-bit number [-32,767 to 32,768] Example: Assume distance from branch instruction to destination is greater than 127 memory locations. Must use long branch instructions: Short Branch: BNE FRONT $1074 26 0A : : FRONT NOP $1080 A7 Long Branch: LBNE FRONT $1074 18 26 0408 : : FRONT NOP $1480 A7 Long Branch Instructions Address Opcode Postbyte Operand
Branch if bit(s) clear (BRCLR) Example: Branch to DOG when bit 0 and bit 2 are clear in data contained at address $19 BRCLR $19 #$05 DOG Assume $19 contains #$63 #$63 = 0110 0011 #$05 = 0000 0101 (MASK) will not branch to DOG Assume $19 contains #$62 #$62 = 0110 0010 #$05 = 0000 0101 (MASK) will branch to DOG (Note: BRCLR may be used in extended addressing mode.) Example Branch Instructions Can also be written as: LDY #$0019 BRCLR $00,Y #$05 DOG
Branch if bit(s) set (BRSET) Example: Branch to DOG when bit 0 and bit 2 are set in data contained at address $20 BRSET $20 #$05 DOG Assume $20 contains #$63 #$63 = 0110 0011 #$05 = 0000 0101 (MASK) *will not branch to DOG Assume $20 contains #$65 #$65 = 0110 0101 #$05 = 0000 0101 (MASK) *will branch to DOG Example Branch Instructions Can also be written as: LDX #$0020 BRSET $00,X #$05 DOG
Branch Instructions from Programming Reference Guide
Branch Instructions from Programming Reference Guide (continued) (Note: BRCLR and BRSET boxed in Red)
Bit Clear Example: BCLR $34 #$04 Assume $34 contains #$44 #$44 = 0100 0100 #$04 = 0000 0100 Result: $34 will now contain 0100 0000 Alternative: If index register X contains #$0034, we can use the following instruction to perform the same operation as above. BCLR $00,X #$04 Special Instructions
Bit Set Example: BSET $00,X #$01 Assume the content of index register X is #$0045 and assume that $45 contains #$60. #$60 = 0110 0000 #$01 = 0000 0001 Result: $45 now contains 0110 0001 Alternative: We can use the following instruction to perform the same operation as above. BSET $45 #$01 Special Instructions Cont’d
BITA This instruction is similar to ANDA except that the result of this operation is not stored in accumulator A. Example: BITA #$7C BNE LOOP Assume accumulator A contains #%11110001 #$7C = #%01111100 Accumulator A will still contain #%11110001 after above program is executed Result: The operation produces the result #%01110000 but this is not stored anywhere. The following instruction will branch to LOOP because the result of the preceding operation was not equal to zero. Special Instructions Cont’d
Homework Set #3 Solution 1. Write a subroutine to save the first 5 odd (8-bit) numbers pointed to by the x-register (passed in) onto the stack. Note: For subroutine to execute in main program, a BSR or JSR command must be used in main program. 2. Write a program to output a square wave thru port S pin The output can be observed on the scope, and the period T of the wave should be measured. More than one period wave should be generated. The machine cycle time of the MC9S12C32 should be estimated. Draw the square wave.