0
我试图写分配的存储器,存储在给定的指针的地址的组件的功能。但是,我不知道如何将地址存储在传递给函数的参数中。FASM通过引用传递/指针
我有以下几点:
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
任何想法?
,我也不太清楚,我明白了这一切,因为我是相当新在汇编依然。我做了:http://pastebin.com/fYUXBkeW,它似乎工作。我不得不保存'edx'和其他寄存器,否则会崩溃。你对此是正确的。我理解你的意思,但不是全部。特别是'更简单的方法'。我接受这个答案是因为我明白了它的大部分,并且保存寄存器部分保存了我:D – Brandon
让我看看我能否更好地解释它:在'ReAllocate_Memory'中为新缓冲区调用'malloc'。 'malloc'的结果在'eax'中返回。如果你把它留在那里并且不做任何事情就返回给调用者,当'ReAllocate_Memory'返回时,这个值仍然会在调用范围中,并且你可以像原来一样将它存储起来(虽然注释掉了) '再打电话。 –
OHHH我明白了!我没有想到这一点。我的思想坚持让它使用参数来思考返回值。 Ahaha。这是一个更好的解释。谢谢! :d – Brandon