Example #30 -- Reference Parameters <1 arctic:~/Examples >cat swap.cpp #include void swapA( int * X, int * Y ) { int Z = *X; *X = *Y; *Y = Z; } void swapB( int & X, int & Y ) { int Z = X; X = Y; Y = Z; } /********** hand-written assembly language ********** .global swapA .section ".text" .align 4 swapA: save %sp, -96, %sp ld [%i0], %l0 ld [%i1], %l1 st %l1, [%i0] st %l0, [%i1] ret restore **********/ int main() { int AA = 5, BB = 9; printf( "AA: %d BB: %d\n", AA, BB ); swapA( &AA, &BB ); printf( "AA: %d BB: %d\n", AA, BB ); swapB( AA, BB ); printf( "AA: %d BB: %d\n", AA, BB ); } <2 arctic:~/Examples >g++ swap.cpp <3 arctic:~/Examples >a.out AA: 5 BB: 9 AA: 9 BB: 5 AA: 5 BB: 9 <4 arctic:~/Examples >g++ -S swap.cpp <5 arctic:~/Examples >cat swap.s .section ".text" .align 4 .global _Z5swapAPiS_ _Z5swapAPiS_: save %sp, -120, %sp st %i0, [%fp+68] st %i1, [%fp+72] ld [%fp+68], %g1 ld [%g1], %g1 st %g1, [%fp-20] ld [%fp+68], %i5 ld [%fp+72], %g1 ld [%g1], %g1 st %g1, [%i5] ld [%fp+72], %i5 ld [%fp-20], %g1 st %g1, [%i5] ret restore .align 4 .global _Z5swapBRiS_ _Z5swapBRiS_: save %sp, -120, %sp st %i0, [%fp+68] st %i1, [%fp+72] ld [%fp+68], %g1 ld [%g1], %g1 st %g1, [%fp-20] ld [%fp+68], %i5 ld [%fp+72], %g1 ld [%g1], %g1 st %g1, [%i5] ld [%fp+72], %i5 ld [%fp-20], %g1 st %g1, [%i5] ret restore <6 arctic:~/Examples >g++ -O -S swap.cpp <7 arctic:~/Examples >cat swap.s .section ".text" .align 4 .global _Z5swapAPiS_ _Z5swapAPiS_: ld [%o0], %o5 ld [%o1], %g1 st %g1, [%o0] retl st %o5, [%o1] .align 4 .global _Z5swapBRiS_ _Z5swapBRiS_: ld [%o0], %o5 ld [%o1], %g1 st %g1, [%o0] retl st %o5, [%o1]