ece 3561 - lecture 1 1 l9: subroutines department of electrical and computer engineering the ohio...

27
ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Upload: adrian-parrish

Post on 22-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

ECE 3561 - Lecture 1 1

L9: Subroutines

Department of Electrical and Computer EngineeringThe Ohio State University

ECE 2560

Page 2: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Subroutines

SubroutinesWhat are theyHow they can simplify a programAn example

StacksPassing data on the stackHow subroutine calls use the stack

ECE 3561 - Lecture 1 2

Page 3: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Stacks

The stack – an area of memory for storage of data accessed through the PUSH and POP instructions

ECE 3561 - Lecture 1 3

Page 4: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

The 430 stack

After initialization – where is the stack? After initialization the value in the SP register (R2)

is 0x0300 (can see this in Code Composer) Placing data on the stack

Done using a PUSH(.B) instruction PUSH(.B) src Push the source onto stack

Action is SP-2-> SP src->@SP

Removing data from the stack Done using a POP(.B) instruction POP(.B) dst Pop item from stack to destination

Action is @SP-> dst SP+2 -> SP

ECE 3561 - Lecture 1 4

Page 5: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Also in code composer

If you view memory it will show you data variables names at their location

To see action of stack will do some push and pop actions while watching memory

Will push values of 10 copies of $FFFF on the stack Then 10 pops to get the SP back to no data – pop

into R4 Then push $0000,$0110,$0220,etc up to $0990 on

the stack Again 10 pops back into R4 Also show what occurs when one is .B

ECE 3561 - Lecture 1 5

Page 6: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

In Demo

After initialization, the SP is set to value 0x0300.

Remember that the stack grows down in memory and the SP register points to where the last value pushed on the stack is. So when a value is pushed on the stack it, the SP register is decremented by 2 and then the data is stored in memory.

ECE 3561 - Lecture 1 6

Page 7: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Binary Multiplication

Need to perform integer multiplication of A*B. Will do this by simply adding up the value B, A times in a

loop. mult mov A,R5 ;A is in R5 mov B,R6 ;B is in R6 clr R7 ;R7 to accumulate sum mlp add R6,R7 ;add R6 to R7 dec R5 ;A=A-1 jne mlp ;repeat ;end R7=B*A

As register is 16 bits and treated as 2’s complement, can do up to a 7-bit x 7-bit multiply for a 14 bit result.

Number of times through the loop? 128 times max

ECE 3561 - Lecture 1 7

Page 8: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Now turn this into a subroutine

Could code up the previous instructions each time you need to do a multiply OR create a routine to do it. The routine needs the data to multiply passed to it.

Where to pass the data? Could do it in registers Could do it in memory Could do it on the stack

Set up before call move A and B to stack use push instruction

ECE 3561 - Lecture 1 8

Page 9: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Why one an not the other?

Why pass the data on the stack? Pass it in registers

Works fine but program may be using registers for other tasks.

Pass in variables in memory Works fine

Pass on the stack Also works fine

For a simple routine like this no real advantage of any method. Just shows how to do pass variables on the stack.

Will look at subroutine for all three.

ECE 3561 - Lecture 1 9

Page 10: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Demo of routine – use registers

Pick two register to pass data Pick R5, and R6 for numbers to be multiplied Pick R7 for the result Code it up – calling routine mov A,R5 ;A is in R5 mov B,R6 ;B is in R6 call #mult end jmp end ;the subroutine mult clr R7 ;R7 to accumulate sum mlp add R6,R7 ;add R6 to R7 dec R5 ;A=A-1 jne mlp ;repeat ret ;end R7=B*A .data A .word 3 B .word 5 RES .word 0

ECE 3561 - Lecture 1 10

Page 11: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Subroutine call/return

At end of subroutine code need a ret instruction. (see manual) RET Note action of instruction @SP PC SP + 2 SP

The CALL instruction action – again see manual. dst tmp SP-2 SP PC @SP tmp PC (tmp is not a register you can see)

ECE 3561 - Lecture 1 11

Page 12: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Demo of routine – use memory

Pick memory locations to pass data A and B are the data to be multiplied RES is the result Code it up – calling routine mov #3,&A mov #4,&B call #mult end jmp end ;the subroutine mult clr &RES ;RES to accumulate sum mlp add &B,RES ;add B to RES dec &A ;A=A-1 jne mlp ;repeat ret ;end RES=B*A .data A .word 3 B .word 5 RES .word 0

ECE 3561 - Lecture 1 12

Page 13: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

A third way – the stack

First two methods work, BUTLimitations

REGISTER USE is fixedSubroutine restricted to specific registersFor subroutine to have no side effects, must

protect any register it uses.

ECE 3561 - Lecture 1 13

Page 14: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

