Download presentation
Presentation is loading. Please wait.
Published byMatilda Burns Modified over 9 years ago
1
© 2010 Kettering University, All rights reserved.
3
Microcomputers I – CE 320 Electrical and Computer Engineering Kettering University jkwon@kettering.edu http://www.kettering.edu/~jkwon Jaerock Kwon, Ph.D.
4
Announcements Midterm result Average: 68.81 Standard Dev: 13.25 Maximum: 95 Minimum: 41 Update grades on Blackboard Q1, Q2, Q3, and Midterm Lab1, Lab2, Lab3, and Practical exam Question 1, 2, and 3 in Lab2 I intended to have actual assembly code. Questions were not clearly specified. I will accept proper explanations as a right answer. Feel free to stop by my office after this class.
5
Midterm Review 1. Branch instructions BHI (unsigned), BGT (signed) Take a look at the preceding comparison instruction. Then, you can use this instead of using complex formula in the instruction reference. BRANCH IF REGISTER IS HIGHER/GREATHER/… THAN OPERAND e.g. CMPA #$D0 Branch if ‘A’ is HIgher than ‘operand.’ BMI, BVC, BNE Check the result if it is Minus/signed oVerflow/Not Equal to zero CMPA: A – (M) TSTA: A – 0 V bit (signed overflow) N + N P P + P N N – P P P – N N
6
Midterm Review – cont’d 2. BRA and LBRA Write a line of assembly code that begins in memory location $2450. BRA LBRA When the effective (destination) address is calculated in relative addressing mode, a value that you need to add to an offset is PC. We have to use PC which is the next address of the line of assembly code of (L)BRA
7
Midterm Review 3. Mostly this is about calculating postbyte(s) -20: You cannot use 5-bit offset. You have to use 9-bit one.
8
Lecture 16: Parameter Passing
9
Today’s Goals Parameter passing Understand how to pass parameters using the stack
10
Call by Value vs. Call by Reference Main Program Subroutine Value Subroutine Address Memory Look for values
11
Passing Parameters in Registers Use registers to pass parameters The simplest type of parameter passing Pros vs. Cons Data is immediately available to the subroutine No extra preparations are needed. Often fastest execution and smallest code size Only a limited number of registers are available. We can use ‘pass-by-reference’ to point a list of input/output values.
12
Example (Parameter Passing in Registers) Requirements A subroutine that adds an array of two-byte numbers and a sample main program that calls it. The array is passed by reference in X. The length is passed by value in D. The sum should be returned by value in D. Do not worry about indicating signed or unsigned overflow.
13
Example (Parameter Passing in Registers) ORG $3000 Arraydc.w$1234,$5678,$ABCD Lengthdc.w3 ORG$2000 LDS#$3600 LDX#Array ; load X with address of list LDDLength; Load D with actual length JSRsumword SWI sumwordTFRD,Y LDDD#0 CPY#0 LoopBEQendsum ADDD0,X INX DEY BRAloop endsumRTS
14
Preserving Registers In the previous example, the subroutine uses Y. The value in Y is destroyed. If Y was in use by the main program, the main program would continue after the subroutine with an incorrect value in Y. To avoid this, registers used by a subroutine may be saved to the stack before a subroutine uses them. The saved registers are restored after the subroutine. We have two options The caller (main program) does this. The callee (subroutine) does this.
15
Preserving Registers Responsibility of the Caller: The calling main program assumes all registers are destroyed by the subroutine If the registers used by the subroutine are unknown (i.e. using a sub. provided by someone else), may save registers that the subroutine wouldn’t affect Code to save/restore registers duplicated with every subroutine call. Responsibility of the Callee: Saves only the registers that will be used by the subroutine Save/restore code only occurs once
16
Example – Caller Responsible ORG $3000 Arraydc.w$1234,$5678,$ABCD Lengthdc.w3 ORG$2000 LDS#$3600 LDX#Array ; load X with address of list LDDLength; Load D with actual lengthPSHX JSRsumword PULX SWI sumwordTFRD,Y LDDD#0 CPY#0 loopBEQendsum ADDD0,X INX DEY BRAloop endsumRTS RAH RAL XH XL Stack Frame
17
Example – Callee Responsible ORG $3000 Arraydc.w$1234,$5678,$ABCD Lengthdc.w3 ORG$2000 LDS#$3600 LDX#Array ; load X with address of list LDDLength; Load D with actual length JSRsumword SWI PSHX sumwordPSHXPSHY TFRD,Y LDDD#0 CPY#0 loopBEQendsum ADDD0,X INX DEY BRAloop PULY endsumPULYPULX RTS XH XL RAH RAL YH YL Stack Frame Why not D? It is used to return the answer! Why not D? It is used to return the answer!
18
Passing Parameters in the Stack Pros and Cons The stack pointer is already in use for the return address Indexed addressing can easily access data stored on the stack The amount of data passed is not limited by the register set The data passed on the stack must be removed, and this is usually the responsibility of the caller (although this can be done by the callee) Note: If the caller will save registers on the stack, it should be done before passing parameters. Why? Those parameters are supposed to be used in the subroutine.
19
Example – Param Passing in the Stack ORG $3000 Arraydc.w$1234,$5678,$ABCD Lengthdc.w3 ORG$2000 LDS#$3600 LDX#$1234; something to save PSHX LDDLength PSHD LDD#Array PSHD JSRsumword LEAS4,SP PULX SWI sumwordLDDD#0 LDX2,SP LDY4,SP LoopBEQendsum ADDD0,X INX DEY BRAloop endsumRTS ArrH ArrL LenH LenL RetH RetL Stack Frame XH XL
20
Questions?
21
Wrap-up Parameter Passing Pros and Cons of each method What we’ve learned
22
What to Come More about subroutines
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.