Parameter Passing 

Parameter Passing

Not like high level language, Assembly language has no support to define a list of parameters of a subroutine. This deficiency fortunately gives full flexibility to the programmers to arrange the way to pass parameters.

There are three different ways of parameter passing:

Using general purpose registers.
Using global (static) variabels.
Using stack.

 Using general purpose registers

The programmer must place first the necessary parameters into a set of registers before invoking the subroutine using call instruction. To avoid confusion, it suggests to comment carefully each subroutine as to which parameters it expects to receive and in which registers they should be placed.

The following is an example of call-by-reference subroutine using register method.

Example: The subroutine to swap 2 byte-variables
 ;INPUT:	SI and DI = the pointers to the variables
 ;OUTPUT:
 SWAP	PROC
	xchg AL, [SI]
	xchg AL, [DI]
	xchg AL, [SI]
	ret
 SWAP	ENDP

The following is an example of call-by-value subroutine using register method.

Example: The subroutine to display a byte as a Hexadecimal number
 ;=================================
 ;INPUT:	DH
 ;OUTPUT:	
 ;This subroutine displays the hexadecimal digits
 ;of the content of DH.
 ;AX and BX are preserved.
 ;BX and DL are destroyed.
 HEX	PROC
	push AX
	push DS
	mov  AX, CS
	mov  DS, AX	; DS == CS
	mov  AL, DH	; make a copy
	shr  AL, 4
	mov  BX, OFFSET Hexdigit
	xlatb	; equiv. to mov AL, [BX+AL]
	mov  DL, AL
	mov  AH, 2
	int  21h
	mov  AL, DH
	and  AL, 0Fh
	xlatb
	mov  DL, AL
	mov  AH, 2
	int  21h
	mov  DL,'h'
	mov  AH, 2
	int  21h
	pop  DS
	pop  AX
	ret
 HexDigit DB '0123456789ABCDEF'
 HEX ENDP

 Pros and Cons of the register method

Advantages

Convenient and easier
Faster

Disadvantages

Only a few parameters can be passed using the register method.
Only a small number of registers are available.
Often these registers are not free. In fact, freeing them by pushing their values onto the stack negates the second advantage.

 Using global (static) variables

The programmer must place first the necessary parameters into a set of global (public static) variables before invoking the subroutine using call instruction.

 Pros and Cons of the global variables method

Advantages

Convenient and easier
Many parameters can be passed.

Disadvantages

Actually this method violates the essence of modular programming paradigm.
Hard and difficult to maintain the program because of the dependencies on global variables.
Many times the global variables creates unprecedented side effects especially in distributed processes.

Try to avoid to use this method as much as possible. Only use in cases as the last solution.