© 2010 Kettering University, All rights reserved. Microcomputers I – CE 320 Jaerock Kwon, Ph.D. Electrical and Computer Engineering Kettering University jkwon@kettering.edu http://www.kettering.edu/~jkwon 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. 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+NP P+PN N–PP P–NN Midterm Review – cont’d • 2. BRA and LBRA Write a line of assembly code that begins in memory location $2450. BRA <8-bit signed offset value> LBRA <16-bit signed offset value> 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 Midterm Review • 3. Mostly this is about calculating postbyte(s) -20: You cannot use 5-bit offset. You have to use 9-bit one. Lecture 16: Parameter Passing Today’s Goals • Parameter passing • Understand how to pass parameters using the stack Call by Value vs. Call by Reference Value Subroutine Main Program Memory Address Subroutine Look for values 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. 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. Example (Parameter Passing in Registers) Array Length ORG dc.w dc.w $3000 $1234,$5678,$ABCD 3 ORG LDS LDX LDD JSR SWI $2000 #$3600 #Array ; load X with address of list Length ; Load D with actual length sumword sumword TFR LDDD CPY Loop BEQ ADDD INX INX DEY BRA endsum RTS D,Y #0 #0 endsum 0,X loop 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. 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 Example – Caller Responsible Array Length JSR ORG dc.w dc.w $3000 $1234,$5678,$ABCD 3 ORG LDS LDX LDD PSHX sumword PULX SWI $2000 #$3600 #Array Length sumword TFR LDDD CPY loop BEQ ADDD INX INX DEY BRA endsum RTS D,Y #0 #0 endsum 0,X ; load X with address of list ; Load D with actual length Stack Frame RAH RAL XH loop XL Example – Callee Responsible Array Length sumword TFR loop endsum RTS ORG dc.w dc.w ORG LDS LDX LDD JSR SWI PSHX PSHY D,Y LDDD CPY BEQ ADDD INX INX DEY BRA PULY PULX $3000 $1234,$5678,$ABCD 3 $2000 #$3600 #Array ; load X with address of list Length ; Load D with actual length sumword #0 #0 endsum 0,X Why not D? It is used to return the answer! Stack Frame YH YL XH loop XL RAH RAL 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. Example – Param Passing in the Stack ORG dc.w dc.w $3000 $1234,$5678,$ABCD 3 ORG LDS LDX PSHX LDD PSHD LDD PSHD JSR LEAS PULX SWI $2000 #$3600 #$1234 sumword LDDD LDX LDY Loop BEQ ADDD INX INX DEY BRA endsum RTS #0 2,SP 4,SP endsum 0,X Array Length ; something to save Length #Array sumword 4,SP Stack Frame RetH RetL ArrH ArrL LenH LenL XH loop XL Questions? Wrap-up What we’ve learned • Parameter Passing • Pros and Cons of each method What to Come • More about subroutines