ECE251: Tuesday September 12 Finish Branch related instructions Stack Subroutines Note: Lab 3 is a 2 week lab, starting this week and covers the Stack and Subroutines. Labs: Lab #2 is due this week. Lab #3 this week and next. Note: Lab 3 has prework! Homework: #2 Due Thursday, 4:30 pm Mid-Term Exam: October 5 Review on October 3 Lecture #7 1
Comparison Instructions Instruction Operands Operation Flags CMP Rn, Op2 Compare N,Z,C,V CMN Rn, Op2 Compare Negative N,Z,C,V TEQ Rn, Op2 Test Equivalence N,Z,C TST Rn, Op2 Test N,Z,C The only effect of the comparisons is to update the N, Z, C and V flags. There is no destination register Rd. Operations are: - CMP operand1 - operand2, but result not written - CMN operand1 OR operand2, but result not written - TST operand1 AND operand2, but result not written - TEQ operand1 EXOR operand2, but result not written Examples: CMP r0, r1 ;Performs r0 - r1 and updates N,Z,C,V appropriately TST r2, #5 ;Performs r2 AND 5 and updates N,Z,C appropriately Lecture #7 2
Number Interpretation Which is greater? 0xFFFFFFFF or 0x00000001 If they represent signed numbers, the latter is greater. (1 > -1) If they represent unsigned numbers, the former is greater. (4billion+ > 1) Lecture #7 3
So What? It s software s responsibility to tell computer how to interpret data. Use signed vs unsigned branch instructions: MOVS r6, #0xFFFFFFFF MOVS r5, #0x00000001 CMP r5, r6 BLE Then_Clause... BLE: Branch if less than or equal, signed MOVS r6, #0xFFFFFFFF MOVS r5, #0x00000001 CMP r5, r6 BLS Then_Clause... BLS: Branch if lower or same, unsigned Lecture #7 4
If-then Statement If a<0 then a = -a x = x + 1 Implementation 1: ; r1 = a, r2 = x CMP r1, #0 ; Compare a with 0 BGE endif ; Go to endif if a 0 then RSB r1, r1, #0 ; a = - a endif ADD r2, r2, #1 ; x = x + 1 Lecture #7 5
Compound Boolean Expression x > 20 AND x < 25 x 20 OR x 25 x == 20 OR x == 25 NOT(x == 20 OR x == 25) What condition above does the program below test? What happens if condition is met (i.e. then )? Assembly Program ; r0 = x CMP r0, #20 ; compare x and 20 BLE then ; go to then if x 20 CMP r0, #25 ; compare x and 25 BLT endif ; go to endif if x < 25 then MOV r1, #1 ; a = 1 endif Lecture #7 6
If a=1 then b = 3 else b=4 If-then-else ; r1 = a, r2 = b CMP r1, #1 ; compare a and 1 BNE else ; go to else if a 1 then MOV r2, #3 ; b = 3 B endif ; go to endif else MOV r2, #4 ; b = 4 endif What are then, else and endif in the program above? Lecture #7 7
Big New Topic: The Stack Breaking large programming problems down into smaller subcomponents (subroutines) is an excellent practice. The stack is useful to store parameters passed between these subcomponents and temporary data used only by the subroutine. In addition, storage space is required for the return address of each (possibly nested) subroutine call. These storage requirements are even more obvious if a recursive algorithm is used (subroutine calls itself, like a factorial computation might be done). So, we need to allocate this temporary storage space as we go. This space is called a stack because the last stored data is the first accessed, like a stack of plates. Lecture #7 8
Stack A Last-In-First-Out (LIFO) data structure Allows easy access to the most recently added item(s) Also called the top of the stack Key operations: push (add item(s) to stack) pop (remove top item(s) from stack) Lecture #7 9
Stack Growth Convention: Ascending vs Descending Descending stack: Stack grows towards low memory address Ascending stack: Stack grows towards high memory address Lecture #7 10
Stack Growth Convention: Full vs Empty Full stack: SP points to the last item pushed onto the stack Empty stack: SP points to the next free space on the stack Lecture #7 11
ARM Cortex-M4 Stack: Full Descending Cortex-M4 uses full descending stack! Memory Address 0xFFFFFFFF Stack always operates on 32-bit data Stack base Stack Pointer (SP) = R13 Stack Pointer is Pre-decremented on PUSH Stack Pointer (SP) Stack top Post-incremented on POP This must be, given Full Descending definition PUSH stack grow downwards POP 0x00000000 Lecture #7 12
Stack Manipulation PUSH {Rd} ;n registers in list SP = SP-4*n descending stack [[SP]] = Rd full stack Push multiple registers PUSH {r6, r7, r8} Examples below are all equivalent. PUSH {r8, r7, r6} PUSH {r8} PUSH {r7} PUSH {r6} The order in which registers listed in the register list does not matter. When pushing multiple registers, these registers are automatically sorted by name and the lowest-numbered register is stored to the lowest memory address (stored last). Lecture #7 13
Stack Manipulation POP {Rd} Rd = [[SP]] SP = SP + 4*n Stack shrinks. So why +? Pop multiple registers Examples below are all equivalent. POP {r6, r7, r8} POP {r8, r7, r6} POP {r6} POP {r7} POP {r8} The order in which registers listed in the register list does not matter. When popping multiple registers, these registers are automatically sorted by name and the lowest-numbered register is loaded from the lowest memory address, (loaded first). Lecture #7 14
Stack Examples PUSH {r3, r1, r7, r2} POP {r3, r1, r7, r2} Lecture #7 15
Stack Examples PUSH {r3, r1, r7, r2} POP {r3, r1, r7, r2} Lecture #7 16
Stack Examples PUSH {r3, r1, r7, r2} POP {r3, r1, r7, r2} Lecture #7 17
Example: Swap R1 & R2 R1 0x11111111 R2 0x22222222 PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x20000200 xxxxxxxx xxxxxxxx xxxxxxxx Address 0x20000200 0x200001FC 0x200001F8 memory Lecture #7 18
Example: Swap R1 & R2 R1 0x11111111 R2 0x22222222 PC PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x200001FC xxxxxxxx 0x11111111 xxxxxxxx Address 0x20000200 0x200001FC 0x200001F8 memory Lecture #7 19
Example: Swap R1 & R2 R1 0x11111111 R2 0x22222222 PC PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x200001F8 xxxxxxxx 0x11111111 0x22222222 Address 0x20000200 0x200001FC 0x200001F8 memory Lecture #7 20
Example: Swap R1 & R2 R1 0x22222222 R2 0x22222222 PC PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x200001FC xxxxxxxx 0x11111111 xxxxxxxx Address 0x20000200 0x200001FC 0x200001F8 memory Lecture #7 21
Example: Swap R1 & R2 R1 0x22222222 R2 0x11111111 PC PUSH {R1} PUSH {R2} POP {R1} POP {R2} R13 (SP) 0x20000200 xxxxxxxx xxxxxxxx xxxxxxxx Address 0x20000200 0x200001FC 0x200001F8 Could you do this with one PUSH and One POP instruction? memory Lecture #7 22
Subroutine A subroutine, also called a function or a procedure, single-entry, single-exit Return to caller after it exits When a subroutine is called, the Link Register (LR), R14, holds the memory address of the next instruction to be executed after the subroutine exits (the instruction physically beyond the instruction that calls the subroutine). That is how it knows to get back to where it came from. Lecture #7 23
Subroutines Separate, independent module of program, performs a specific task shortens code modules (not necessarily total code) often provide reusable tools Good subroutine: independence: does not depend on other code, and calling code isn t affected by it (e.g. register values) registers: stores/restores key registers upon entrance/exit using PUSH and POP instructions as needed data and code independent: local variables, limited use of direct memory addressing, unless it accesses global (program-wide) data structures. Lecture #7 24
Link Register 32 bits Low Registers R0 R1 R2 R3 R4 R5 R6 R7 General Purpose Register Link Register (LR) holds the return address of the current subroutine call I.e. how to get back to where you were (the following instruction) when the subroutine was called R8 High Registers R9 R10 R11 32 bits xpsr R12 R13 (SP) R14 (LR) R13 (MSP) R13 (PSP) BASEPRI PRIMASK FAULTMASK Special Purpose Register R15 (PC) CONTROL Lecture #7 25
Call a Subroutine BL <Subroutine Name> to go to subroutine BX LR to return from subroutine Subroutine itself enclosed with PROC and ENDP Directives Caller Program MOV r4, #100... BL foo;call subroutine foo... ADD r4, r4, #1 ; r4 =? Subroutine/Callee foo PROC... MOV r4, #10 ; foo changes r4... BX LR ; return to caller ENDP Lecture #7 26
Preserve Runtime Environment using the Stack Subroutine must not change caller s r4 value Save it on the stack! Can this be idea be generalized? Yes! Caller Program MOV r4, #100... BL foo... ADD r4, r4, #1 ; r4 = 101, not 11 Subroutine/Callee foo PROC PUSH {r4} ; preserve r4... MOV r4, #10 ; foo changes r4... POP {r4} ; Recover r4 BX LR ENDP Lecture #7 27
Summary The Stack Subroutines Next Lecture: Subroutines and passing info Read Chapter 8 of text. Lab #3, starting this week is about Subroutines. Two week lab not a trivial lab Prework on this lab! 3 deliverables to TA Pre-read the entire lab to be better prepared Lecture #7 28