2014-04-12 87 views
0

我想將C轉換爲x86。我正在使用一個結構...x86結構scanf

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]); 

而且我86 ..

;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 
+0

學習使用GDB。這對任何彙編程序員來說都是非常有價值的。而且,你有什麼錯誤? – refi64

+0

注意:在'scanf()'格式'「...%s \ n」'的末尾,很少有代碼需要空格。建議'「...%s」'。或更好''%127s%127s%9s%d%d%127s「' – chux

回答

0

month_of_birth你有push ax,而不是push eax。這將只推動堆棧中地址的低16位,實際上保證了在scanf的崩潰。解決這個問題,它應該是確定的。

+0

我修正了,但它仍然跳閘我的錯誤檢查...; //如果有什麼不對的話,打印一個簡單的錯誤並返回一個錯誤代碼 ; if(result!= 6) ; { ; printf(「錯誤閱讀輸入」); ;返回-1; ; } CMP DWORD [EBP-12],6 JE main_for_expr_3 推format_string_main_3 呼叫的printf 添加ESP,4 MOV EAX,-1 離開 RET – j0nnyKash

+0

它可能是我的釋放calloc呼叫是; //分配所有記錄的緩衝區 ; records =(person_record *)calloc(number_of_records,sizeof(person_record)); 推STRUCT_SIZE MOV EAX,[EBP-8] 推EAX 呼叫釋放calloc 添加ESP,8 MOV [EBP-16],EAX – j0nnyKash

0

代碼中有很多奇怪的/錯誤的事情發生。展現更清晰的方式會更容易。你沒有提到你正在使用的彙編程序,有一些針對x86,每個都有自己的語法。下面是如何使用NASM做到這一點:

extern printf, scanf, calloc, exit, free, puts 
global main 

struc person_record 
    .last_name   resb 128 
    .first_name   resb 128 
    .year_of_birth  resb 10 
    .month_of_birth  resd 1 
    .day_of_birth  resd 1 
    .drivers_license_no resb 128 
    .size equ $ - person_record 
endstruc 

MAX_RECORDS  equ 2 

section .data 
Space    db 32, 0 
input_format db "%s%s%s%d%d%s", 0 
output_format db "%s %s %s %d %d %s", 0 

section .text 
main: 

    push person_record.size 
    push MAX_RECORDS  
    call calloc 
    add  esp, 4 * 2 
    mov  esi, eax 
    mov  ebx, eax 

    mov  edi, MAX_RECORDS - 1 
.FillRecord:  
    lea  eax, [ebx + person_record.drivers_license_no] 
    push eax 
    lea  ecx, [ebx + person_record.day_of_birth] 
    push ecx 
    lea  edx, [ebx + person_record.month_of_birth] 
    push edx 
    lea  eax, [ebx + person_record.year_of_birth] 
    push eax 
    lea  ecx, [ebx + person_record.first_name] 
    push ecx 
    lea  edx, [ebx + person_record.last_name] 
    push edx 
    push input_format 
    call scanf 
    add  esp, 4 * 7 

    push Space 
    call puts 
    add  esp, 4 * 1 

    add  ebx, person_record.size 
    dec  edi 
    jns  .FillRecord 

    mov  ebx, esi 
    mov  edi, MAX_RECORDS - 1 
.ShowRecord:  
    lea  eax, [ebx + person_record.drivers_license_no] 
    push eax 
    mov  ecx, [ebx + person_record.day_of_birth] 
    push ecx 
    mov  edx, [ebx + person_record.month_of_birth] 
    push edx 
    lea  eax, [ebx + person_record.year_of_birth] 
    push eax 
    lea  ecx, [ebx + person_record.first_name] 
    push ecx 
    lea  edx, [ebx + person_record.last_name] 
    push edx 
    push output_format 
    call printf 
    add  esp, 4 * 7 

    push Space 
    call puts 
    add  esp, 4 * 1 

    add  ebx, person_record.size 
    dec  edi 
    jns  .ShowRecord 

    push esi 
    call free 
    add  esp, 4 * 1 

    push 0 
    call exit 

和2個記錄的輸入和輸出:
enter image description here