如何使用malloc在NASM程序集中创建新的char数组

时间:2022-12-20 02:54:26

Given this c code:

鉴于此c代码:

char** names=(char**)malloc(count*sizeof(char*));

I want to convert it to NASM assembly code. Here is what I tried, but the code crashes:

我想将其转换为NASM汇编代码。这是我尝试过的,但代码崩溃了:

  mov eax, dword count
  mov ebx, [eax*4] ;; i did times 4 because we clear stack by 4 bits or bytes?
  push ebx
  call _malloc
  mov names, eax
  add esp, 4

What does sizeof(char*)mean? which char pointer is the code addressing?

sizeof(char *)是什么意思?哪个char指针是代码寻址?

3 个解决方案

#1


3  

The reason it crashes is because mov ebx, [eax*4] is accessing memory at address eax * 4 which is unlikely to be valid, and definitely not what you want anyway. To multiply by 4, you can use lea ebx, [eax*4] or shl eax, 2 then push eax.

它崩溃的原因是因为mov ebx,[eax * 4]正在访问地址eax * 4的内存,这不太可能有效,绝对不是你想要的。要乘以4,您可以使用lea ebx,[eax * 4]或shl eax,2然后按eax。

PS: Learn to use a debugger.

PS:学习使用调试器。

#2


1  

sizeof (char *)

returns the size of a pointer:

返回指针的大小:

  • 16 bits [2 bytes] for a near (small model) pointer
  • 16位[2字节]用于近(小型号)指针

  • 32 bits [4 bytes] for a large or huge model in real mode, or a pointer in 32-bit virtual mode
  • 32位[4字节]用于实模式下的大型或大型模型,或32位虚拟模式下的指针

  • 64 bits [8 bytes] for a pointer in 64-bit mode.
  • 64位[8字节]用于64位模式下的指针。

#3


1  

It would be interesting to know more about how it crashes. On which instruction?

了解它如何崩溃将会很有趣。在哪个指令?

To answer your question sizeof(char *) means the size of any char * -- they are all the same size. 32-bit pointers are 4 bytes long, 64-bit pointers are 8 bytes long.

要回答你的问题,sizeof(char *)表示任何char *的大小 - 它们都是相同的大小。 32位指针长4个字节,64位指针长8个字节。

The code isn't dereferencing any pointer inside sizeof(). It's evaluated at compile-time and results in the size required to store a pointer of type char *.

代码不会取消引用sizeof()内的任何指针。它在编译时进行评估,并产生存储char *类型指针所需的大小。

#1


3  

The reason it crashes is because mov ebx, [eax*4] is accessing memory at address eax * 4 which is unlikely to be valid, and definitely not what you want anyway. To multiply by 4, you can use lea ebx, [eax*4] or shl eax, 2 then push eax.

它崩溃的原因是因为mov ebx,[eax * 4]正在访问地址eax * 4的内存,这不太可能有效,绝对不是你想要的。要乘以4,您可以使用lea ebx,[eax * 4]或shl eax,2然后按eax。

PS: Learn to use a debugger.

PS:学习使用调试器。

#2


1  

sizeof (char *)

returns the size of a pointer:

返回指针的大小:

  • 16 bits [2 bytes] for a near (small model) pointer
  • 16位[2字节]用于近(小型号)指针

  • 32 bits [4 bytes] for a large or huge model in real mode, or a pointer in 32-bit virtual mode
  • 32位[4字节]用于实模式下的大型或大型模型,或32位虚拟模式下的指针

  • 64 bits [8 bytes] for a pointer in 64-bit mode.
  • 64位[8字节]用于64位模式下的指针。

#3


1  

It would be interesting to know more about how it crashes. On which instruction?

了解它如何崩溃将会很有趣。在哪个指令?

To answer your question sizeof(char *) means the size of any char * -- they are all the same size. 32-bit pointers are 4 bytes long, 64-bit pointers are 8 bytes long.

要回答你的问题,sizeof(char *)表示任何char *的大小 - 它们都是相同的大小。 32位指针长4个字节,64位指针长8个字节。

The code isn't dereferencing any pointer inside sizeof(). It's evaluated at compile-time and results in the size required to store a pointer of type char *.

代码不会取消引用sizeof()内的任何指针。它在编译时进行评估,并产生存储char *类型指针所需的大小。