68000 StackRelated Instructions PEA EA Push Effective Address

  • Slides: 17
Download presentation
68000 Stack-Related Instructions PEA <EA> Push Effective Address • Calculates an effective address <ea>

68000 Stack-Related Instructions PEA <EA> Push Effective Address • Calculates an effective address <ea> and pushes it onto the stack pointed at by address register A 7 (the stack pointer, SP). • The difference between PEA and LEA – LEA loads an effective address in any address register. – PEA pushes an effective address onto the stack. • PEA <EA> is equivalent to: LEA MOVEA. L <EA>, Ai Ai, -(A 7) Where Ai is an address register other than A 7 (A 0 -A 6) EECC 250 - Shaaban #1 Lec # 6 Winter 99 12 -15 -99

The MOVE Multiple: MOVEM Instruction • This instruction saves or restores multiple registers. •

The MOVE Multiple: MOVEM Instruction • This instruction saves or restores multiple registers. • Useful in subroutines to save the values of registers not used to pass parameters. MOVEM has two forms: MOVEM register_list, <ea> MOVEM <ea>, register_list • No effect on CCR. Example: SUBR 1 MOVEM. . . MOVEM RTS Saving/restoring registers to from memory D 0 -D 7/A 0 -A 6, SAVEBLOCK SAVE D 0 -D 7/A 0 -A 6 SAVEBLOCK, D 0 -D 7/A 0 -A 6 Restore D 0 -D 7/A 0 -A 6 Example: Saving/restoring registers using the stack (preferred method). SUBR 1 MOVEM. . . MOVEM RTS D 0 -D 7/A 0 -A 6, -(SP) Push D 0 -D 7/A 0 -A 6 onto the stack (SP)+, D 0 -D 7/A 0 -A 6 Restore D 0 -D 7/A 0 -A 6 from the stack EECC 250 - Shaaban #2 Lec # 6 Winter 99 12 -15 -99

Example: Power Calculation Subroutine • A subroutine is needed which accepts two integers as

Example: Power Calculation Subroutine • A subroutine is needed which accepts two integers as input parameters: – a base, B (a signed integer), Size = one byte (range: -128 £ B £ 127) – an exponent E (a positive integer) Size = one byte, – and, compute the function BE size of answer = long word Functional specification (pseudo code) of subroutine POWER: POWER (B, E) D 1 = B D 2 = E ; input arguments, base ; exponent, a positive integer initialize D 3 to 1 while D 2 > 0 D 3 = D 1*D 3 D 2 = D 2 - 1; end POWER Return to calling program ; answer initialized to 1 ; compute function using ; continued product of base EECC 250 - Shaaban #3 Lec # 6 Winter 99 12 -15 -99

Basic Flow Chart of Power POWER Subroutine Start Point Effect on The Stack Word

Basic Flow Chart of Power POWER Subroutine Start Point Effect on The Stack Word D 1 = base D 2 = exponent D 3 = 1 SP during subroutine D 3 = D 3 * D 1 Initial SP (and after return from subroutine) D 2 = D 2 - 1 No Yes D 2 = 0 ? Return address Return to calling program EECC 250 - Shaaban #4 Lec # 6 Winter 99 12 -15 -99

POWER: Four Parameter Passing Cases • We’ll examine four assembly versions of the subroutine

POWER: Four Parameter Passing Cases • We’ll examine four assembly versions of the subroutine POWER and sample Main programs that calls it. • Each version uses a different parameter passing method: – Case 1: Parameter passing by value, using data registers. – Case 2: Parameter passing by reference, using address registers. – Case 3: Parameter passing by value, using the stack. – Case 4: Parameter Passing by reference, using the stack EECC 250 - Shaaban #5 Lec # 6 Winter 99 12 -15 -99

POWER Subroutine Example (Case 1) Parameter Passing by Value: Using Data Registers - Main

