Reputation: 354
I am trying to call printf on the _format string but I get an error instead of printing out both of the strings. I was able to get the program to work before adding the _printStr function so I am really unsure of why it won't print out both of them. I can also print out each string separately and it works fine (with 12(%ebp) and 16(%ebp)). This is the code I have:
.text
.globl _main
_string: .ascii "Hello\0"
_string2: .ascii " World\0"
_format: .ascii "%s %s\0"
_main: // push params, call fn, clear params, return
pushl $_string2
pushl $_string
call _printStr
addl $8, %esp
ret
//function to print a string passed to it on the stack
_printStr:
push %ebp # save old frame ptr
movl %esp, %ebp # set frame ptr
pushl 8(%ebp)
pushl 12(%ebp)
pushl _format
call _printf
addl $12, %esp # clear params from stack
leave
ret
Thanks for your time, I appreciate the help.
Upvotes: 1
Views: 3563
Reputation: 58762
You are missing a $
sign in the pushl _format
. It should read pushl $_format
because you want to pass its address. Incidentally, you are also passing the two words in reverse order, it will print " World Hello"
. Also you are missing a line feed, but have an extra space. Finally, putting your string constants in the .text
section isn't the best practice. See the fixed version in operation (note I removed some leading underscores that are not needed for ideone, but probably needed on your system)
Upvotes: 1
Reputation: 55392
Do not pass an unknown string as the first parameter to printf
. Always use a known format specifier, preferably a literal if possible. If you want to use printf
to print a string, do something like this:
_simpleformat: .ascii "%s\0"
...
pushl 8(%ebp)
pushl _simpleformat
call _printf
addl $8, %esp
Upvotes: 1