Text preview for : passagePar.pdf part of MAAS KNT-5000 Stabilized DC Power Supply MAAS KNT-5000



Back to : Maas KNT-5000.pdf | Home

University of Florida
Department of Electrical & Computer Engineering

EEL 4744 PARAMETER PASSING

Drs. E. M. Schwartz &A. Antonio Arroyo

Professors in ECE
21-Aug-98 5:48 PM

Page 1/5

PARAMETER PASSING METHODS How do you pass parameters between the subroutines (or interrupts) and the main routine or other subroutines? 1. 2. Pass the parameter(s) (data or pointer) in the internal registers. Pass the parameter(s) immediately after the call instruction, i.e. in the program memory space. (This requires that the parameter(s) be fixed at assembler time.) Pass a pointer to the location of the parameter(s) immediately after the call instruction. Pass the parameter(s) on the stack prior to the call. (PSH) Pass a pointer to parameter(s) on the stack prior to the call. (PSH)

3. 4. 5.

The Problem: Find the average of two numbers Solution 1: Solution 1a: START: Pass the parameter(s) in the internal registers. Pass the parameter data in the internal registers. $B600 #$0041 #$37 #$A3 AVG ;Start program at $B600 ;initialize stack pointer ;Load data in the A and B ; registers ;Call the subroutine AVG to ; get average

ORG LDS LDAA LDAB JSR

... ******************************************************* * Get average of inputs in accumulators A and B * Output in accumulator A AVG: ABA ASRA ;A=A+B ;Shift A right by 1 bit ; (divide by 2) keeping ; bit 7 (for sign ; extension)

RTS Solution 1b: DATA: START: ORG FCB ORG LDS LDX JSR Pass the parameters addresses in internal pointer registers. $0000 $37, $A3 $B600 #$0041 #DATA AVG1 ;Start program at $B600 ;initialize stack pointer ;Load X index reg. with ; address of data ;Call the subroutine AVG1 ; to get average

... ***************************************************** * Get average of two data bytes in successive memory * starting at location pointed to by X; Output in A AVG1: LDAA ADDA ASRA 0,X 1,X ;A = 1st piece of data ;A=A + 2nd piece of data ;Divide by 2

University of Florida
Department of Electrical & Computer Engineering

EEL 4744 PARAMETER PASSING

Drs. E. M. Schwartz &A. Antonio Arroyo

Professors in ECE
21-Aug-98 5:48 PM

Page 2/5 RTS

Solution 2 Pass the parameter(s) immediately after the call instruction, i.e., in the program memory space. (This requires that the parameter(s) be fixed at assemble time.) Since data follows the call, the return address pushed on the top of the stack by the subroutine call must be corrected before returning from the subroutine. ORG $B600 ;Start program at $B600 LDS #$0041 ;initialize stack pointer JSR AVG2 ;Call the subroutine AVG2 DATA1: FCB $37 DATA2: FCB $A3 NEXT: ... **************************************************** * Get average of two data bytes in program memory at * location pointed to by data on top of stack * Output in A * X affected * Stack return address corrected ORG $B700 AVG2: PULX ;Get address of data LDAA 0,X ;Get first piece of data ; (at DATA1) into A ADDA 1,X ;A=A + (data at DATA2) ASRA ;Divide by 2;Result in A INX ;Increment the X index INX ; register twice to point ; to next instruction upon ; RTS PSHX ;Push the corrected address ; back on stack RTS START:

LABEL START

DATA1 DATA2 NEXT

Address $B600 $B601 $B602 $B603 $B604 $B605 $B606 $B607

Value $8E $41 $BD $B7 $00 $37 $A3 ...

$003B $003C $003D $003E $003F $0040 $0041

$B6 (Data1 Hi) $05 (Data1 Lo)

University of Florida
Department of Electrical & Computer Engineering

EEL 4744 PARAMETER PASSING

Drs. E. M. Schwartz &A. Antonio Arroyo

Professors in ECE
21-Aug-98 5:48 PM

Page 3/5

