PROTO Directive


The PROTO directive creates a prototype for an existing procedure. A prototype declares a procedure's name and parameter list. It allows you to call a procedure before defining it and to verify that the number and types of arguments match the procedure definition. Create a procedure's prototype by copying the PROC statement and making the two changes:
MASM requires a prototype for each procedure called by INVOKE. PROTO must appear first before INVOKE. The examples show how to find the greatest common divisor (GCD) of two integers, which is the largest integer that will evenly divide both integers.  

 
    int  GCD( int x, int y ) {

 
       x = abs( x );

 
       y = abs( y );

 
       do {

 
          int  n = x % y;

 
          x = y;

 
          y = n;

 
       } while y > 0;

 
       return( x );

 
    }

 Greatest Common Divisor (GCD) 
 Without PROTO Directive   With PROTO Directive 

 
 INCLUDE Irvine32.inc

 
 .data

 
   x  SDWORD  12

 
   y  SDWORD  16


 
 .code

 
 ;; Find |value|

 
 abs PROC, ptrValue:PTR SDWORD

 
      mov   esi, ptrValue

 
      cmp   SDWORD PTR[esi], 0

 
      jge   POS

 
      neg   SDWORD PTR[esi]

 
 POS: ret

 
 abs ENDP


 
 main PROC

 
   INVOKE  abs, ADDR x

 
   INVOKE  abs, ADDR y

 
   .REPEAT

 
     mov   eax, x

 
     cdq       ; EDX:EAX = Dividend

 
     div   y   ; Y       = Divisor

 
     mov   ebx, y

 
     mov   x, ebx

 
     ;; EDX = Remainder
     

.UNTIL (edx == 0) mov eax, x call WriteInt exit main ENDP END main

 
 INCLUDE Irvine32.inc

 
 abs  PROTO,  ptrValue:PTR SDWORD

 
 .data

 
   x  SDWORD  -15

 
   y  SDWORD   12


 
 .code

 
 main PROC

 
   INVOKE  abs, ADDR x

 
   INVOKE  abs, ADDR y

 
   .REPEAT

 
     mov   eax, x

 
     cdq       ; EDX:EAX = Dividend

 
     div   y   ; Y       = Divisor

 
     mov   ebx, y

 
     mov   x, ebx

 
     ;; EDX = Remainder
     

.UNTIL (edx == 0) mov eax, x call WriteInt exit main ENDP ;; Find |value| abs PROC, ptrValue:PTR SDWORD mov esi, ptrValue cmp SDWORD PTR[esi], 0 jge POS neg SDWORD PTR[esi] POS: ret abs ENDP END main
 Output  Output

   

   


      “I’m not arguing, I’m just explaining why I’m right.”    
      ― Unknown