更新:修复了我的 C 代码示例。我在汇编中从一个 char* 复制到另一个 char* 。我需要按值复制,然后将新 char* 的地址返回给调用者。
我正在尝试在程序集中创建与以下 C 代码等效的代码,但 gdb/valgrind 报告说我正在尝试释放已经“释放”的东西。
C中的代码:
char* func( int x, char* name){
namestr = (char *) malloc( strlen(name) + 1 );
nameestr = strdup( namestr, strlen(name) +1 );
free( name ); //Just showing what I plan to do later.
return namestr;
}
int main( ){
char* name = (char *) malloc( 10 );
*name = "0123456789"
char* some_string = func( 10, name );
free( some_string );
}
汇编代码:
new_student:
pushl %ebp
movl %esp, %ebp
pushl %edi
pushl %esi
subl $20, %esp
movl 12(%ebp), %ecx
movl %ecx, %edi
movl %ecx, -12(%ebp)
;get length and allocate the appropriate space
.STR_ALLOCATE:
movl $0, %eax
movl $-1, %ecx
repnz scasb
notl %ecx
subl $1, %eax
addl $1, %eax
movl %ecx, -8(%ebp)
pushl %ecx
call allocate
add $4, %esp
movl %eax, -16(%ebp)
;copy the string byte by byte
.STR_CPY:
movl -8(%ebp), %ecx
movl %eax, %edi
movl 12(%ebp), %esi
rep movsb
movl -12(%ebp), %eax
.END:
popl %esi
popl %edi
leave
ret
谁能给我一些关于我做错了什么的提示?