Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  Let's look more closely at the difference between the pass-by-value and
  pass-by-reference methods of parameter passing. The pass-by-value method
  works by passing a copy of a parameter's current value to the subroutine.
  In contrast, the pass-by-reference method passes a parameter's address.
  This affects the subroutine interface in two different ways.

  First, the value of a parameter passed by reference cannot be accessed
  directly. Instead, you must first copy the parameter's address from the
  stack and then obtain the parameter's value through the address. For
  example:

  _TEXT           SEGMENT byte public 'CODE'
                  ASSUME  cs:_TEXT

                  PUBLIC  _SmallAbs
  _SmallAbs       PROC    near            ; call with near CALL

                  push    bp
                  mov     bp,sp

                  mov     bx,[bp+4]       ; BX = address of 1st parameter
                  mov     ax,[bx]         ; AX = value of 1st parameter

                  cwd
                  xor     ax,dx
                  sub     ax,dx

                  mov     [bx],ax         ; leave result at parameter address

                  pop     bp
                  ret                     ; near RETurn

  _SmallAbs       ENDP

  _TEXT           ENDS

  SmallAbs(), which uses pass-by-reference, obtains the value of its
  parameter in two steps. First, it copies the parameter's address from the
  stack (MOV BX,[BP + 4]). Then it obtains the parameter's value from that
  address (MOV AX,[BX]). Once the parameter's value is in AX, the
  computation of its absolute value proceeds as before.

  To pass a parameter from a C program to SmallAbs(), you need to pass its
  address instead of its value:

  SmallAbs( &x );         /* pass the address of x */

  The corresponding executable code would look something like this:

  mov ax,offset X         ; push the address of X
  push ax
  call _SmallAbs          ; call the subroutine (near call)
  add  sp,2               ; discard the address from the stack

  The way SmallAbs() returns its result points out the key reason to use the
  pass-by-reference method: SmallAbs() actually changes the value of its
  parameter. Instead of simply returning a result in AX, SmallAbs() stores
  its return value at the parameter's address (MOV [BX],AX).

  In high-level programming languages, both the pass-by-reference and
  pass-by-value methods can be used. In some languages, the method of
  passing parameters defaults to one method or the other. For example, BASIC
  uses pass-by-reference by default, but C uses the pass-by-value method as
  the default. In many languages, the default method can vary, depending on
  a parameter's data type. You can usually determine which method is used to
  call a subroutine by specifying a method in your source code (if your
  compiler supports such specifications) or by using a data type associated
  with a particular parameter-passing method.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson