我试图编写一个程序集函数,它分配内存并将地址存储在给定的指针中。但是,我不知道如何在传递给函数的参数中存储地址。
我有以下几点:
struc SSLSocket sock, ssl, ctx, address, port, connected, type
{
.sock dd sock
.ssl dd ssl
.ctx dd ctx
.address dd address
.port dw port
.connected db connected
.type dd type
}
SockArray dd 0 //will allocate 5 of the above struct on the heap and store it in this pointer.
section '.code' code readable executable
main:
push ebp
mov ebp,esp
;push 5
;call [malloc]
;add esp, 0x04
;mov [SockArray], eax
push SockArray ;pointer that will hold allocated memory
push 23 ;size of struct
call ReAllocate_Memory
add esp, 0x08
push [SockArray] //print address of allocated memory.
push PrintPtr
call [printf]
add esp, 0x08
mov esp, ebp
pop ebx
call [getchar]
mov eax, 0x00
ret
ReAllocate_Memory:
push ebp
mov ebp, esp
mov eax, [ebp + 0x0C] ;Pointer that will hold address of allocation
mov edx, [ebp + 0x08] ;Size to allocate in bytes
push eax
call [free] ;Free any allocated memory
add esp, 0x04
push edx
call [malloc] ;Allocate n-size bytes
add esp, 0x04
;mov address into parameter pointer ([ebp + 0x0C]).
mov esp, ebp
pop ebp
ret有什么想法吗?
发布于 2014-04-20 18:52:00
您不能将新指针存储在ReAllocate_Memory中,因为在该例程中没有它的地址。
任一
lea eax, SockArray; push eax或类似的地址),然后加载该参数并将其存储到其中,例如mov edx, [ebp + 0x10],然后用mov [edx], eax存储。否则,这就容易多了:
ReAllocate_Memory中。因为它是在eax中返回的,所以您可以简单地将它存储在调用范围中,就像在malloc调用之后一样。旁白:用一个值加载edx,然后调用一个函数(free)是危险的:不需要子程序来保存edx的值。最好在free返回之前不要加载它,即使它目前正在工作。
https://stackoverflow.com/questions/23186037
复制相似问题