Question

I would like to call the printf function from C with two integers. My format string is:

LC0:
    db "odd bits: %d, even bits: %d", 10, 0

I have the integer ob and eb:

ob: DD 0
eb: DD 0

and then I do at the end:

push dword [ob]
push dword [eb]
push LC0
call printf
add esp,8

However, this gives me the result Odd bits: [ob], Even bits: [ob, repeated] then gives me a segmentation fault. Am I calling the printf function wrong?

EDIT: I added LC1 as db "even bits: %d", 10 0, then redid:

push dword [ob]
push LC0
call printf
push dword [eb]
push LC1
call printf
add esp, 8

This gives me a REVERSED result, giving the eb to the LC0 string, and ob to the LC1 string, and, it gives a segmentation fault at the end. Any clue?

Était-ce utile?

La solution

You're not adjusting the stack pointer correctly.

In your original code you were pushing 12 bytes, but only "popping" 8.

In your updated code you're pushing 8 bytes twice, i.e. 16 bytes in total, but only "popping" 8 bytes once.


As for the order in which the values are printed; in your original code you had:

push dword [ob]  
push dword [eb]
push LC0

You've declared LC0 as db "odd bits: %d, even bits: %d", 10, 0, so clearly you intended ob to the printed first. Arguments are push right-to-left, so you should push eb before ob.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top