The main and subroutine

In Main program ;Setup for call mov A,R5 mov B,R6 push R5 push R6 call #srmult ;after return have to cleanup

SP will be pointing to B – result is Apop stack twice – 2nd time is result position

ECE 3561 - Lecture 1 14

Page 15: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Subroutine

Now in subroutinesrmult mov 2(SP),R6 ;B to R6 mov 4(SP),R5 ;A to R5 clr R7mlp add R6,R7 dec R5 jne mlp ;at end R7 is A*B mov R7,4(SP) ;R7 for rtn ret ;return from sr

ECE 3561 - Lecture 1 15

Page 16: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

But the call can be a problem

Have to make sure that call works correctly The first time this was done, call was transferring control all over

the place or so it seemed. ;in main ;subroutine mov A,R5 smult mov

2(SP),R6 mov B,R6 mov

4(SP),R5 push R5 ret push R6 mov #smult,R4 call R4 pop R7 pop R7 push R5 push R6 call #smult ;this code worked pop R7 pop R7 mov R7,res

ECE 3561 - Lecture 1 16

Page 17: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Get code working

When having problems get some code working and then build on it In this case will build the subroutineDemo

ECE 3561 - Lecture 1 17

Page 18: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Multiply routine

The multiply routine is important as there is no multiply instruction

Note that the routine is for positive integer multiplication only.

With the algorithm implemented cannot multiply a negative number

Both A and B and the result have to be between 0 and 215-1, positive integers

ECE 3561 - Lecture 1 18

Page 19: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Clean up the stack

After returning from subroutineA ret simply pops the return address from

the stack into the PC registerSo execution continues after the subroutine

call If data was passed on the stack it needs to

be cleaned up to retrieve the result and set the SP to before the call.

ECE 3561 - Lecture 1 19

Page 20: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

The pop instruction

The pop instruction removes the top item of the stack (the item currently pointed to by the SP register) and moves it to dst.POP(.B) dst Pop item from stack to

destinationAction is @SP-> dst SP+2 -> SP

ECE 3561 - Lecture 1 20

Page 21: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Cleanup on Return

Same code – note the pop instructions that remove data and result from the stack

;in main ;subroutine mov A,R5 smult mov

2(SP),R6 mov B,R6 mov

4(SP),R5 push R5 ret push R6 mov #smult,R4 call R4 call #smult ;this code worked pop R7 pop R7 mov R7,res

ECE 3561 - Lecture 1 21

Page 22: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Subroutine side effects

Subroutine should have no side effects!,i.e., change the values of registers or data the main program is using If subroutine should have no side effects then the state

of machine (registers, memory, etc.) should be the same after the RTS as before the call. What does that mean?

That means that subroutine needs to preserve any register that it will affect during execution of the subroutine.

What are they? (refer to slide 11) CALL and RET do not affect SR Action in subroutine will alter sr bits, and here contents of R5,

R6, and R7

ECE 3561 - Lecture 1 22

Page 23: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Items to preserve

The first item to preserve is the SRHow to do it? Push it onto the stack.

What else to save? Any register used.

ECE 3561 - Lecture 1 23

Page 24: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Saving of the SR (R(2))

Save the register you will use and SR.

Becomes ------- ->State of SR upon return is same as itwas before the call.

ECE 3561 - Lecture 1 24

smult mov 2(SP),R6mov 4(SP),R5clr R7

mlp add R6,R7dec R5jne mlpmov R7,4(SP)ret smult push R2

mov 4(SP),R6mov 6(SP),R5clr R7

mlp add R6,R7dec R5jne mlpmov R7,6(SP)pop R2ret

Page 25: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Save other registers

Subroutine used R5,R6,R7 – push on Stack

ECE 3561 - Lecture 1 25

Page 26: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

What is the code?

Note the change to the offsets of the passed arguments.

Now subroutine has no effect on calling program.

ECE 3561 - Lecture 1 26

smult push R2mov 4(SP),R6mov 6(SP),R5clr R7

mlp add R6,R7dec R5jne mlpmov R7,6(SP)pop R2ret

smult mov 2(SP),R6mov 4(SP),R5clr R7

mlp add R6,R7dec R5jne mlpmov R7,4(SP)ret

smult push R2push R5push R6push R7mov 10(SP),R6mov 12(SP),R5clr R7

mlp add R6,R7dec R5jne mlpmov R7,12(SP)pop R7pop R6pop R5pop R2ret

Page 27: ECE 3561 - Lecture 1 1 L9: Subroutines Department of Electrical and Computer Engineering The Ohio State University ECE 2560

Assignment

Code up a positive integer multiply subroutine as shown in this lecture

Description of this is on the webpage and is assignment HW6

ECE 3561 - Lecture 1 27