shift/rotate, cycle counting, stacks

22
1 Shift/Rotate, Cycle Shift/Rotate, Cycle Counting, Stacks Counting, Stacks Notes: Have you picked up your pocket reference yet? We’ll use them in lecture today… Today: First Hour: Shift/Rotate, Cycle Counting Section 2.9-2.10 of Huang’s Textbook In-class Activity #1 Second Hour: Stacks Section 3.3, 3.6, 3.7 of Huang’s Textbook In-class Activity #2

Upload: valin

Post on 05-Feb-2016

47 views

Category:

Documents


0 download

DESCRIPTION

Shift/Rotate, Cycle Counting, Stacks. Notes: Have you picked up your pocket reference yet? We’ll use them in lecture today… Today: First Hour : Shift/Rotate, Cycle Counting Section 2.9-2.10 of Huang’s Textbook In-class Activity #1 Second Hour : Stacks - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Shift/Rotate, Cycle Counting, Stacks

1

Shift/Rotate, Cycle Shift/Rotate, Cycle Counting, StacksCounting, Stacks

Notes:

• Have you picked up your pocket reference yet?

• We’ll use them in lecture today…

Today:• First Hour: Shift/Rotate, Cycle Counting

– Section 2.9-2.10 of Huang’s Textbook

– In-class Activity #1

• Second Hour: Stacks

• Section 3.3, 3.6, 3.7 of Huang’s Textbook

– In-class Activity #2

Page 2: Shift/Rotate, Cycle Counting, Stacks

2

Apply to a memory location and accumulators A, B and D.

There are three 8-bit arithmetic shift left instructions:

ASL opr memory location opr is shifted left one placeASLA accumulator A is shifted left one placeASLB accumulator B is shifted left one place

C 0b7 ----------------- b0

Arithmetic Shift Left Arithmetic Shift Left

Has the effect of multiplying by 2

Page 3: Shift/Rotate, Cycle Counting, Stacks

3

Arithmetic shift right. E.g.: ASR opr memory location opr is shifted right one place ASRA accumulator A is shifted right one place

Cb7 ----------------- b0

16-bit arithmetic shift left instruction:ASLD

C 0b7 ----------------- b0 b7 ----------------- b0accumulator A accumulator B

Shift Instructions Shift Instructions

Note: Divides by 2 each time

A negative number remains negative after shifting

Page 4: Shift/Rotate, Cycle Counting, Stacks

4

Logical shift left instructions. E.g.: LSL opr memory location opr is shifted left one place LSLA accumulator A is shifted left one place

C b7 ----------------- b0 0

Logical shift right instructions:LSR opr memory location opr is shifted right one place LSRA accumulator A is shifted right one place

Cb7 ----------------- b00

Shift & Rotate Instructions Shift & Rotate Instructions

Useful for unsigned multiply and divide by 2

Page 5: Shift/Rotate, Cycle Counting, Stacks

5

Rotate left instructions that operate on 9-bit operands.ROL opr memory location opr is rotated left one place ROLA accumulator A is rotated left one placeROLB accumulator B is rotated left one place

Cb7 ----------------- b0

Rotate right instructions: Eg: ROR opr memory location opr is rotated right one place RORA accumulator A is rotated right one place

C b7 ----------------- b0

Shift & Rotate Instructions Shift & Rotate Instructions

Useful for controlling stepper motors, computing checksums, etc.

Page 6: Shift/Rotate, Cycle Counting, Stacks

6

How long does a How long does a program take to run?program take to run?

• The 6811 manual lists the number of clock cycles that each instruction takes

• How long is a clock cycle?– Let’s look it up.

• Execution time = # cycles x time per cycle

Instruction Execution time (E clock cycles)

BNE <rel> 3DECB 2DEX 3LDAB <imme> 2LDX <imme> 3NOP 2

Table: Execution times of a sample of instructions

500ns

Page 7: Shift/Rotate, Cycle Counting, Stacks

7

LDX #2 ; 3 cyclesagain NOP ; 2 cycles

NOP ; 2 cyclesDEX ; 3 cyclesBNE again ; 3 cycles

ExampleExample

Time = (3 + 102) cycles

Page 8: Shift/Rotate, Cycle Counting, Stacks

8

Example Write a program loop to create a delay of 100 ms.

Solution: A delay of 100 ms can be created by repeating the previous loop 20000 times.

The following instruction sequence creates a delay of 100 ms.

LDX #20000AGAIN NOP

NOPDEXBNE AGAIN

Longer delays can be created by using nested program loops.

Program Execution TimeProgram Execution Time

Page 9: Shift/Rotate, Cycle Counting, Stacks

9

Let's explore the cycle counting technique

Interrupt methods come later

Let's explore the cycle counting technique

Interrupt methods come later

Time DelaysTime DelaysThere are two ways to generate time delays using the 68HC11

• Timer-based interrupts

• Cycle countingCycle counting

Let's see about generating a 1 ms delay subroutineLet's see about generating a 1 ms delay subroutine

Page 10: Shift/Rotate, Cycle Counting, Stacks

10

Do Activity #1 NowDo Activity #1 NowExample Write a program loop to create a delay of 100 ms.

Solution: A delay of 100 ms can be created by repeating the previous loop 20000 times.

The following instruction sequence creates a delay of 100 ms.

LDX #20000AGAIN NOP

NOPDEXBNE AGAIN

Much longer delays can be created by using nested program loops.

Things to do:

Choose number of times to do the loop

Add NOP instructions as “padding” if needed

Page 11: Shift/Rotate, Cycle Counting, Stacks

11

Complete Programmer's ModelComplete Programmer's Model

SS XX HH II NN ZZ VV CC CCondition CCode RRegister

