NASM Forum > Programming with NASM

print on windows

<< < (2/2)

fredericopissarra:
Now... if you want to allocate memory use VirtualAlloc() and VirtualFree() Win API functions. They are well documented in MSDN. Tip, as you can see the name of the 'stub' library used to call this functions from kernel32.dll are prefixed and suffixed (in 32 bits mode only) differently, accordingly to the mode used: x86-64 use __imp_ prefix only and i386 mode use prefix __imp__ and sufixed with @N, where N is the number of bytes of the arguments, pushed to the stack.

So, to allocate 128 bytes of memory in i386 mode you must do:

--- Code: ---; allocate 128 bytes
  push 0       ; NULL.
  push 128     ; sizeof of requested block.
  push 0x1000 ; MEM_COMMIT
  push 4   ; PAGE_READWRITE
  call [__imp__VirtualAlloc@16]
  ; EAX has the address of buffer allocated.

  mov [allocaddr],eax    ; save it.

  ; It is prudent to call VirtualLock here.
  push eax
  push 128 ; size
  call [__imp__VirtualLock@8]
  ...
; deallocate buffer
  push [allocaddr]
  push 128 ; size
  call [__imp__VirtualUnlock@8]

  push [allocaddr]
  push 0 ; size (must be 0 to release memory).
  push 0x8000 ; MEM_RELEASE
  call [__imp__VirtualFree@12]
--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version