Solution 3 Pass a pointer to the location of the parameter(s) immediately after the call instruction. Similar to Solution 2 except now a pointer to the data is passed (instead of the data itself), so that the data does not have to be know at assemble time. LOCAT: ORG FCB $0001 $37, $A3 $B600 #$0041 AVG3 LOCAT ;Define location of data ; and some default data ;Start program at $B600 ;initialize stack pointer ;Call the subroutine AVG3 ;Define the location of ;data

ORG LDS JSR DAT_AD: FDB START:

NEXT: ... **************************************************** * Get average of two data bytes in program memory at * location pointed to by pointer on top of stack * Output in A * X affected * Stack return address corrected AVG3: ORG PULX INX INX PSHX DECX DECX LDX LDAA ADDA ASRA RTS $B700 ;Get address of pointer to ; data ;Increment the X index ; register twice to point ; to next instruction ; upon RTS ;Push the corrected address ; back on stack ;Make X point to pointer at ; DAT_AD ;Put the pointer into X ;Get first piece of data ;A=A + (data at DATA2) ;Divide by 2

0,X 0,X 1,X

LABEL START

DATA_AD DATA2 NEXT

Address $B600 $B601 $B602 $B603 $B604 $B605 $B606 $B607

Value $8E $41 $BD $B7 $00 $00 $01 ...

$003B $003C $003D $003E $003F $0040 $0041

$B6 (DAT_AD Hi) $05 (DAT_AD Lo)

University of Florida
Department of Electrical & Computer Engineering

EEL 4744 PARAMETER PASSING

Drs. E. M. Schwartz &A. Antonio Arroyo

Professors in ECE
21-Aug-98 5:48 PM

Page 4/5

Solution 4 Pass the parameter(s) on the stack prior to the call. (PSH) ORG $B600 ;Start program at $B600 LDS #$0041 ;initialize stack pointer LDAA #$37 ;Load data onto stack PSHA LDAA #$A3 PSHA JSR AVG4A ;Call the subroutine AVG4A/AVG4B ... ******************************************************* * Get the average of the inputs on the stack * Output in accumulator A * A,B,X affected * Stack modified ORG $B700 AVG4A: PULX ;Save return address PULA ;Get second piece of data PULB ABA ;A=A+B ASRA ;Divide by 2 PSHX ;Fix stack for return ; address RTS START: ******************************************************* * Get the average of the inputs on the stack * Output in accumulator A * A,X affected * Stack unmodified ORG $B700 AVG4B: TSX ;IX = SP+1 & IX points to the stack LDAA 2,X ;A = 1st parameter ADDA 3,X ;A = A + 2nd parameter ASRA ;Divide by 2 RTS

University of Florida
Department of Electrical & Computer Engineering

EEL 4744 PARAMETER PASSING

Drs. E. M. Schwartz &A. Antonio Arroyo

Professors in ECE
21-Aug-98 5:48 PM

Page 5/5

Solution 5 Pass the address of the parameter(s) on the stack prior to the call. ORG LOCAT1: FCB ORG LOCAT2: FCB ORG START: $0001 $37 0006H $A3 $B600 ;Define location of data ; and some default data ;Start program at $B600

(PSH)

LDS #$0041 ;initialize stack pointer LDX #LOCAT2 ;Put location of 2nd data PSHX ; onto stack LDX #LOCAT1 ;Put location of 1st data PSHX ; onto stack JSR AVG5A ;Call the subroutine AVG5A/5B ... ******************************************************* * Get average of numbers pointed to by addresses * on stack * Output in accumulator A * A,B,X,Y affected; stack modified ORG $B700 AVG5A PULX ;Save return address PULY ;Get address of 1st data LDAA 0,Y ;Put first data in A PULY ;Get address of 2nd data ADDA 0,Y ;A=A + 2nd data ASRA ;Divide by 2 PSHX ;Fix stack for return add. RTS ******************************************************* * Get average of numbers pointed to by addresses * on stack * Output in accumulator A * A,B,X,Y affected; stack unmodified ORG $B700 AVG5B: TSX ;IX = SP+1 & IX points to the stack LDY 2,X ;Get address of 1st data LDAA 0,Y ;Put first data in A LDY 4,X ;Get address of 1st data ADDA 0,Y ;A=A + 2nd data ASRA ;Divide by 2 RTS · · What if you wanted the subroutine to have no effect on any registers? Can you think of any other ways to pass data? How about using RAM for variables!