UMBC CMSC 211

UMBC | CSEE


Assembler Help with Procedures

The Assembler can help with communicating with high-level lanaguages. Note the change in the .model statement: .MODEL SIZE, C ; or Pascal or STDCALL Note that there is no C++ version, use C instead. The default for .MODEL SMALL is NEAR PROCs.

This can also be applied to the procedure definition:

name PROC C; ; or PASCAL OR STDCALL

When you specify the language, it implies certain things for some instructions (PUBLIC, EXTRN, and PROC psuedo-instructions, and the RET instruction):

The "lazy" Way

Notice where the commas are required or not!
Stuff      PROC      C  USES SI DI, A : WORD, B : DWORD, C : WORD
           ...
           RET
Stuff	   ENDP
  

The "hard" Way (or the equivalent way!)

           PUBLIC   _Stuff
_Stuff	   PROC
           push     bp
           mov      bp, sp
           push     si
           push     di
A          EQU      WORD PTR [bp + 4]
B          EQU      DWORD PTR [bp + 6]
C          EQU      WORD PTR [bp + 10 ]
           ...
           pop      di
           pop      si
           mov	    sp, bp
           pop      bp
           ret
_Stuff     ENDP
  

Oh, them local variables

One of the important things necessary for re-entrant or recursive procedures, is that we must have local variables. These are stored on the stack and accessed in a similar way as the parameters, except this are on the other side of the return address.

The "lazy" Way

aProc      PROC     USES SI DI
           LOCAL    L1 : WORD, L2 : DWORD
  

The "hard" Way (or the equivalent way!)

aProc      PROC
           push     bp
           mov      bp, sp
           sub	    sp, 6
L1	   EQU      WORD PTR [bp - 2]
L2         EQU      DWORD PTR [bp - 6]
           push     si
           push     di
  
Now the ret instruction becomes:
           pop      di
           pop      si
           mov      sp, bp
           pop      bp
           ret      whatever
  


UMBC | CSEE