UMBC CMSC 211

UMBC | CSEE


Reference Parameters

To use pointers in C to swap bytes, we can have a function:
void Swap( int *xp, int *yp )
{
    int	temp;

	temp = *yp;
	*xp = *yp;
	*yp = temp;
}
  
This allows us to actually change what xp and yp pointed to, not xp and yp themselves. This is call by reference. The equivalent assembly language is:
.MODEL SMALL
ASSUME SS:NOTHING
.CODE
xp EQU DWORD PTR [pb + 4] ; C parameters are reversed
yp EQU DWORD PTR [pb + 8] ; These are far pointers
PUBLIC Swap
Swap PROC
push pb
mov bp, sp
push ds
push si
push di ; save registers
lds si, xp ; ds:si --> x
les di, yp ; es:di --> y
mov ax, ds:[si]
xchg ax, es:[di]
pop di
pop si
pop ds
pop bp
ret ; the caller pops params in C
Swap ENDP
END
To make the swap in C, we would use the call Swap( &a, &b); This would be assembly code:
mov ax, SEG b
push ax
mov ax, OFFSET b
push ax
mov ax, SEG a
push ax
mov ax, OFFSET a
push ax
EXTRN Swap : NEAR
call Swap
add sp, 8


UMBC | CSEE