Transcript CSE22MAL

ELE22MIC Lecture 7
• Instruction Set Part 5-continued from Lecture 5
–
–
–
–
Selecting Conditional Jumps
Coding For-Loops (in AS11)
Increasing the size of arithmetic Add, Mul
Interrupt processing
For-Loop Example
org
<RAM>
; say $2000 for HC-COM
Count RMB 1
; Uses Count, Acc A and Flags
org
<CODE>
; say $8000 or $2100 for HC-COM
FiveSomethings:
; for(Count = 0; Count <> 5; Count++);
CLR Count
; Count = 0;
LoopAgain:
LDAA Count
; A = Count
CMPA #5
; (A == 5)?
BEQ ExitLoop
; Yes, exit loop
JSR DoSomething ; { DoSomething (); } ; do it 5 times
INC Count
; Count++;
JMP LoopAgain: ;
ExitLoop: RTS
Another For-Loop Example
org
<CODE>
; Uses: AccB = LoopCounter, AccA = LoopLimit, CCR
ASomethings:
; for (B = 0; B <> A; B++);
CLRB
; B = 0;
LoopAgain:
;
CBA
; (A == B)?
BEQ ExitLoop
; Yes, exit loop
JSR DoSomething ; { DoSomething (); } do it A times
INCB
; B++;
JMP LoopAgain: ;
ExitLoop:
RTS
Repeat .. DownTo 0 Example
org
<CODE>
; Uses: = N, CCR
NSomethings:
;
RepeatAgain:
; repeat {
JSR DoSomething ; { DoSomething () // do it A times
DEC N
; N--;
;}
BNE RepeatAgain ; until (N == 0)
ExitLoop:
RTS
org
<RAM>
N
RMB 1
More Robust Repeat Example
org
<CODE>
; Uses: = I, CCR
NSomethings:
;
RepeatAgain:
; repeat {
JSR DoSomething ; { DoSomething () // do it A times
DEC I
; I--; // Alters CCR bits: N, Z, V
;}
BGT RepeatAgain ; until (I <= 0), I.e. if !(I>0) Repeat
ExitLoop:
RTS
org
<RAM>
I
RMB 1
; N can be in range 0..127
Arithmetic/Logical Shift & Rotate
ROtate Left AccA
ROtate Right AccA
Arithmetic Shift Left AccA
Arithmetic Shift Right AccA
Note Negative Number Sign Extended
Arithmetic Shift Right AccA
Note Positive Number Sign Extended
Logical Shift Right AccA
8-bit x 8-bit Multiply Example (1)
org
<RAM>
OP1
RMB 2
; RMB Allocate 2 Bytes =16 bits
OP2
RMB 1
COUNT
RMB
0
org
<CODE>
Multiply:
;perform 8-bit x 8-bit multiply -> 16bit result
; D = OP1 * OP2
CLR OP1
LDAA #8
; AccA = 8
STAA Count ; Count = AccA = 8
LDAA #0
; Result is Accumulated in A:B = D
LDAB #0
; initialised to 0
-> Continued next Page
8-bit x 8-bit Multiply Example (2)
Loop:
LSR
op2+1 ; Carry Flag now = LSB of OP2
BCC
DontAdd ; Carry = 0, so don’t accumululate
ADDD op1
; Add (OP1 << ?) to 16-bit result
DontAdd:
LSL
op1+1 ; Shift Low Byte, MSB of OP1 -> CF
ROL
op1
DEC
Count ; from 8 downto 1
BNE
Loop ; If (Count==0) loop is done.
RTS
; returns result in AccD = OP1 * OP2
; Rotate High Byte, CF-> LSB of OP1+1
Adder/Subtracter Circuit
Adder/Subtracter from
Digital Systems:
Principles and
Applications by Tocci &
Widmer. PHIPE.
32-bit Add Unsigned Example
op1
RMB 2
op2 RMB 2
result RMB 4
; Allocate 2 bytes = 16 bits for operand 1
; Allocate 2 bytes = 16 bits for operand 2
; Allocate 4 bytes = 32 bits for result
Add32bit:
LDD op1+2
; Get source operand (low word)
ADDD op2+2
; 16 bit add, CarryFlag is set
STD result+2
; Mem[Result] = D = low word result
LDD op1
; Get source operand (high word)
ADCB op2+1
; 8 bit add with carry (Low Byte)
ADCA op2
; 8 bit add with carry (High Byte)
STD result
; memory[result] = D = A:B
; result + 2 = High word result. CarryFlag is set if Carry Out
16-bit x 16-bit Multiply Example (1)
Modify the previous 8-bit x 8-bit example:
org
<RAM>
OP1
RMB 4
; Allocate 4 Bytes = 32 bits
OP2
RMB 2
; Allocate 2 Bytes =16 bits
RESULT
RMB 4
; Allocate 4 bytes = 32 bits for result
COUNT
RMB 1
org
<CODE>
To Shift Right all 16 bits:
LSR OP2 ; LS Bit of MS Byte -> CarryFlag
ROR OP2+1 ; CarryFlag -> MS Bit of LS Byte
; LSB of OP2 -> CarryFlag- Used forBCC…
16-bit x 16-bit Multiply Example (2)
ADDD OP1 needs to be 32 bit add to result
To Shift Left all 32 bits: (Multiply by 2)
LSL
OP1+3 ; Shift Lowest Byte, MSB of OP1+3 -> CF
ROL
OP1+2 ; Next, CF-> LS Bit of OP1+2, MS Bit->CF
ROL
OP1+1 ; and again
ROL
OP1
; Finally Rotate CF -> MSBit of High Byte
68HC11 represents numbers stored in Memory in “Big Endian”
format: i.e. Most significant byte is at lowest address, next most at address+1...
Intel 80x86 is “Little Endian” : LS Byte -> Lowest Memory Address.
ASCII Strings Always start at lowest # address in Big & Little