CCarry/borrow (MSB)

oVVerflow (2s C)

ZZero

NNegativeHHalf carry (bit 34),(BCD)

15 SP 0 Stack Pointer

SSTOP disable

XX-interrupt mask

II-interrupt mask

7 A 0 7 B 0

15 D 0 16-bit Accumulator D

15 PC 0 Program CounterProgram Counter

15 IX 0 16-bit Index Register IX

15 IY 0 16-bit Index Register IY

8-bit Accumulators AA & BB

Page 12: Shift/Rotate, Cycle Counting, Stacks

12

StacksStacks

• Very important data structure– Also a source of headaches– Very demanding, requires discipline– If a stack is messed up by even one byte, a computer is

likely to “crash”

• A “last-in first-out” data structure– A queue is a first-in first-out structure

• May be implemented differently on different computers

• Let’s study the 6811 stack….

Page 13: Shift/Rotate, Cycle Counting, Stacks

13

Stacks on 68HC11Stacks on 68HC11

StacksStacks

Grow down, toward low memorytoward low memory

DataData

PuSHed on the stack (PSH)

PULled off the stack (PUL)

Always PULPUL as many times as PSHPSH

The Stack Pointer always points to the next empty cell on the stackThe Stack Pointer always points to the next empty cell on the stack

Top of Stack cell

Stack AreaStack Area

15 SP 0

Stack Pointer empty

Low address

High address

Page 14: Shift/Rotate, Cycle Counting, Stacks

14

PPuuSHSHing Data on Stacksing Data on Stacks

SP empty

Stack AreaStack Area

SP points to empty cell

SP points to empty cell

Data is PuSHed

on the stack

Data is PuSHed

on the stack

data

emptySP

SP decrements and points to

empty cell

SP decrements and points to

empty cell

Page 15: Shift/Rotate, Cycle Counting, Stacks

15

A = $33, B = $20, SP = $00FF. Question 1: What will the top byte of the stack contain before and after a PSHA?

Question 2: What will the top two bytes of the stack contain if PSHB is executed?

XX

original stack

SP = $00FF

$33XX SP =

$00FE

after PSHA

XX

$20

$33

SP =$00FD

after PSHB

PUSH PUSH

Page 16: Shift/Rotate, Cycle Counting, Stacks

16

PULPULling Data off Stacksling Data off Stacks

SP

data

Stack AreaStack Area

empty

SPSP

SP points to empty cellSP points to empty cell

SP incrementsSP increments

Data is PULPULled off stack

SPSP points to empty cell

empty

Page 17: Shift/Rotate, Cycle Counting, Stacks

17

Stack InstructionsStack InstructionsReserve a Stack AreaReserve a Stack Area, e.g., 256 cells:

ORG $0000

STACK RMB 256

INITSP EQU * - 1 Top of Stack

Initialize the Stack PointerInitialize the Stack Pointer:

INIT LDS #INITSP

Used together

Push Registers on StackPush Registers on Stack:

PSHA Push ACCA

PSHB Push ACCB

PSHX Push IX (2 bytes)

PSHY Push IY (2 bytes)

SimilarlySimilarly, PULA, PULB, PULX, and PULY

Page 18: Shift/Rotate, Cycle Counting, Stacks

18

More Stack InstructionsMore Stack Instructions

Increment / Decrement Stack PointerIncrement / Decrement Stack Pointer:

INS SP+1 SP

DES SP-1 SP

Load / Store Stack PointerLoad / Store Stack Pointer:

LDS M:M+1 SP IMM, DIR, EXT, indX, indY

STS SP M:M+1 DIR, EXT, indX, indY

Lots of addressing modes !!

Lots of addressing modes !!

Notice the

increment SP and decrement IX, IY

Notice the

increment SP and decrement IX, IY

Transfer RegistersTransfer Registers:

TSX SP+1 X

TSY SP+1 Y

TXS IX-1 SP

TYS IY-1 SP

Page 19: Shift/Rotate, Cycle Counting, Stacks

19

Main program

Subroutine 1 Subroutine 2 Subroutine 3

Subroutine 1.1 Subroutine 2.1 Subroutine 3.1

Subroutine 2.1.1 Subroutine 2.1.2

Program Structure

Main program

Subroutine

<call> sub_x . . .

. . .

<return>

sub_x:

Subroutine Processing

SubroutinesSubroutines

Page 20: Shift/Rotate, Cycle Counting, Stacks

20

Subroutine InstructionsSubroutine Instructions

Subroutine callSubroutine call:

JSR

BSR

SP

RTHL

Stack AreaStack Area

RTNH

SPSP

JSRJSR, BSR BSR push

Return Address

on Stack

JSRJSR, BSR BSR push

Return Address

on Stack

RTN is the address of the instruction following

the subroutine call

RTN is the address of the instruction following

the subroutine call

RTSRTS pulls

Return Address

off Stack

empty

empty

Subroutine returnSubroutine return:

RTSempty

Page 21: Shift/Rotate, Cycle Counting, Stacks

21

Subroutine ChecklistSubroutine ChecklistChecklist:

Remember that stack grows to lower memory addresses

Initialize stack pointer Create enough space for all items that need to be

pushed to the stack Push return address and other information on stack Jump or Branch to the subroutine. Special steps for different types of parameter

passing methods Execute subroutine RTS pulls return address off stack

Page 22: Shift/Rotate, Cycle Counting, Stacks

22

Do Activity #2 NowDo Activity #2 NowDue: End of Class Today.

RETAIN THE LAST PAGE(S) (#3 onwards)!!

For Next Class:• Bring Huang Textbook, & HC11 PRG

• Required Reading:– Sec 3.9-3.11 of Huang

• This reading is necessary for getting points in the Studio Activity!