我有以下头文件:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <stdbool.h>
/**  **/

// size: 1B
typedef enum string_proc_func_type_t{
    REVERSIBLE      = 0,
    IRREVERSIBLE    = 1
} __attribute__((__packed__)) string_proc_func_type;


// size: 12B
typedef struct string_proc_key_t {
    uint32_t length; // 4B
    char* value; // 8B (ptr)
} __attribute__((__packed__)) string_proc_key;
typedef void (*string_proc_func) (string_proc_key*);

// size 24B
typedef struct string_proc_list_t {
    char* name; // 8B (ptr)
    struct string_proc_node_t* first; // 8B (ptr)
    struct string_proc_node_t* last; // 8B (ptr)
} __attribute__((__packed__)) string_proc_list;

// size 33B
typedef struct string_proc_node_t {
    struct string_proc_node_t* next; // 8B (ptr)
    struct string_proc_node_t* previous; // 8B (ptr)
    string_proc_func f; // 8B (ptr)
    string_proc_func g; // 8B (ptr)
    string_proc_func_type type; // 1B
} __attribute__((__packed__)) string_proc_node;

string_proc_list* string_proc_list_create(char* name);
void string_proc_list_destroy(string_proc_list* list);

// Aux functions
uint32_t str_len(char* a);
char* str_copy(char* a);


我已经实现了aux函数,如下所示:

uint32_t str_len(char* a) {
    uint32_t cont = 0;
    while(a != NULL && *a != '\0') {
        cont++;
        a++;
    }
    return cont;
}

char* str_copy(char* a) {
    uint32_t length = str_len(a);
    char* copy = malloc(length);
    for (uint32_t i = 0; i < length; ++i) {
        copy[i] = a[i];
    }
    return copy;
}


现在,我想在Assembly上实现string_proc_list_create函数,最终我在这里:

; C functions
    extern malloc
    extern free
    extern fopen
    extern fclose
    extern fprintf
    extern str_copy

; structs sizes
    %define STRUCT_STRING_PROC_LIST_SIZE 24

; structs offsets
    %define STRUCT_STRING_PROC_LIST_NAME_OFFSET 0
    %define STRUCT_STRING_PROC_LIST_FIRST_OFFSET 8
    %define STRUCT_STRING_PROC_LIST_LAST_OFFSET 16
section .data


section .text

global string_proc_list_create
string_proc_list_create:
    push rbp
    mov rbp, rsp

    push rdi ; save ptr to name

    ; allocate mem for struct
    mov rdi, STRUCT_STRING_PROC_LIST_SIZE
    sub rsp, 8 ; align stack
    call malloc
    add rsp, 8 ; unalign stack
    ; get ptr to struct at RAX

    pop rdi ; get ptr to name en RDI

    ; save ptr to struct at RBX
    push rbx
    mov rbx, rax

    ; copy name
    add rsp, 8 ; align stack
    call str_copy
    add rsp, 8 ; unalign stack
    ; get ptr to name (copy) at RAX

    ; initialize struct (ptr to struct at RBX, ptr to name (copy) at RAX)
    mov qword [rbx + STRUCT_STRING_PROC_LIST_NAME_OFFSET], rax
    mov qword [rbx + STRUCT_STRING_PROC_LIST_FIRST_OFFSET], 0
    mov qword [rbx + STRUCT_STRING_PROC_LIST_LAST_OFFSET], 0

    pop rbx
    pop rbp
    ret


然后,我尝试执行此行:

string_proc_list* list =  string_proc_list_create("aName");


但是我在RET遇到分段错误。我认为我可能打破了堆栈框架,但是我找不到我的代码出了什么问题。有什么帮助吗?

最佳答案

问题不在于您发布的代码中,那是完全正确的(如果您所说的是您的意思),但最有可能在使用str_copy的地方。问题是您得到的字符串不是以空终止的字符串。您

uint32_t length = str_len(a);
char* copy = malloc(length);


当您必须malloc(length+1);以允许最后一个\0字符时。
然后,将精确长度的chars从原始字符串复制到副本。然后,您应该在字符串中添加最终的\0 char,这样就很好了。

您的代码没有问题,但是它不会生成字符串,而是具有原始字符串有价值内容的char数组。为了使它成为正确的字符串,您需要获取一个空字符的空间,然后将其添加到字符串中,如下所示:

char* str_copy(char* a) {
    uint32_t length_plus_1 = str_len(a) + 1; // enough for null to be included
    char* copy = malloc(length_plus_1);
    for (uint32_t i = 0; i < length_plus_1; ++i) {
        copy[i] = a[i];
    }
    return copy;
}

10-07 19:16
查看更多