944,117 Members | Top Members by Rank

Ad:
0

Memory Initializaiton

by on Feb 9th, 2005
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.
Assembly Code Snippet (Toggle Plain Text)
  1. ; *** INITIALIZE PROCEDURE FRAME ***
  2.  
  3. ; Many times routines need local variables that are usually saved on the stack. At times it
  4. ; is convenient to have this space intialized to nulls.
  5.  
  6. ; ENTRY: EBP = Top of procedure frame
  7. ; ESP + 4 = Bottom of frame
  8.  
  9. ; LEAVE: EAX = 0
  10. ; ECX = Size of procedure frame
  11. ; EDX = Base pointer to fill area.
  12.  
  13. InitFrame xor eax, eax
  14. mov ecx, ebp ; Top of area
  15. mov edx, esp ; Bottom of area
  16. add edx, byte 4 ; Allow for callers return
  17. sub ecx, edx ; Calculate size of frame
  18.  
  19. ; Now we can just fall into MemSet to fill procedure frame.
  20.  
  21. ; ============================================================================================
  22. ; *** MEMORY SET ***
  23.  
  24. ; ENTRY: EAX = Fill pattern (32 bit).
  25. ; ECX = Total number of bytes to initialize.
  26. ; EDX = Base pointer to fill area.
  27.  
  28. ; LEAVE: All registers preserved.
  29.  
  30. ; NOTE: If EAX is not evenly divisible by 4 then all 4 bytes of EAX must be the same,
  31. ; otherwise last 1, 2 or 3 bytes of buffer will only be fill with AL.
  32.  
  33. MemSet push edi
  34. push ecx ; Preserve registers
  35. push edx
  36.  
  37. mov edi, edx ; Set destination index.
  38. mov dl, cl ; Save LSB of counter
  39. shr ecx, 2 ; Number of dwords that can be written
  40. cld ; Assure EDI will auto increment with STOS
  41. rep stosd
  42. mov cl, dl
  43. and cl, 3 ; Is buffer evenly divisible by 4
  44. jz .Done ; ZR = 1, if so.
  45.  
  46. rep stosb ; Write odd numbered bytes
  47.  
  48. .Done pop edx
  49. pop ecx ; Restore registers
  50. pop edi
  51. ret
  52.  
Comments on this Code Snippet
Feb 11th, 2005
0

Re: Memory Initializaiton

Tight_Coder_Ex
nice to see the Grandfather of it all appear!! Thanks!
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Message:
Previous Thread in Assembly Forum Timeline: Realtime application
Next Thread in Assembly Forum Timeline: Register class & Create Window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC