; Program that demonstrates C-style procedure manipulating
; C-like LOCAL array 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    myproc          ; push address of next instruction
                                ; onto the stack, and pass control
                                ; to the address of myproc
        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

myproc PROC NEAR32 C USES edi, arg:DWORD
        ARRAY_SIZE  EQU 20
        LOCAL   myarray[ ARRAY_SIZE ] : BYTE

        ; For 32-bit protected mode flat model, all segments are congruent
        ; and thus ES does not need to be specified explicitly, so the segment
        ; modification is done here only for the purpose of demonstration:
        push    ss
        pop     es              ; Set ES=SS
        
        lea     edi, myarray    ; ES:EDI now points to beginning of myarray
        mov     ecx, ARRAY_SIZE ; Load count
        sub     al, al          ; set AL equal to zero
        rep     stosb           ; Store zeros from AL in [ES:DI]
        mov     eax, ARRAY_SIZE ; put size result in EAX

        ; use array:
        mov     ebx, 9
        mov     myarray[ebx], 5 ; set 10th array element equal 5

        ret                     ; Return result in EAX

myproc ENDP

END                             ; end of source code

; Compiled:
;
; 0040104C B8 0A000000       MOV EAX,0A
; 00401051 50                PUSH EAX
; 00401052 E8 0B000000       CALL main._myproc
; 00401057 66:83C4 04        ADD SP,4
; 0040105B 6A 00             PUSH 0                                   ; ExitCode = 0
; 0040105D E8 AC030000       CALL main._ExitProcess@4                 ; ExitProcess
; 00401062 55                PUSH EBP
; 00401063 8BEC              MOV EBP,ESP
; 00401065 83C4 EC           ADD ESP,-14
; 00401068 57                PUSH EDI
; 00401069 16                PUSH SS
; 0040106A 07                POP ES                                   ;  Modification of segment register
; 0040106B 8D7D EC           LEA EDI,DWORD PTR SS:[EBP-14]
; 0040106E B9 14000000       MOV ECX,14
; 00401073 2AC0              SUB AL,AL
; 00401075 F3:AA             REP STOS BYTE PTR ES:[EDI]
; 00401077 B8 14000000       MOV EAX,14
; 0040107C BB 09000000       MOV EBX,9
; 00401081 36:C6442B EC 05   MOV BYTE PTR SS:[EBX+EBP-14],5
; 00401087 5F                POP EDI
; 00401088 C9                LEAVE
; 00401089 C3                RETN
;