How can one implement printf-like functionality in assembler?-Collection of common programming errors
I’m very much just looking for feedback and guidance rather than a direct answer.
Using the .586
assembler instructions, how can you simulate printf-like functionality? For fixed width fields, I know I could do something like this:
.data
Format BYTE 'You are ## years old.',0
And then before printing the string, simply replace the ## values to the appropriate number, changing them back afterwards in case I need to use the format string multiple times with different values.
This might not be the best way, but for now it works.
What I can’t figure out is how to do it if you don’t know the number of digits ahead of time. The format option is okay if you know the maximum number of digits you want to use (above example won’t work pretty-like for people 100 years old and older, you lose the spaces between words).
If you reserve too much space though, you get double spaces between your number and your worlds.
.data
Format BYTE 'You are ### years old.',0
Using my scheme with someone who is 12 would produce:
You are 12 years old.
My only thought was is there happened to be an ASCII character which printed nothing (aside from \0
), but it seems tacky.
The whole problem arises because the instructor wants us to be able to print this type of formatted string using a single call to a PrintString procedure (which he provided us) which looks for esi
to be set to a \0
terminated byte array.