80 likes | 217 Views
Outline Local Variables Link/Unlink Instructions Local Variable Usage Control Constructs Goal Understand instruction usage Reading Microprocessor Systems Design, Clements, Ch. 2-3. 680XX Program Examples 2. Subroutines need local workspace permanent or static variables
E N D
Outline Local Variables Link/Unlink Instructions Local Variable Usage Control Constructs Goal Understand instruction usage Reading Microprocessor Systems Design, Clements, Ch. 2-3 680XX Program Examples 2
Subroutines need local workspace permanent or static variables C example: static firsttime = 1; temporary variables allocated at subroutine entry deallocated on subroutine exit C example: int i, j; want subroutines to be reentrant temp vars cannot be in fixed memory locations multiple active subroutine calls would collide Solution allocate static variable space in memory allocate temporary variable space on stack Local Variables
Stack frame (SF) region of temporary storage on top of stack move stack pointer up by d locations at start of subroutine LEA &-200(SP),SP - allocate 200 bytes, stack grows down use A7/SP to access local variables (SP), &4(SP), &8(SP), ... LEA &200(SP),SP - deallocate 200 bytes Stack Usage SP Stack Frame d SP Return Address Return Address Stack after SF allocated by subroutine Stack after call
Automate allocation/deallocation of stack frames address register stores length of stack frame in general different for each subroutine Subroutine entry MOVEM.L D0-D7/A3-A6,-(SP) Save working registers LINK A1,#&-64 Allocate 64 bytes on SF Link action [SP] <-- [SP] - 4 Push contents of A1 [M([SP])] <-- [A1] [A1] <-- [SP] Save SP in A1 [SP] <-- [SP] - 64 Move SP up by 64 Note old A1 saved on stack, old SP in A1 Link/Unlink Instructions
Frame usage LEA (SP),A2 - load first free address of frame into A2 offset from A2 into rest of frame - frame pointer could also use A1 or SP as reference into frame Nested calls - multiple frames on stack new subroutine issues LINK A1,#-d A1 now points to old A1 on stack, old A1 points to old old A1 the links between stack frames Subroutine return UNLK A1 Deallocate current SF MOVEM.L (SP)+,D0-D7/A3-A6 Restore registers RTS Return to caller Unlink action [SP] <-- [A1] Restore old SP [A1] <-- [M([SP])] Pop old A1 off stack [SP] <-- [SP] + 4 Link/Unlink Instructions
Compute R = (P2 + Q2)/(P2 - Q2) MOVE.W D0,-(SP) Push P MOVE.W D1,-(SP) Push Q PEA R Push reference to R BSR CR Call subroutine LEA 8(SP),SP Pop P,Q,R off stack ... CR MOVEM.L D6/A6,-(SP) Save working registers LINK A0,#&-8 Allocate 8-byte stack frame MOVE.W &22(A0),D6 Get P MULU.W D6,D6 Calc P2 MOVE.L D6,&-4(A0) Save on SF MOVE.L D6,&-8(A0) Save again MOVE.W &20(A0),D6 Get Q MULU.W D6,D6 Calc Q2 ADD.L D6,&-4(A0) Store P2 + Q2 on SF SUB.L D6,&-8(A0) Store P2 - Q2 on SF MOVE.L &-4(A0),D6 Get P2 + Q2 DIVU.W -6(A0),D6 Calc (P2 + Q2)/(P2 - Q2) LEA &16(A0),A6 Get ptr to addr of R MOVEA.L (A6),A6 Get address of R MOVE.W D6,(A6) Modify R in caller UNLK A0 Deallocate SF MOVEM.L (SP)+,D6/A6 Restore working registers RTS Return Local Variable Usage
if L {S} TST.B D0 L in D0 - 0 or 1 BEQ.B EXIT If 0 skip S S EXIT if L {S1} else {S2} TST.B D0 BEQ.B ELSE If 0 go to S2 S1 BRA.B EXIT Skip over S2 ELSE S2 EXIT for (i = N1; i <= N2; i++) {S} MOVE.B #N1,D1 D1 is i NEXT S Loop body ADDQ.B #1,D1 i++ CMP.B #N2+1,D1 i <= N2 BNE NEXT Repeat until i = N2+1 Control Constructs
while L {S} REPEAT TST.B D0 D0 is 0 or 1 BEQ.B EXIT Quit if 0 S Execute loop body BRA REPEAT Repeat EXIT do {S} while L NEXT S Execute loop body TST.B D0 D0 is 0 or 1 BNE NEXT Repeat until 0 for (i = N; i >= 0; i--) {S} MOVE.W #N,D1 i = N NEXT S Execute loop body DBRA D1,NEXT i--, loop if not -1 Control Constructs