Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Lecture #9 1
ECE251: Tuesday September 24
• Subroutine Parameter Passing (Important)• Allocating Memory in Subroutines (Important)• Recursive Subroutines (Good to know)• Debugging Hints• Programming Hints• Preview of I/O (GPIO) as Time Allows
Labs: #4 starts next week. Prework as always• Homework: #3 due Thursday, Oct. 3.• Mid-Term Exam on October 10, week after next.• Lab Practical #1 week of October 14 in lab.
Lecture #9 2
But First, A Message From Lab #9!Another Interesting Project
• ST77735 SPI 128x160TFT LCD Display
• Interfaced to TIVA Board in C• Also Interfaced to Arduino• Convert this to library callable by our
Assembly Language Routines• 1 or 2-person project. See me.
Lecture #9 3
And Another!
• Initialized RAM– Initial RAM values are stored in ROM now.– Linker has enough info to allow startup.s to
move those ROM values into the correct place in RAM.
– Just need to figure out how to get the right information to startup.s and do the moves.
– Zhu’s startup.s DOES that task.– Jim and I are available to consult on this.
Lecture #9 4
Stacks, PUSH, and POP• PUSHes and POPs must be balanced and in
opposite orders at subroutine levelE.g. PUSH {Ra}, “Allocate_30_words” must be followed by “Deallocate_30_words”, POP {Rb}
• Otherwise higher level routines would not have the stack pointing to the right information after a subroutine exit (BX LR)
• One of the reasons these structures are called stacks, like stacking and unstacking plates. Unstacking occurs in exact opposite order of stacking.
Lecture #9 5
Subroutines: Parameter Passing (8.4.1)
• Call by Value:– Sends parameter VALUES (e.g. numbers) directly
to/from subroutine – These values can be in registers or the stack– They have nothing to do with memory addresses
• “Just give me the facts, ma’am”• “Don’t need no stinkin’ addresses”
• Call by Reference:– Sends parameter ADDRESSES to/from subroutine
– E.g. First element of an array or first character in a string
– These addresses can be in registers or on the stack– The content of these address is not passed directly
Lecture #9 6
Call by Value: Swap Routine
Label Op-Code Operand(s) CommentSWAP
PUSH {R1}PUSH {R2}POP {R1}POP {R2}BX LR
• Is this a valid Subroutine? Why or why not?• Where are the parameters when SWAP is called?• Why are they “Call by Value” ?
Lecture #9 7
Call by Reference: Copy Array of BytesRoutine to copy array of bytes to a second area
COPYTest CMP r0,#0 ; r0 is counter-test for 0
BXEQ LR ;Return if counter is 0 !LDRB r3, [r1],#1 ;r3=[r1] r1=r1+1 (byte)STRB r3, [r2],#1 ;r3=[r2] r2=r2+1 (byte)SUB r0, #1 ;decrement counterB Test ;retry loop
• Where & what are the subroutine’s parameters?• Which are call by reference? Why?• How short can the copied array be? How long?• How would we modify this to copy array of WORDS?• What kind of loop is this (While,Until,For,...)• How to send parameters on stack instead of in registers?• See HW #3.
Lecture #9 8
Parameter passing: These are all valid methods- Use registers- Use the stack (set up by calling program)- Use global memory, i.e. any addresses with data you have defined
Returning results: These are all valid methods- Use registers- Use the stack (We can create a location where the result will be placed)- Use global memory
LOCAL VARIABLE ALLOCATION (ONLY by subroutine)- Allocated by the called subroutine, not caller. Why?- The following instruction efficiently allocates local variables:SUB SP, #40 ; allocate __ words in the stack
Local variable deallocation (also by subroutine)- Must always be done before subroutine exits. Why?- The following instruction efficiently deallocates local variables:ADD SP, #40 ; deallocate __ words from the stack
Subroutine Parameters and Memory Allocation
Lecture #9 9
- The region in the stack that holds subroutine parameters, the subroutinereturn address, local variables, and saved registers is referred to as the stack frame.
- The stack frame is also called activation record, because it is a block ofmemory which is activated as a subroutine is being called.
Stack Frame
SP before SUBPart created bycalled subroutineitself
Part created bycalling program
SP after SUB
SUB SP,#40
IncreasingMemory
Stack
Local variables
Saved registers
(Save return address)
Subroutine Incoming andOutgoing parameters
StackFrame
SP at BL
Lecture #9 10
Look a little more at incoming and outgoing parameters• That’s where results go if they are in the stack (vs. registers or global
memory• Just like incoming parameters, outgoing parameters (return info to caller)
must have space set aside by the calling program. Why?
Stack Frame
Part created bycalled subroutineitself
Part created bycalling program
Stack
Local variables
Saved registers
(Save return address)
Subroutine Incoming andOutgoing parameters
StackFrame
Lecture #9 11
It’s easy--just use normal indexing with SP. Offset will always be positive. Why?
Accessing Parameters in the Stack Frame
Part created bycalled subroutineitself
Part created bycalling program
SP after SUB
IncreasingMemory
Stack
Local variables
Saved registers
(Save return address)
Subroutine Incoming andOutgoing parameters
StackFrame
LDR Rn,[SP,#8]
Lecture #9 12
It’s not too hard--just use normal indexing with SP, like local variables. The challenge is computing the offset, which the programmer must do. You can equate the offset number to a label, making understanding of
the program a little easier. E.g. STR ro, [SP,#Mx]
Accessing Local Variables in the Stack Frame
Part created bycalled subroutineitself
Part created bycalling program
SP after SUB
IncreasingMemory
Stack
Local variables
Saved registers
(Save return address)
Subroutine Incoming andOutgoing parameters
StackFrame
LDR Rn,[SP,#Mx]
Recursive Functions
13
• A recursive function is one that solves its task by calling itself on “smaller” pieces of data.
• Recursive functions are good at showing how stack is used
• An effective tactic is to – divide a problem into sub-problems of the same
type as the original,– solve those sub-problems, and – combine the results
Lecture #9
Defining Factorial(n)
1 × 2 × 3 × … × n
factorial(0) = 1
factorial(1) = 1 = 1 × factorial(0)
factorial(2) = 2×1 = 2 × factorial(1)
factorial(3) = 3×2×1 = 3 × factorial(2)
factorial(4) = 4×3×2×1 = 4 × factorial(3)
factorial(n) = n×(n-1)×…×1 = n × factorial(n-1)
14
Product of the first n numbers
Lecture #9
15
Classic Recursion Example: Factorial• Factorial is the classic educational example:
– 6! = 6 × 5! = 6 × 5 x 4! = 6 × 5 x 4 x 3! …– 6! = 6 × 5 × 4 × 3 × 2 × 1
• The factorial function can be written as a recursive function:; Input: r0 is n; Output: r0 is Fact(n); A bit hard to follow? Recursive functions often are!FACT CMP r0, #1 ;Is n <= 1?
BLE ENDC ;If so, to ENDCPUSH {r0,lr} ;Save r0 (n) and lr on TOSSUB r0, r0, #1 ;n=n=1BL FACT ;get FACT(n-1) in r0POP {r1, lr} ;restore r1 (n), lr from TOSMUL r0, r1, r0 ;r0=n*fact(n-1)BX LR ;normal return
ENDC MOV r0, #1 ;r0=fact(1)=1BX LR ;end case return
Lecture #9
16
Factorial Parameters• Input to FACT is in r0, call by value• Output is in r0, returns value• Temporary “n” stored in stack as a value • Try this “by hand” with input r0=3. I.e. compute 3!
Lecture #9
; Input: r0 is n; Output: r0 is Fact(n); A bit hard to follow? Recursive functions often areFACT CMP r0, #1 ;Is n <= 1?
BLE ENDC ;If so, to ENDCPUSH {r0,lr} ;Save r0 (n) and lr on TOSSUB r0, r0, #1 ;n=n=1BL FACT ;get FACT(n-1) in r0POP {r1, lr] ;restore r1 (n), lr from TOSMUL r0, r1, r0 ;r0=n*fact(n-1)BX LR ;normal return
ENDC MOV r0, #1 ;r0=fact(1)=1BX LR ;end case return
17
Classic Example: Fibonacci Numbers
f(n) = f(n-1) + f(n-2)f(0) = 0f(1) = 1
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, …
Lecture #9
How might we modify Factorial function to perform this function recursively?How would we write a program to compute Fibonacci series non-recursively? Would we need to store the entire series up to the number we’re computing?
Analysis of fib(5)
18
fib(5)
fib(4) fib(3)
fib(3) fib(2)
fib(1) fib(0)fib(2)
fib(1) fib(0)
fib(1)
fib(2)
fib(1) fib(0)
fib(1)
1
1 1 1
1
0 0
0
1
12 1
3 2
5
Lecture #9
Recursion vs IterationAny problem that can be solved recursively (calls
itself) can also be solved iteratively (using loop).
Recursive functions (vs. Iterative functions)• Cons:
– Recursive functions are slow – Recursive function take more memory
• Pros– Recursive functions resembles the problem more naturally – Recursive functions may be easier to program and debug IF the
function is CAREFULLY created as recursive (see next slide)
This is NOT key information for ECE251!
19Lecture #9
We are NOT going to go overthe next 18 slides in detail!
20Lecture #9
• We will review them briefly to see how the stack is used in recursive subroutines.
• They are available to you to look at the details of subroutine calls, stack usage, and computation to compute a factorial.
• There are easier ways to do this!
AREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END
3 * factorial(2)
2 * factorial(1)
1
return 2
return 6
21
0x080001300x08000134
Recursive Factorial in Assembly
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Note implied BX LR instruction!
AREA main, CODE, READONLYEXPORT __main
ENTRY
__main MOV r0, #0x03 BL factorial
stop B stop
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END22
0x080001300x08000134
0x08000130
0xFFFFFFFF
0x20000600
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x200006000x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
0
3
r4
r0
……
Recursive Factorial in Assembly
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
AREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END23
0x080001300x08000134
0x08000136
0x08000134
0x20000600
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x200006000x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
0
3
r4
r0
……
Recursive Factorial in Assembly
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END24
0x080001300x08000134
0x08000138
0x08000134
0x200005F8
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
3
3
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Order in which registers are specified is not important: the lowest register is always stored at the lowest address
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END25
0x080001300x08000134
0x08000144
0x08000134
0x200005F8
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
3
2
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END26
0x080001300x08000134
0x08000136
0x08000148
0x200005F8
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
3
2
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END27
0x080001300x08000134
0x08000138
0x08000148
0x200005F0
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x08000148
3
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
2
2
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END28
0x080001300x08000134
0x08000144
0x08000148
0x200005F0
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x08000148
3
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
2
1
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
AREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END29
0x080001300x08000134
0x08000136
0x08000148
0x200005F0
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x08000148
3
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
2
1
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in Assembly
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END30
0x080001300x08000134
0x08000138
0x08000148
0x200005E8
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x08000148
3
0x08000148
2
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
1
1
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END31
0x080001300x08000134
0x0800013E
0x08000148
0x200005E8
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x08000148
3
0x08000148
2
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
1
1
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END32
0x080001300x08000134
0x08000148
0x08000148
0x200005F0
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x08000148
3
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
2
1
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END33
0x080001300x08000134
0x08000148
0x08000148
0x200005F0
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x08000148
3
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
2
2
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END34
0x080001300x08000134
0x08000140
0x08000148
0x200005F0
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x08000148
3
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
2
2
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Order in which registers are specified is not important.
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END35
0x080001300x08000134
0x08000148
0x08000148
0x200005F8
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
3
2
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END36
0x080001300x08000134
0x08000148
0x08000148
0x200005F8
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
3
6
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END37
0x080001300x08000134
0x08000148
0x08000148
0x200005F8
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x08000134
0x20000600
0
0x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
3
6
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END38
0x080001300x08000134
0x08000134
0x08000148
0x20000600
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x200006000x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
0
6
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Recursive Factorial in AssemblyAREA main, CODE, READONLYEXPORT __main
ENTRY
__main PROCMOV r0, #0x03 BL factorial
stop B stopENDP
factorial PUSH {r4, lr}MOV r4, r0CMP r4, #0x01BNE NZMOVS r0, #0x01
loop POP {r4, pc}NZ SUBS r0, r4, #1
BL factorialMUL r0, r4, r0B loop
END39
0x080001300x08000134
0x08000134
0x08000148
0x20000600
pclrsp
0x00000000
0xFFFFFFFFAddressData
0x200006000x12345678
……
0x200005FC
0x200005F8
0x200005F40x200005F0
0x200005EC
0x200005E8
0x200005E40x200005E0
0x200005DC
0x200005D8
0x200005D4
0
6
r4
r0
……
0x08000136
0x080001440x08000148
0x080001400x08000142
0x080001380x0800013A0x0800013C0x0800013E
0x0800014C
Lecture #9
Finished WithStack & Subroutines & Instructions
(But we’re not done yet today!)
• Any Questions?• Basic topic of HW #3• Will certainly be on Mid-Term Exam !• Next lecture is on new topic:
– General Purpose or Parallel I/O– Read Chapter 14 in text
40Lecture #9
Lecture #9 41
Debugging Hardware
If your system isn’t working:• Replace the TM4C board with a known working board. If
the system now works, you had a defective board. If not:• Change out ONE thing from non-working system—e.g. the
USB cable. Reboot board, reload .o file, reset board, rerun program in an identical manner to previously.
• If system doesn’t work, there’s a problem with your USB cable. If it does work, your USB cable is OK.
• Do same process with another part. Result will tell you if it’s working or not.
• Replace any offending parts. Problem should be fixed. If not, the non-working system is now working!
Lecture #9 42
Debugging Software• Begin with a known, hardware working system (see
previous slide)• Be sure your program ends with a deadloop, so it
doesn’t “run off a cliff” when done and maybe changes program and data area on your board.
• Don’t expect that by just running the same program again, it will give same answer UNLESS board is reset.
• If you’re getting wrong or no answers, you need to single step your program from the start, observing all changes in register and memory data values. Also note whether program area has somehow been changed. This should be obvious if program does weird things when stepped.
Lecture #9 43
Debugging Software-cont’d
• During single stepping, if you see that the results are different from your expectations, figure out what’s wrong with your expectations* about what should be happening. The problem is almost certainly there.
• This means you to need to change your program, reassemble, reset and reload board and rerun—single step again to assure it’s now doing the right thing at the point you had problems with earlier.
• Continue this process throughout the program until you get to the end with nothing different than you expected, i.e. your program executed correctly.
• * If you don’t know what to expect after each instruction, then THAT is your primary problem.
44
Tips for Writing Programs - Review
• Data and Data Structures are Key– Think about how data will be stored in memory– Draw a picture or diagram
• Processing Algorithm is Key– Think about how to process data– Draw a FLOWCHART
• Break Problem into manageable chunks – One page or less if several branches– Two pages if mostly in-line code
• Use helpful names rather than numbers for values (EQU).
Lecture #9
45
Next Lecture
•I/O: 2/3 of courseGeneral Purpose I/O aka GPIO aka Parallel I/O
• Read Chapter 14 in text• GPIO is “Fair Game” on Mid-Term Exam!
– It will/would be minor in point contribution– It will/would be broad vs. detailed– See sample exam online (coming soon)
Lecture #9
Stack Usage Example-Time Allowing; Main Program Test Program to exercise subroutine Smart; Assume SP Initialized to 0x20000.0600
AREA mydata, DATAArray SPACE 0x100 ; Starts at 0x2000.0000 ASSUME ALL BYTES=0xFFVar1 DCW 0x12AB ; Assume initialized
AREA myprog, CODEEXPORT __main
__main ; Start of programLDR R1, =Array ; Get parameter addressLDR R0, =Var1 ; Get second parameter addressLDRB R2, [R0] ; Put the call-by-value data in R2PUSH {R1,R2} ; Push R2 and then R1 onto stackSUB SP, #4 ; Make ROOM for return dataBL Smart ; Call subroutine Smart
Callret NOP ; Smart returns to here• ; Undo stack changes, restore registers in main program
Deadl B Deadl ; Done;Smart
PUSH {R1} ; Save R1 on stackSUB SP, #8 ; Create local storage
• LDRB R1, [SP, #CBV] ; Fetch call-by-value data from stack
ADD R1, R1, #1 ; Increment itSTR R1, [SP, #ROOM] ; Put Data where ROOM was made above
; Undo all local storage created in subroutine Smart
BX LR ; BX to LR addressLecture #9 46
Lecture #9 47
a. What actual code would be used to replace the commented line:; Undo stack changes, restore registers in main program
________________________________________________________________
________________________________________________________________
b. On the next (opposing) page, fill in the registers and memory table with hex values where memory values are defined, and show (to left of table) where pointer SP points when the processor reaches the location commented with . If a value can’t be determined, leave it blank. If it is the address of an instruction, use its label (Deadl, etc.).
In addition, to the right side of the memory map show where (i.e. annotate)(1) incoming and return parameters, (2) saved registers, (from any subroutine calls) and (3) local storage (in any subroutines)
are stored.
c. What should the values of CBV and ROOM be, to make the instructions with “ ”
operate properly? CBV EQU ________ ROOM EQU _________
Stack Usage Example
Lecture #9 48
Show SP M
emory Value Annotation area:
below ↓ or R
egister (Word or B
yte) ↓
R0
R1
R2
SP
LR
0x20000600
SP initialized to this addr.
0x200005FF
0x200005FE
0x200005FD
0x200005FC
0x200005FB
0x200005FA
0x200005F9
0x200005F8
0x200005F7
0x200005F6
0x200005F5
0x200005F4
0x200005F3
0x200005F2
0x200005F1
0x200005F0
0x200005EF
0x200005EE
0x200005ED
0x200005EC
0x200005EB
0x200005EA
0x200005E9
0x200005E8
0x200005E7