POWER Subroutine Example (Case 1) Parameter Passing by Value: Using Data Registers - Main Program MAIN B E A ORG MOVEA. L MOVE. B EXT. W CLR. W MOVE. B BSR LEA MOVE. L STOP ORG DC. B DS. L $400 #$07 FFE, SP B, D 1 D 2 E, D 2 POWER A, A 5 D 3, (A 5) #$2700 $600 4 2 1 Main Program origin Initialize Stack Pointer Put base number into D 1 Sign extend base to word length Clear D 2 before loading exponent Put exponent number into D 2 Call subroutine POWER put address of answer into A 5 save answer Done Base number stored here Exponent number stored here answer to be stored here EECC 250 - Shaaban #6 Lec # 6 Winter 99 12 -15 -99

POWER Subroutine Example (Case 1) Parameter Passing by Value: Using Data Registers Continued -

POWER Subroutine Example (Case 1) Parameter Passing by Value: Using Data Registers Continued - Subroutine ORG POWER MOVE. L LOOP MULS SUB BNE RTS $800 #1, D 3 D 1, D 3 #1, D 2 LOOP Subroutine POWER origin initialize result to 1 multiply result with base decrement power by one and repeat as long as power > 0 Done, return to calling program EECC 250 - Shaaban #7 Lec # 6 Winter 99 12 -15 -99

POWER Subroutine Example (Case 2) Parameter Passing by Reference: Using Address Registers - Main

POWER Subroutine Example (Case 2) Parameter Passing by Reference: Using Address Registers - Main Program MAIN B E A ORG MOVEA. L LEA BSR LEA MOVE. L STOP ORG DC. B DS. L $400 #$07 FFE, SP B, A 1 E, A 2 POWER A, A 5 D 3, (A 5) #$2700 $600 4 2 1 Main Program origin Initialize Stack Pointer A 1 points to base number A 2 points to exponent Call subroutine POWER put address of answer into A 5 save answer in memory Done Base number stored here Exponent number stored here answer to be stored here EECC 250 - Shaaban #8 Lec # 6 Winter 99 12 -15 -99

POWER Subroutine Example (Case 2) Parameter Passing by Reference: Using Address Registers Continued -

POWER Subroutine Example (Case 2) Parameter Passing by Reference: Using Address Registers Continued - Subroutine ORG POWER MOVE. B EXT. W CLR. W MOVE. B MOVE. L LOOP MULS SUB BNE RTS $800 (A 1), D 1 D 2 (A 2), D 2 #1, D 3 D 1, D 3 #1, D 2 LOOP Subroutine POWER origin copy base number to D 1 Sign extend base to word length Clear D 2 before loading exponent copy exponent to D 2 initialize result in D 3 to 1 multiply result D 3 with base D 1 decrement power in D 2 by one and repeat as long as power > 0 Done, return to calling program EECC 250 - Shaaban #9 Lec # 6 Winter 99 12 -15 -99

68000 Addressing Modes Revisited: Address Register Indirect Addressing with Displacement • The addressing notation:

68000 Addressing Modes Revisited: Address Register Indirect Addressing with Displacement • The addressing notation: d 16(A 0) or (d 16, A 0) – Refers to the address contained in register A 0 plus a signed 16 bit displacement d 16 – Some assembles accept only one of the above syntax forms. • Examples: MOVE. L (12, A 4), D 3 or MOVE. L 12(A 4), D 3 MOVE. W (-$4, A 1), D 0 or MOVE. W -$4(A 1), D 0 EECC 250 - Shaaban #10 Lec # 6 Winter 99 12 -15 -99

POWER Subroutine Example (Case 3) Parameter Passing by Value: Using The Stack - Main

POWER Subroutine Example (Case 3) Parameter Passing by Value: Using The Stack - Main Program MAIN B E A ORG MOVEA. L MOVE. B EXT. W MOVE. W CLR. W MOVE. B MOVE. W BSR MOVE. L LEA MOVE. L STOP ORG DC. B DS. L $400 #$07 FFE, SP B, D 1 D 1, -(SP) D 2 E, D 2, -(SP) POWER (SP)+, D 3 A, A 5 D 3, (A 5) #$2700 $600 4 2 1 Main Program origin Initialize Stack Pointer Put base number into D 1 Sign extend base to word length push base B onto the stack Clear D 2 before loading exponent Put exponent number into D 2 push exponent E onto the stack Call subroutine POWER pop answer from stack resetting SP put address of answer into A 5 save answer Done Base number stored here Exponent number stored here answer to be stored here EECC 250 - Shaaban #11 Lec # 6 Winter 99 12 -15 -99

