I am trying to print a string in assembly by calling printf.
My assembly code:
mov dword[ebx + 0], '"'
mov dword[ebx + 4], 'h'
mov dword[ebx + 8], 'e'
mov dword[ebx + 12], 'l'
mov dword[ebx + 16], 'l'
mov dword[ebx + 20], 'o'
mov dword[ebx + 24], '"'
mov dword[ebx + 28], 0
push ebx
push formatString
call printf
add esp, 8
...
formatString db '%s', 10, 0
However when I run this it only prints the first character – ‘”‘, not the whole word (“hello”).
Many thanks
My assembly is rusty, but those should probably be byte-wise moves. You need a byte array in memory since that is what
printfis expecting for a%s. The string in memory is probably"\0\0\0h\0\0\0e\0\0\0l\0\0\0l\0\0\0o\0\0\0"\0\0\0\0\0\0\0.So, if
ebxcontains the address 0x123456, then you would have something like the following in memory:Even though you are passing 0x123456 into
printfas the address, it is only seeing a single character before it hits the first NUL byte. The following should work:There is probably a much better way to load bytes into an indirect address based on
ebxbut I haven’t looked at assembly in more years than I can count.