0

I am trying to convert C to x86. I am using a struct...

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;

I am having trouble getting my scanf to work. Here is the 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]);

And my 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

I'm using a check to see if result is 6 and if it's not my program that prints an error and exits. It keeps having an error and I'm not sure what I am doing wrong. Any help would be much appreciated. Thank you.

This is my calloc call which appears to be correct...

;  // 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
2
  • Learn to use GDB. It's valuable to any assembly programmer for situations like this. And, what error are you getting? Apr 12, 2014 at 17:45
  • Note: It is very unlikely code needs a white-space at the end of a scanf() format "...%s\n". Suggest "...%s". or better yet "%127s%127s%9s%d%d%127s" Apr 12, 2014 at 20:08

2 Answers 2

0

Under month_of_birth you have push ax instead of push eax. This would push only the lower 16 bits of the address on the stack, virtually guaranteeing a crash in scanf. Fix that and it should be OK.

2
  • I fixed that however it is still tripping my error check...; // if anything doesn't go right, print a simple error and return an error code ; if (result != 6) ; { ; printf("error reading input"); ; return -1; ; } cmp dword [ebp-12], 6 je main_for_expr_3 push format_string_main_3 call printf add esp, 4 mov eax, -1 leave ret
    – j0nnyKash
    Apr 12, 2014 at 18:11
  • could it be my calloc call which is; // 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
    – j0nnyKash
    Apr 12, 2014 at 18:13
0

There are many weird/wrong things going on in your code. It will be easier to show a cleaner way. You have not mentioned the Assembler you are using, there are a few for x86 and each has its own syntax. Here is how you can do it using 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

And the input and output of 2 records:
enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.