ceg 320/520: computer organization and assembly language programmingthe stack and subroutines 1 the...

28
CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 1 The Stack and Subroutines

Upload: sheryl-byrd

Post on 21-Dec-2015

223 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 1

The Stack and Subroutines

Page 2: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 2

The Stack: Introduction• A7 is a special address register, called the stack

pointer.• When programming assembly, we can use SP as an

alias for A7. MOVEA.L #$3000,SP• In the simulator, it is also called US (user stack

pointer)• There is also a supervisor stack pointer, but we won’t

worry about it yet.• Since our program usually starts at a low memory

address and grows upward, we start the stack at a high memory address and work downward.

Page 3: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 3

The Stack: Introduction

• We push values onto the stack using predecrement mode– MOVE.W D2,-(SP)– MOVE.W D3,-(SP)

• We pop values from the stack using postincrement mode– MOVE.W (SP)+, D3– MOVE.W (SP)+, D2

• Some instructions affect the stack directly

Page 4: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 4

The Stack: Operations (push/pop)

$7000$6FFE$6FFC

main MOVE.W #12,-(A7)MOVE.W #20,-(A7)MOVE.W #30,-(A7)…MOVE.W (A7)+,D0MOVE.W (A7)+,D1MOVE.W (A7)+,D2

PUSH(12) Last-in, first-outPUSH(20) (LIFO)PUSH(30)POP = 30POP = 20POP = 12

Page 5: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 5

The Stack: Purposes

• Temporary storage of variables

• Temporary storage of program addresses

• Communication with subroutines– Push variables on stack– Jump to subroutine– Clean stack– Return

Page 6: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 6

Programming Subroutines

• Why use subroutines?– Code re-use– Easier to understand code (readability)– Divide and conquer

• Complex tasks are easier when broken down into smaller tasks

• How do we call a subroutine in assembly?– Place the parameters somewhere known– JSR or BSR to jump to the subroutine– RTS to return

Page 7: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 7

C Assemblymain MOVE.W A,D1

JSR sqrMOVE.W D0,Bmove.w #228,D7TRAP #14

;*** subroutine sqr ***sqr MUL.W D1,D1

MOVE.W D1,D0RTSORIGIN $2000

A DC.W 5B DS.W 1

end

main() {int a, b;a = 5;b = sqr(a);printf(“%d\n” b);

}/* subrtn sqr */int sqr(int val) {int sqval;sqval = val * val;return sqval;

}

Page 8: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 8

JSR and BSR

• JSR label– MOVE.L PC, –(SP)– LEA address-of-label, PC

In other words:– SP [SP] – 4– [SP] [PC]– PC address-of-label

• BSR label– Same, but offset from the current PC is stored

instead of the absolute address

Page 9: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 9

JSR example

1000 MOVE.L #5,D11006 LEA ARRAY,A0100C JSR SUMARR1012 MOVE.L D0,SUM1018 MOVE.L #228, D7101E TRAP #141020 SUMARR CLR.L D0

…RTS

ARRAY DC.L 12,15,31SUM DS.L 1

END

$7000

00001012PC

?10120000

$6FFE$6FFC

00007000A7

Page 10: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 10

RTS

• RTS

• Pop the address from the stack back into the PC– MOVE.L (SP)+, PC

In other words:– PC [SP]– [SP] [SP] + 4

Page 11: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 11

RTS example

1000 MOVE.L #5,D11006 LEA ARRAY,A0100C JSR SUMARR1012 MOVE.L D0,SUM1018 MOVE.L #228, D7101E TRAP #141020 SUMARR CLR.L D0

…1032 RTS

ARRAY DC.L 12,15,31SUM DS.L 1

END

$7000 ?10120000

$6FFE$6FFC

00001034PC

00006FFCA7

Page 12: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 12

Passing parameters in registersmain MOVE.W A,D1

JSR sqrMOVE.W D0,Bmove.w #228,D7TRAP #14

;sqr MOVE.W D1,D0

MULS.W D0,D0RTSORIGIN $2000

A DC.W 5B DS.W 1

end

• The number to be squared is in D1.

• The result is returned in D0, D1 is unchanged.

Page 13: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 13

Parameter passing on the stack• If we use registers to pass our parameters:

– Limit of 7 parameters to/from any subroutine.– We use up registers so they are not available to our

program.

• So, instead we push the parameters onto the stack.

• Our conventions:– Parameters are passed on the stack– One return value can be provided in D0.– D0, D1, A0, A1 can be used by a subroutine.

Other registers must first be saved.

Page 14: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 14

First things first…

• Both the subroutine and the main program must know how many parameters are being passed!– In C we would use a prototype:int power (int number, int exponent);

• In assembly, you must take care of this yourself.

• After you return from a subroutine, you must also clear the stack. You have to clean up your mess.

Page 15: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 15

Passing parameters on the stackMul3 – multiply three numbers and place the result in D0.Mul3 – multiply three numbers and place the result in D0.; ******** Main Program ********************************1000 START MOVE.W NUM1,-(SP) ;Push first param1006 MOVE.W NUM2,-(SP) ;Push 2nd param100C MOVE.W NUM3,-(SP) ;Push 3rd param1012 JSR MUL31018 ADDA.L #6,SP ;Clean the stack!101E MOVE.W #228,D71020 TRAP #14; ******* Subroutine Mul3 ******************************1022 MUL3 MOVE.W 4(SP),D0 ;D0 = NUM31026 MULS.W 6(SP),D0 ;D0 *= NUM2102A MULS.W 8(SP),D0 ;D0 *= NUM1102E RTS ;SP --> rtrn addr!

