1

I'm a having a problem with fprintf which I just can't grasp. Maybe you can find a solution to this problem.

You see, When I call fprintf it starts looping infinitely, for no good reason. I have no idea what's going here, so perhaps you can give me a hand.

String_int:
    db 91; "[" 
    db 37; "%"
    db 115;"i"
    db 93; "]" 
    db 0;  "end_string" 

w_IN_ASCII:
    db 119; "w" 
    db 0;  "end_string" 

<SOME CODE>

    mov rdi, FILE_LOCATION;
    mov rsi, w_IN_ASCII;
    call fopen;
    mov r15, rax;

    cmp r15, 0; (with this I can be sure it is not a NULL)
    je .endProgram;

    mov rdi, r15;
    mov rsi, String_int;
    mov rdx, TO_PRINT_LOCATION;
    call fprintf; this is where my code dies

    mov rdi, r15;
    call fclose;

.endProgram:
    ret
NacOverflow
  • 97
  • 1
  • 2
  • 8
  • It's misleading to say AMD64 is x8086. – obataku Sep 11 '12 at 20:56
  • Are your strings null-terminated? – Kerrek SB Sep 11 '12 at 20:59
  • 1
    What about the variadic arguments to `fprintf`? – obataku Sep 11 '12 at 21:01
  • How is this code being called? Is it `main`, or are you trying to make your own `_start` function to replace the one in `crt1.o`? If you're doing the latter, you must not use any functions from the C standard library, because necessary initialization steps to make them usable may have been skipped. – R.. GitHub STOP HELPING ICE Sep 12 '12 at 00:50
  • I'm not sure since I didn't build the makefile I'm using to compile this, but I've been using other C standard Library functions without problem, so I guess is the former. – NacOverflow Sep 12 '12 at 04:48
  • Can you show us the rest of the file? The fact that it's worked so far does not mean everything's okay. It could just mean the breakage is unpredictable. You definitely do not need a va_list to do this. – R.. GitHub STOP HELPING ICE Sep 12 '12 at 05:04
  • By the way, have you tried using the debugger? How do you know where your program fails? If you're overwriting `r15` and returning without restoring it, it's very likely that the infinite loop is in the code that called your function, due to its registers being corrupted by your code... – R.. GitHub STOP HELPING ICE Sep 12 '12 at 05:06
  • I have a few debuggers but I'm not used to their interface, so I'd rather avoid them as much as possible. I have made a few trial and error, seeing if adding or removing anything avoided the loop. I discovered that just by not calling the fprintf function, the whole programs works fine. – NacOverflow Sep 12 '12 at 05:16
  • (BTW I pushed all registries at the begging of the the function, and poped them at the end, so the problem is clearly not there) Thanks for the info, I'll see if I'm able to work something out. – NacOverflow Sep 12 '12 at 05:17

1 Answers1

0

In System V AMD64 ABI, variadic functions expect the actual number of variadic arguments in al. I don't see you setting it.

Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109