POWER Subroutine Example (Case 3) Parameter Passing by Value: Using The Stack Continued -

POWER Subroutine Example (Case 3) Parameter Passing by Value: Using The Stack Continued - Subroutine ORG POWER MOVE. W CLR. W MOVE. B MOVE. L LOOP MULS SUB BNE MOVE. L RTS $800 6(SP), D 1 D 2 4(SP), D 2 #1, D 3 D 1, D 3 #1, D 2 LOOP D 3, 4(SP) Subroutine POWER origin copy base from stack to D 1 Clear D 2 before loading exponent copy exponent from to D 2 initialize result in D 3 to 1 multiply result D 3 with base D 1 decrement power in D 2 by one and repeat as long as power > 0 Push result onto the stack Done, return to calling program EECC 250 - Shaaban #12 Lec # 6 Winter 99 12 -15 -99

Just before calling Subroutine Word Effect on The Stack Just after calling Subroutine Current

Just before calling Subroutine Word Effect on The Stack Just after calling Subroutine Current SP Exponent E 4 Base B 8 Word Return address Exponent E Initial SP Base B Initial SP Just before return to main Current SP Word Return address Just before end of main Word 8 Answer Initial SP (Case 3) Current SP = Initial SP EECC 250 - Shaaban #13 Lec # 6 Winter 99 12 -15 -99

POWER Subroutine Example (Case 4) Parameter Passing by Reference: Using The Stack - Main

POWER Subroutine Example (Case 4) Parameter Passing by Reference: Using The Stack - Main Program MAIN B E A ORG MOVEA. L PEA PEA BSR LEA STOP ORG DC. B DS. L $400 #$07 FFE, SP B E A POWER 12(SP), SP #$2700 $600 4 2 1 Main Program origin Initialize Stack Pointer Push address of Base onto the stack Push address of Exponent onto the stack Push address of Answer onto the stack Call subroutine POWER Stack clean-up: stack pointer reset Done Base number stored here Exponent number stored here answer to be stored here EECC 250 - Shaaban #14 Lec # 6 Winter 99 12 -15 -99

POWER Subroutine Example (Case 4) Parameter Passing by Reference: Using The Stack Continued -

POWER Subroutine Example (Case 4) Parameter Passing by Reference: Using The Stack Continued - Subroutine ORG POWER MOVEA. L MOVE. B EXT. W CLR. W MOVE. B MOVE. L LOOP MULS SUB BNE MOVE. L RTS $800 12(SP), A 1 8(SP), A 2 4(SP), A 3 (A 1), D 1 D 2 (A 2), D 2 #1, D 3 D 1, D 3 #1, D 2 LOOP D 3, (A 3) Subroutine POWER origin load Base address in A 1 load Exponent address in A 2 load Answer address in A 3 Put base number into D 1 Sign extend base to word length Clear D 2 before loading exponent copy exponent from to D 2 initialize result in D 3 to 1 multiply result D 3 with base D 1 decrement power in D 2 by one and repeat as long as power > 0 Save result in memory Done, return to calling program EECC 250 - Shaaban #15 Lec # 6 Winter 99 12 -15 -99

Effect on The Stack Just before calling Subroutine Word Just after calling Subroutine Current

Effect on The Stack Just before calling Subroutine Word Just after calling Subroutine Current SP PEA PEA B E A Initial SP (Case 4) Answer Address Exponent Address BSR POWER Base Address Word Return Address Answer Address 0 +4 Exponent Address +8 Base Address +12 Initial SP EECC 250 - Shaaban #16 Lec # 6 Winter 99 12 -15 -99

Effect on The Stack Just after return to main Current SP RTS Initial SP

Effect on The Stack Just after return to main Current SP RTS Initial SP (Case 4) Just after stack clean-up in Main Word Answer Address 0 Exponent Address +4 Base Address +8 LEA Word 12(SP), SP Current SP = +12 Initial SP EECC 250 - Shaaban #17 Lec # 6 Winter 99 12 -15 -99