0

These are a pair of routine I use quite often. I know MemSet is virtualy the same as ZeroMemory in kernel32.dll, but there may be a time I want to use these in an embedded system where this dll won't be avaliable.

;			         *** INITIALIZE PROCEDURE FRAME ***

; Many times routines need local variables that are usually saved on the stack.  At times it
; is convenient to have this space intialized to nulls.

;	ENTRY:	EBP 	= Top of procedure frame
;		ESP + 4 = Bottom of frame

;	LEAVE:	EAX	= 0
;		ECX 	= Size of procedure frame
;		EDX	= Base pointer to fill area.

  InitFrame	xor	eax, eax
		mov	ecx, ebp		; Top of area
		mov	edx, esp		; Bottom of area
		add	edx, byte 4		; Allow for callers return
		sub	ecx, edx		; Calculate size of frame

; Now we can just fall into MemSet to fill procedure frame.

; ============================================================================================
; 					*** MEMORY SET ***

;	ENTRY:	EAX = Fill pattern (32 bit).
;		ECX = Total number of bytes to initialize.
;		EDX = Base pointer to fill area.

;	LEAVE:	All registers preserved.

; NOTE: If EAX is not evenly divisible by 4 then all 4 bytes of EAX must be the same,
;	otherwise last 1, 2 or 3 bytes of buffer will only be fill with AL.

  MemSet	push	edi
		push	ecx			; Preserve registers
		push	edx

		mov	edi, edx		; Set destination index.
		mov	 dl,  cl		; Save LSB of counter
		shr	ecx, 2			; Number of dwords that can be written
		cld				; Assure EDI will auto increment with STOS
		rep	stosd
		mov	 cl,  dl
		and	 cl,  3			; Is buffer evenly divisible by 4
		jz	.Done			; ZR = 1, if so.

		rep	stosb			; Write odd numbered bytes

  .Done		pop	edx
		pop	ecx			; Restore registers
		pop	edi
		ret
2
Contributors
1
Reply
3
Views
12 Years
Discussion Span
Last Post by vegaseat
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.