ORG $20002000 NUM1 DC.W 52002 NUM2 DC.W 82004 NUM3 DC.W 2

END

Page 16: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 16

Know how to…

• Push parameters onto the stack

• Access parameters on the stack using indexed addressing mode

• Draw the stack to keep track of subroutine execution– Parameters– Return address

• Clean the stack after a subroutine call

Page 17: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 17

Review problem; ******** Main Program *********1000 START MOVE.L NUM1,-(SP)1006 MOVE.L NUM2,-(SP)100C MOVE.L NUM3,-(SP)1012 JSR SUB11018 Next instr…… …; ******* Subroutine SUB1 *******1022 SUB1 …1026 …102E RTS

ORG $20002000 NUM1 DC.L $1502004 NUM2 DC.L $1802008 NUM3 DC.L $12

END

Page 18: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 18

Writing transparent subroutines• A transparent subroutine doesn’t change any

registers except D0, D1, A0 and A1.• If we need more registers than this, we must

save the register values when we enter the subroutine and restore them later.

• Where do we store them? You guessed it: the stack.

• The 68000 provides a convenient instruction, MOVEM, to push the contents of several registers to the stack at one time.

Page 19: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 19

A transparent subroutinesubr1 MOVEM.L D2-D3/A2-A3,-(SP)

MOVE.L #32,D2MOVE.L 20(SP),D3…MOVEM.L (SP)+,D2-D3/A2-A3RTS

$7000 ?

rtrnrtrn

$6FFE$6FFC

D2D2D3D3A2A2A3A3

P1P2

We can now safely modify D2, D3, We can now safely modify D2, D3, A2 and A3, knowing that we will A2 and A3, knowing that we will

restore their original contents later.restore their original contents later.

We saved 4 registers, so the last We saved 4 registers, so the last parameter lives at SP + (4parameter lives at SP + (4×4) + 4. ×4) + 4.

(4 bytes/reg + 4 for the return addr.)(4 bytes/reg + 4 for the return addr.)

Page 20: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 20

Review problem #2; ******** Main Program *********1000 START MOVE.L NUM1,-(SP)1006 MOVE.L NUM2,-(SP)100C MOVE.L NUM3,-(SP)1012 JSR SUB11018 Next instr…… …; ******* Subroutine SUB1 *******1022 SUB1 MOVEM.L D2-D3/A4,-(SP)1026 … ; show stack102E MOVEM.L (SP)+,D2-D3/A41032 RTS

ORG $20002000 NUM1 DC.L $1502004 NUM2 DC.L $1802008 NUM3 DC.L $12

END

Page 21: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 21

Know how to…

• Write transparent subroutines

• Draw the stack and access parameters on the stack, taking into account the saved register values and the subroutine return address.

Page 22: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 22

Passing by value & reference

• We pushed the value of NUM1, NUM2, and NUM3 on the stack.

• What if we want to change the input parameter values?

• For example, what if we want to write a subroutine that will multiply all of its arguments’ values by 2, actually changing the values in memory?

• We must pass the parameters by reference…

Page 23: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 23

The PEA instruction

• PEA – Push effective address

PEA labelis the same as…

LEA label,A0MOVEA.L A0,-(SP)

but without using A0.

Page 24: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 24

Passing parameters by referencedbl3 – double the values of three parametersdbl3 – double the values of three parameters

; ******** Main Program *********1000 START PEA NUM11006 PEA NUM2100C PEA NUM31012 JSR dbl31018 ADDA.L #12,SP101E MOVE.W #228,D71020 TRAP #14

ORG $20002000 NUM1 DC.W 52002 NUM2 DC.W 82004 NUM3 DC.W 2

END

$7000 ?

20020000

$6FFE$6FFC

2004000010180000

20000000

Page 25: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 25

Using parameters passed by referencedbl3 – double the values of three parametersdbl3 – double the values of three parameters

; ******* Subroutine dbl3 *********DBL3 MOVEA.L 4(SP),A0

MOVE.W (A0),D0MULS.W #2,D0MOVE.W D0,(A0)MOVEA.L 8(SP),A0… ; repeat for eachRTSORG $2000

2000 NUM1 DC.W 52002 NUM2 DC.W 82004 NUM3 DC.W 2

END?

200200002004000010180000

20000000

$7000$6FFE$6FFC

Page 26: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 26

Putting it all togetherMAIN MOVE.W COUNT,-(SP)

PEA ARRAYJSR SUB2…

SUB2 MOVEM.L D2-D3,-(SP);HEREORG $2000

COUNT DC.W 105ARRAY DC.W 10,12,3,7,9,18

• What does the stack look like at the line;HERE ?

Page 27: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 27

Characteristics of good subroutines• Generality – can be called with any arguments

– Passing arguments on the stack does this.

• Transparency – you have to leave the registers like you found them, except for D0, D1, A0, and A1.– We use the MOVEM instruction for this purpose.

• Readability – well documented.– See the example handout.

• Re-entrant – subroutine can call itself if necessary– This is done using stack frames, something we will look at

in a few days…

Page 28: CEG 320/520: Computer Organization and Assembly Language ProgrammingThe Stack and Subroutines 1 The Stack and Subroutines

CEG 320/520: Computer Organization and Assembly Language Programming The Stack and Subroutines 28

Know how to…

• Pass parameters by value and by reference

• Use address registers to access parameters passed by reference

• Write general, transparent, readable subroutines