News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Help with converting C++ to ASM

Started by Kyle, December 01, 2009, 09:48:57 AM

Previous topic - Next topic

Kyle

Yeah I'm definitely a noob but thats ok, I'm learning  :bg.

Alright, so I'm converting a C++ app I've wrote to assembly, but theres one part that has me a bit stumped. Its using "new" and "delete". Here's the code I'm wondering about:

DWORD FileSize;
char* FileData = { 0 };

FileData = new char [ FileSize ];

delete [] FileData;


I figure FileData would be BYTE and FileSize would remain a DWORD, but how would I handle the code I've shown in assembly?

For the new data would I do something like:

push eax
mov DWORD PTR FileSize [ ebp ], eax
call new
pop ecx


And is new and delete even necessary in assembly?

jj2007

include \masm32\include\masm32rt.inc

.data? ; non-initialised variables
FileSize dd ?
FileData dd ?

.code

start:
mov FileSize, 1234h  ; for testing
mov FileData, alloc(FileSize)
MsgBox 0, "FileData is ready for use", "Hi", MB_OK
free FileData
exit ; short form of invoke ExitProcess, 0

end start


alloc and free are defined in \masm32\macros\macros.asm:

     alloc MACRO bytecount
        invoke GlobalAlloc,GMEM_FIXED or GMEM_ZEROINIT,bytecount
        EXITM <eax>
      ENDM

     free MACRO hmemory
        invoke GlobalFree,hmemory
      ENDM

Kyle

Ah wow fast response. Thank you very much!

drizz

There is no reason you can't have new and delete in assembly. Under the hood they are just functions.
.code

??2@YAPAXI@Z proto syscall; operator new - mangled name
??3@YAXPAX@Z proto syscall; operator delete - mangled name

$new macro bytes:req
push bytes
call ??2@YAPAXI@Z
pop ecx
exitm <eax>
endm

$delete macro pointer:req
push pointer
call ??3@YAXPAX@Z
pop ecx
exitm <>
endm

main proc C uses esi edi ebx argc:dword,argv:ptr dword
int 3
mov ebx,$new(100h)
$delete(ebx)
return 0
main endp

end


just link with msvcrt.dll

The truth cannot be learned ... it can only be recognized.

drizz

or something more elegant, like:

.code

??2@YAPAXI@Z proto syscall :DWORD; operator new
??3@YAXPAX@Z proto syscall :PTR BYTE; operator delete
new equ ??2@YAPAXI@Z
delete equ ??3@YAXPAX@Z

$new macro elementsize:req,numelements:req
mov eax,elementsize
mov edx,numelements
mul edx
invoke new,eax
exitm <eax>
endm

$delete macro pointer:req
invoke delete,pointer
exitm <>
endm

main proc C uses esi edi ebx argc:dword,argv:ptr dword
local FileSize:DWORD, FileData:PBYTE
;// FileData = new char [ FileSize ];
mov FileData,$new(BYTE,FileSize)
;// delete [] FileData;
$delete(FileData)
ret;urn 0
main endp

end


macro power!  :boohoo:
The truth cannot be learned ... it can only be recognized.

Kyle

Haha thank you. I've never used macros before. Time to look into them  :eek.