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):
- If the .MODEL statement or the PROC statement has specified a language,
the PROC name is automatically declared PUBLIC.
- If the C language is specified, all symbols declared PUBLIC or EXTRN (including
PROC names) within range have a leading '_' added to them, which is the
standard C convention.
- Code to save and set up the bp register is added immediately after any PROC
statement, and code to restore the bp register is added immediately before any ret
statement.
- The PROC statement can specify USES R1 R2 ... Rn where each Ri is one of the
80X86 machine registers. The USES clause causes the specified registers to be pushed
on the stack immediately after the PROC statemnt and to be popped from the stack
in reverse order just before each RET in the PROC.
Note that there are no commas between the register names or before USES.
- The PROC statement can specify the number of parameters that are used to call it
after the optional USES clause. the assembler can then equate each parameter name
to bp + n where n is the appropriate number. Account is taken
as to whether or not the the model is large or small. For the Pascal language
type, parameters are assumed to be pushed in the order encountered, and for C,
they are assumed to be bushed in the reverse order.
- When parameters are specified in the PROC statement and the language is
Pascal or STDCALL, the ret instruction is automatically changed to
ret m, where m is the number of bytes of parameters specified in
the PROC statement.
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