; Program that demonstrates C-style procedure manipulating
; C-like LOCAL variables on the stack.
.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
.CODE ; start of main program code
_start:
mov eax, 10 ; Load data into EAX
push eax ; Push first argument
call subtract6 ; push address of next instruction
; onto the stack, and pass control
; to the address of subtract6
add sp, 4 ; Destroy the pushed arguments
; (equivalent to one pop)
INVOKE ExitProcess, 0 ; exit with return code 0
PUBLIC _start ; make entry point public
subtract6 PROC NEAR32 C arg:DWORD
LOCAL counter:DWORD
mov counter, 6 ; Initialize local variable with 6
mov eax, counter ; put value in EAX
sub arg, eax ; Subtract value from the 1st argument
mov eax, arg ; put result in EAX
ret ; Return result in EAX
subtract6 ENDP
END ; end of source code
; Compiled as:
;
; 0040104C B8 0A000000 MOV EAX,0A
; 00401051 50 PUSH EAX
; 00401052 E8 0B000000 CALL main._subtract6
; 00401057 66:83C4 04 ADD SP,4
; 0040105B 6A 00 PUSH 0
; 0040105D E8 98030000 CALL main._ExitProcess@4
; 00401062 55 PUSH EBP
; 00401063 8BEC MOV EBP,ESP
; 00401065 83C4 FC ADD ESP,-4
; 00401068 C745 FC 06000000 MOV DWORD PTR SS:[EBP-4],6
; 0040106F 8B45 FC MOV EAX,DWORD PTR SS:[EBP-4]
; 00401072 2945 08 SUB DWORD PTR SS:[EBP+8],EAX
; 00401075 8B45 08 MOV EAX,DWORD PTR SS:[EBP+8]
; 00401078 C9 LEAVE
; 00401079 C3 RETN
;