l04.loops+and+array.annotated (1)
DESCRIPTION
ece 362 annotated notes fuck jaewonTRANSCRIPT
ECE 362 Microprocessor Systems and Interfacing ©
4-1
Lecture 4 Program Loops and Arrays
Outline Indexed addressing Program structure Basic Subroutine and Stack Data arrays Delay loops Table Lookup External references
ECE 362 Microprocessor Systems and Interfacing ©
4-2
Do you remember Indexed Addressing mode?
When need to access data arrays, useLDAA IND,X ; [X+IND] ALDAA IND,Y ; [Y+IND] A
Registers X and Y are index registers (i.e., they contain address or pointer)
IND can be a ±3-bit auto offset, 5-bit, 9-bit or 16-bit signed offset/label.
LDAA +10,X ; [X+10] ALDAA -100,Y ; [Y-100] A
ECE 362 Microprocessor Systems and Interfacing ©
4-3
oprx0_xysp: This word breaks down into one of the following alternative forms that assemble to an 8-bit indexed addressing postbyte code, which is designated as xb oprx5,xysp oprx3,–xys oprx3,+xys oprx3,xys– oprx3,xys+ abd,xysp
oprx3: A label or expression that evaluates to an offset in the range +1 to +8 oprx5: A label or expression that evaluates to a 5-bit offset in the range –16 to +15 oprx9: A label or expression that evaluates to a 9-bit offset (range –256 to +255) oprx16: A label or expression that evaluates to a signed or unsigned 16-bit offset opr8a — A label or expression that evaluates to an 8-bit address. opr16a — A label or expression that evaluates to a 16-bit address. opr8i — A label or expression that evaluates to an 8-bit immediate value opr16i — A label or expression that evaluates to a 16-bit immediate value
ECE 362 Microprocessor Systems and Interfacing ©
4-4
ECE 362 Microprocessor Systems and Interfacing ©
4-5
Indexed Addressing: Autoincrement and Autodecrement, Pre- or Post-
The index register can be automatically incremented or decremented to change the location being accessed.
It can be modified either before or after it is referenced. contents of index registers are changing
It can be adjusted in the amount of -8 ~ -1, 1 ~ 8.ldaa 2,X+ ; reference first, then add 2; suffixafter refldaa 4,-X ; subtract 4, then reference; prefixbefore refldaa 4,+X ; add 4 (i.e., X+4 X), and then reference
Indexed Addressing Modes (X is pointer)
LDAA 1,X ; where does it access? ; what is X value after execution? LDAA 1,+X ; where does it access? ; what is X value after execution? LDAA 1,X+ ; where does it access? ; what is X value after execution? LDD 2,+X ; what value is loaded into D? ; what is X value after execution?
ECE 362 Microprocessor Systems and Interfacing ©
4-6
$800X $12$800 $34$801 $56$802 $78$803
ECE 362 Microprocessor Systems and Interfacing ©
4-7
Indexed Addressing Exercise
My_constant: sectiondataBytes: dc.b $10,$20,$30,$40My_code: section
ldx #dataBytesldaa 3,x
What is the value loaded into X when the program executes? _______
What value is loaded into A? _______
What’s Subroutine?
When is it used? a section of code is used multiple times
like C function, e.g., printf(…); We separate it and make a standalone code for easy handling, debugging, and maintenance
ECE 365 © 8
What’s Subroutine?
A subroutine has entry and exit The entry is associated with a label (used as subroutine name) The exit has a return-from-subroutine instruction (RTS) A subroutine is invoked by using JSR instruction
JSR means jump to subroutine JSR subroutine_name ; JSR printf
When a JSR instruction is executed, the address of subroutine name (label) is entered into PC and thereby jump occurs
After executing the subroutine, CPU needs to come back to the next instruction after the JSR instruction How?
Since a subroutine is executed and returned, CALL instead of JSR is more commonly used
ECE 365 © 9
How to return from Subroutine?
Right before jump, the address of next-to-JSR instruction (contents of the PC, called return address) must be saved to enable correct return
JSR is a special branch instruction that performs the following operations store the contents of the PC onto stack jump to the target address specified by the JSR instruction
The return-from-subroutine (RTS) instruction moves the return address (saved on the stack) to PC and thereby return jump occurs
What is stack? a special memory space used for subroutine calls
ECE 362 Microprocessor Systems and Interfacing ©
4-10
Memory Calling program Memory
Subroutinelocation location
….200 JSR Sub1 1000 Sub1 first
instruction203 next instruction …..
…. ….. RTS
203
203
1000
PC
Stack memory
When returningWhen calling
Steps of Subroutine call & return using Stack
Performed by the processor automatically
ECE 365 11©
ECE 362 Microprocessor Systems and Interfacing ©
4-12
Behavior of 68HC12 Subroutine Call & Return JSR SubroutineName calls a subroutine
subroutine has a label (used by callers)JSR Add2ToA ; side stepped for repeated tasksSTAA PORTS……
Add2ToA: ADDA #2……RTS
RTS causes the program to return from the subroutine to the caller. Program execution returns to the next instruction after the JSR
instruction. RTS should be the last instruction in subroutines.
The stack pointer must be initialized before making a subroutine call, e.g., using LDS #$c00 ; load into SP
What about Registers in subroutines?
In subroutines Subroutines should not touch registers of the calling program But we have limited number of registers, so how can we not
touch those used in the caller? How do we know which registers the caller uses?
Instead, a subroutine must save (and restore before return) the contents of callers’ registers that it is going to use where? on the stack how? using stack manipulation instructions
ECE 362 Microprocessor Systems and Interfacing ©
4-13
ECE 362 Microprocessor Systems and Interfacing ©
4-14
What Stack Manipulation Instructions exist?
Saving register contents Retrieving register contents
onto the stack from the stack
PSHA PULAPSHB PULBPSHD PULDPSHX PULXPSHY PULY
ECE 362 Microprocessor Systems and Interfacing ©
4-15
What’s Programming Structure?
A few basic structures provide most of the program building blocks needed.
Common program structures allow direct implementation of flow charts, pseudo-code, or high-level language statements.
Implementation of C language blocks: for while do-while if-else
What kinds of Loop Types?
for (variable initialization; continue condition; variable update) { Code to execute while the condition is true } completion check is done at the beginning
Code block may never be executed ascending iterator, e.g., for (i=0; i<N; i++) descending iterator, e.g., for (i=N; i>0; i--)
while (continue condition) { Code to execute while the condition is true } Code block may never be executed
do {Code to execute } while (continue condition); Code block is executed at least once
ECE 362 Microprocessor Systems and Interfacing ©
4-16
ECE 362 Microprocessor Systems and Interfacing ©
4-17
How to assembly program a While loop?
// count the number of tens in a variable value// register b is the counter
ldaa #value Alternativeldab #0
TensLP:cmpa #9 cmpa #10ble done blt doneincbsuba #10bra TensLP
Done:
index = 0;while (value > 9){ index++; value = value - 10;}
Another example of a While loop
// send characters until NULL character// register X points to a character stringDisplayLP: ldaa 1,x+ ; reference and then add 1 to x
beq donepsha ; char sent on stackjsr DisplayChar ; subroutinepulabra DisplayLP
done:
ECE 362 Microprocessor Systems and Interfacing ©
4-18
while(*charPtr != 0){ SendChar(*charPtr); charPtr++;}
ECE 362 Microprocessor Systems and Interfacing ©
4-19
How to program a Do-while loop?
// register X points to a character string
ldab #10 ; the number of charactersSendLP: ldaa 1,x+
psha ; char sent on stackjsr SendChar ; send one characterpuladbne b,SendLP ; decrement b, and branch
if not zerocharCnt = 10do{ SendChar(*charPtr++); charCnt--;} while(charCnt > 0);
ECE 362 Microprocessor Systems and Interfacing ©
4-20
How to program a For-loop?
// register b contains the count (iterator)// register X points to the first character of a character string// numChars contains the total character count
ldab #0 ; B is the countSendLP: cmpb numChars
bcc done ; if carry flag =0ldaa 1,x+ ; passing parameter psha ; char sent on stackjsr SendCharpulaincbbra SendLp
done:
for(count=0; count<numChars; count++){ SendChar(*charPtr); charPtr++;}
ECE 362 Microprocessor Systems and Interfacing ©
4-21
How to program If-then-else?
; assume A has a signed number; output sign character according to value in register A
cmpa #0bge SendPlus ; jump if a ≥ #0ldaa #‘-’ ; passing parameter -bra SendIt
SendPlus: ldaa #‘+’ ; passing parameter +SendIt: psha
jsr SendCharleas 1,s ; sp = sp + 1, just modify SP
; don’t need to pull A backif (a ≥ 0) SendChar(‘+’);else SendChar(‘-’);
ECE 362 Microprocessor Systems and Interfacing ©
4-22
Instruction Timing and E-clock
CPU is a complex FSM (finite state machine) An FSM consists of Flip-Flops (FFs) and logic gates Flip-Flops need clocks CPU is a sophisticated FSM operating clock by clock Clock is generated from a crystal oscillator Our CPU’s clock frequency, referred to as E-clock, is one-half
the crystal oscillator’s frequency. One memory access cycle per E-clock
When accessing internal memory, either ROM or RAM, the processor can read up to two bytes at every memory access.
16-bit data bus internally
ECE 362 Microprocessor Systems and Interfacing ©
4-23
What’s Clock Cycle?
Lab board’s crystal oscillator frequency is 8 MHz. What is the period of the E-clock on the Lab board? Answer in
microseconds. __________ One E-clock period is called a clock cycle How to measure the elapsed time to execute a code section?
The time is called execution time How to measure the execution time of a code section?
the number of clock cycles X clock period
ECE 362 Microprocessor Systems and Interfacing ©
4-24
How to measure Execution Time?
Execution time is the # clock cycles X clock period
What is the total execution time required for the following loop?
LDX #14 ; 2 cyclesDELAY: DEX ; 1 cycle
BNE DELAY ; 3 (back) / 1(down) cycle(s)
First, we need to count the total # clock cycles: _________If clock period is .25 usThen, the execution time is ___________
ECE 362 Microprocessor Systems and Interfacing ©
4-25
Delay for a specific time.
Using the previous delay loop, what count is required to delay 0.14 milliseconds? Assume an 8-MHz crystal.
First determine how many clock cycles are necessary.
Answer _________
ECE 362 Microprocessor Systems and Interfacing ©
4-26
Quiz
What is the total number of clock cycles and the delay time of the following program? Assume a crystal frequency of 1 MHz.
;instruction ; # machine cyclesldaa #5 ; 1
wt_lp: deca ; 1bne wt_lp ; 3 (loop back) / 1 (fall through)
Clock period = _________ Number of machine cycles = ______ Total Execution Time = _________seconds
Programming
Reading switches 50 times into an Array Where to start? What would be main coding? What addressing mode should you use for the main
coding? What program structure should you use?
ECE 362 Microprocessor Systems and Interfacing ©
4-27
ECE 362 Microprocessor Systems and Interfacing ©
4-28
Reading switches 50 times into an Arraydata: sectioncnt: equ 50 Alternative 1 Alternative 2array: ds.b cntswitches: equ $aetext: section
ldx #0 ldx #cnt-1 ldx #arrayagain: ldaa switches
adda #10 staa array,x staa**inx dex -------
(remove)cpx #cnt cpx #0 cpx ***bne again bne again
ldaa switchesadda #10staa array,x
ECE 362 Microprocessor Systems and Interfacing ©
4-29
Reading switches 50 times into an Array
How does the previous program need to change if LDX #0 is replaced with LDX #array? Assume the program will store the results in the same locations as in the example.
Output a sequence of bytes (e.g., string) to display (PortS) till a null is read
What’s a null? 0, which is also often called a terminator
Where to start? What would be main coding? What addressing mode should you use for the main
coding? What program structure should you use?
ECE 362 Microprocessor Systems and Interfacing ©
4-30
ECE 362 Microprocessor Systems and Interfacing ©
4-31
PORTS: equ $d6data: dc.b 25,36,14,27,36,124
dc.b 99,45, 0 ; 0 is the terminatorldx #data ; set base address into
Xagain: ldaa 1,x+ ; reference and inc pointer
cmpa #0 ; check if the loaded beq exit ; value is the
terminatorstaa PORTSbra again
exit:
Output a sequence of bytes (e.g., string) to display (PortS) till a null is read
ECE 362 Microprocessor Systems and Interfacing ©
4-32
Another way of outputting a sequence of bytes to PortS till a null is read
PORTS: equ $d6data: dc.b 25,36,14,27,36,124
dc.b 99,45, 0 ; 0 is the terminatorldx #0 ; clear index
again: ldaa #data,x ; set base addresscmpa #0 ; check if the loaded
beq exit ; data is the terminator
staa PORTS ; displayinx ; move to nextbra again
exit:
ECE 362 Microprocessor Systems and Interfacing ©
4-33
Debugger Break Points
Breakpoints allow you to execute all statements up until the selected one rather than stepping through every statement.
If no breakpoint is placed in the program, execution continues indefinitely (or until an end or halt instruction is reached).
Explain how to set breakpoint in lab
ECE 362 Microprocessor Systems and Interfacing ©
4-34
Debugger Break Points
Laboratory 4.1 : Arrays
1. Write a program that will output the sequence shown below to the LEDs. The LEDs are connected to Port S (address $248). The data direction register for Port S (address $24A) will need to be
programmed to make Port S an output port. The program should continuously loop outputting the data to the LEDs.
When the end of the data list is reached, the program should start over at the beginning of the list outputting the data.
Use a pointer to access the list. You may consider a terminator at the end of the list or alternatively use a counter to count to the end of the list.
To debug, step through the program. When running the program at speed, all the lights will appear to be on.
Use either a for style loop with a counter or while style with a terminator.
Sequence: $81,$42,$24,$18,$00,$24,$42ECE 362 Microprocessor Systems and
Interfacing ©4-35
Unsigned Multibyte Addition using a loop
Where to start? What would be main coding? What addressing mode should you use for the main
coding? What program structure should you use? Do you have to initialize any?
flags? variables?
ECE 362 Microprocessor Systems and Interfacing ©
4-36
ECE 362 Microprocessor Systems and Interfacing ©
4-37
Unsigned Multibyte Addition using a loop
cnt: equ 4data1: dc.b $32,$84,$4c,$9adata2: dc.b $5a,$a2,$2a,$8fdata3: ds.b 4 ; space for result
ldab #cntldx #3 ; set index to the last
byteclc ; clear carry
again: ldaa data1,xadca data2,xstaa data3,xdex ; does not affect Carrydbne b, again; no flags are affected
3-38
Combined Branch Instructions
IBEQ – Increment the first operand and branch (to second operand) if the first operand become zero.
IBNE – Increment and branch if not zero. DBEQ – Decrement and branch if zero. DBNE – Decrement and branch if not zero.
E.g., DBNE B, Loop
*note: they don’t affect flags
ECE 362 Microprocessor Systems and Interfacing ©
ECE 362 Microprocessor Systems and Interfacing ©
4-39
Searching a List (Finish if Terminator)
Where to start? What would be main coding? What addressing mode should you use for the main coding? What program structure should you use? What are the input and output?
VAR: DS.B 1 ; find VAR in list DATDAT: DC.B 20,30,40,50
DC.B 60,70,80,90,0
START: LDX #DAT ; set base address
…
ECE 362 Microprocessor Systems and Interfacing ©
4-40
ECE 362 Microprocessor Systems and Interfacing ©
4-41
Searching a List Using a Terminator
VAR: DS.B 1 ; find VAR in list DAT
DAT: DC.B 20,30,40,50DC.B 60,70,80,90,0
START: LDX #DAT ; set base addressNEXT: LDAA 0,X ; load a value from
listCMPA #0 ; check NULLBEQ TERMCMPA VAR ; keep scanningBEQ MATCHINXBRA NEXT
MATCH: NOP ; do when match is foundTERM: NOP ; did not find match
How to search a Lookup Table- finding nth element in an array
Example Translate a Celsius value to Fahrenheit
Where to start? What would be main coding? What addressing mode should you use for the main coding? What program structure should you use? Do you have to initialize any? What are the input and output?
ECE 362 Microprocessor Systems and Interfacing ©
4-42
3-43
Table Lookup - find nth element in an array
;Translate a Celsius value to Fahrenheit (Celsius input is used as the index); Instead of using equation and calculation, Fahrenheit = Celsius * 9 / 5 + 32
TempTran: dc.b 32,34,36,37,39,41,43 ; Fahrenheit dc.b 45,46,48,50,52,54,55 ; Fahrenheit
TempVal: equ 5 ; Celsius (used as an index to the array)
ldx #TempTran ; get the starting address of arrayldab #TempVal ; get indexabx ; find address of dataldaa 0,x ; get data
An alternative way (indexed addressing mode) to access dataldx #TempValldaa #TempTran, x
What is the purpose of the “abx” instruction? _______
ECE 362 Microprocessor Systems and Interfacing ©
ECE 362 Microprocessor Systems and Interfacing ©
4-44
3-45
Laboratory 4.2 : Table lookup
Write a program that repeatedly reads the dipswitches and writes the following values to the LEDs for the switch inputs (using a lookup table). The left value is the switch input and the right value is sent to the LEDs.
$0->$12 $1->$18 $2->$24 $3->$36 $4->$3a $5->$43 $6->$4b $7->$51$8->$61 $9->$6f $a->$7a $b->$92$c->$b6 $d->$c3 $e->$d6 $f ->$f1
ECE 362 Microprocessor Systems and Interfacing ©
Laboratory 4.3 Search in a table
Start a new project. Write a flowchart and then write program to determine the
index of a number in a look-up table. Use the following table.
table: dc.b $eb,$77,$7b,$7d,$b7,$bb,$bd,$d7dc.b $db,$dd,$e7,$ed,$7e,$be,$de,$ee
val: ds.b 1
Your program should end at the same memory location
(use a NOP instruction) whether a match is found or not.
ECE 362 Microprocessor Systems and Interfacing ©
4-46
ECE 362 Microprocessor Systems and Interfacing ©
4-47
Laboratory 4.4 : Stepper Motor
Write an assembly language program to turn the stepper motor. Start a new project.
Set bits 1 to 4 of port P DDR ($25A) to 1 (output direction) Do not set bits 0, 5, 6 and 7; setting these bits may
produce unexpected results. Output the following sequence to turn the motor clockwise
to port P ($258):Bit # 7 6 5 4 3 2 1 0 |= value
x x d 0 1 0 1 d |= 0x0Ax x d 1 0 0 1 d |= 0x12x x d 1 0 1 0 d |= 0x14x x d 0 1 1 0 d |= 0x0C
ECE 362 Microprocessor Systems and Interfacing ©
4-48
Laboratory 4.4 : Stepper Motor
Note:1. Be sure the cable to the stepper motor is connected
before performing this exercise.2. To control the stepper motor the jumper on J2 (lower
board) must be moved to the left side position.3. Other bits of PORT P are don’t care (either 0 or 1)
ECE 362 Microprocessor Systems and Interfacing ©
4-49
Assembler Directives: xdef & xref
External references. Use xdef to make a defined symbol visible outside the
current module. xdef printf
Use xref to indicate that a referenced symbol is defined outside the current module.
xref printf
ECE 362 Microprocessor Systems and Interfacing ©
4-50
Laboratory 4.5 : Loop Delay, External References
Modify the stepper motor program to place the delay loop in a separate file.
Create a new assembly file with the .asm extension and save it to the sources directory in your project.
Add the assembly file created in the previous step to the sources in your project by right clicking in the project finder.
Create a variable DelayCount to hold the value for the number of iterations of the delay loop to execute(this will allow you to change the length of your delay loop by adjusting the DelayCount variable)
Load the delay count in a variable (DelayCount) in the main program. (continue in the note page)