gpt4 book ai didi

c - x86 结构体 scanf

转载 作者:行者123 更新时间:2023-11-30 17:35:15 25 4
gpt4 key购买 nike

我正在尝试将 C 转换为 x86。我正在使用一个结构...

struct person_record_struct
{
char last_name[128];
char first_name[128];
char year_of_birth[10];
int month_of_birth; // January => 1
int day_of_birth; // 1st Day of a Month => 1
char drivers_license_no[128];
};
typedef struct person_record_struct person_record;

我无法让我的 scanf 工作。这是 C..

result = scanf("%s\n%s\n%s\n%d\n%d\n%s\n", &records[counter].last_name[0],     

&records[counter].first_name[0], &records[counter].year_of_birth[0],

&records[counter].month_of_birth, &records[counter].day_of_birth,

&records[counter].drivers_license_no[0]);

还有我的 x86..

;counter @ [ebp-4]
;records @ [ebp-16]
; format_string_main_2 db '%s\n%s\n%s\n%d\n%d\n%s\n', 0
; read in info

; push drivers_license_no
mov ebx, [ebp-16] ;
mov eax, [ebp-4]
mov ecx, struct_size
mul ecx
add eax, ebx
lea eax, [eax+276]
push eax

; push day_of_birth
mov ebx, [ebp-16]
mov eax, [ebp-4]
mov ecx, struct_size
mul ecx
add eax, ebx
lea eax, [eax+272]
push eax

; push month_of_birth
mov ebx, [ebp-16]
mov eax, [ebp-4]
mov ecx, struct_size
mul ecx
add eax, ebx
lea eax, [eax+268]
push ax

; push year_of_birth
mov ebx, [ebp-16]
mov eax, [ebp-4]
mov ecx, struct_size
mul ecx
add eax, ebx
lea eax, [eax+256]
push eax

; push first_name
mov ebx, [ebp-16]
mov eax, [ebp-4]
mov ecx, struct_size
mul ecx
add eax, ebx
lea eax, [eax+128]
push eax

; push last_name
mov ebx, [ebp-16]
mov eax, [ebp-4]
mov ecx, struct_size
mul ecx
add eax, ebx
lea eax, [eax+0]
push eax

push format_string_main_2
call scanf
add esp, 28
mov [ebp-12], eax

我正在使用检查来查看结果是否为 6,以及是否不是我的程序打印错误并退出。它一直出错,我不确定我做错了什么。任何帮助将非常感激。谢谢。

这是我的 calloc 调用,它似乎是正确的...

;  // allocate the buffer of all the records
; records = (person_record *)calloc(number_of_records, sizeof(person_record));

push struct_size
mov eax, [ebp-8]
push eax
call calloc
add esp, 8
mov [ebp-16], eax

最佳答案

month_of_birth 下,您有 push ax 而不是 push eax。这只会将地址的低 16 位压入堆栈,实际上保证了 scanf 崩溃。解决这个问题就应该没问题了。

关于c - x86 结构体 scanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23033872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com