sprintf - Write Formatted String to string
sprintf formats and prints a series of characters and values to a string - a buffer that located in the memory.
'Format-string' determines what is to be printed and how it is to be printed out.
'Format-string' consists of ordinary characters, escape sequences, and format specifications.
The 'Format-string' is read left to right. When the first format specification is encountered, the value of the first argument after the 'Format-string' is converted and output according to format specifications. The second format specification causes the second argument to be converted and output, and so on.
This function identical to printf except it sends the output to string and not to stdout.
Escape sequences:
Escape sequences are special character combinations that can represent whitespace and non-graphic characters. They are used to specify actions such as carriage returns and tab movements. Escape sequences consist of a backslash ('\') followed by a character or combination of characters:
\n new-line
\t tab
\b backspace
\r carriage return
\\ backslash
"" Reverse-commas
If there are arguments following 'Format-string', then 'Format-string' must contain format specifications that determine the output format for these arguments.
Format specifications always begin with a percent sign (%) and have the following form:
% [width] type
Each field of the format specification is a single character or number signifying a particular format option.
The following describes each field.
Type:
The 'Type' character determines whether the associated argument is interpreted as a character, string, or number. The simplest format specification contains only a percent sign and a 'Type' character. (For example: %s prints a string.)
The 'Type' characters are:
d Decimal Integer
o Octal Integer
x Hex Integer
X Hex Integer (Capital Letters output)
c Character
s String
- Characters printed up to the first null character ('\0')
ld Long Integer
lx Long Hex
Width:
The optional width specifier is a non-negative decimal integer specifying the minimum number of characters to print, padding with blanks and zeros.
Width never causes a value to be truncated.
Returns:
R0 = '0' if successful , '-1' on error.
Notes:
Arguments are: pointers to 'String' variable Value of 'Char', numbers etc. (push address for %s, push values for %c %x %d %o ...)
Arguments are pushed in reversed order. (Last argument is printed first)
Last Argument pushed into Stack is a pointer to the "Format-string".
If there are more arguments than there are format specifications, the extra arguments are ignored.
The results are undefined if there are not enough arguments for all the format specifications.
Ordinary characters are simply copied in the order of their appearance.
If the percent sign is followed by a character that has no meaning as a format field, the character is copied to the buffer.
Example:
.text
main: .word 0
pushl $10
pushl $7
pushal format
pushal buffer
calls $4, .sprintf
pushal buffer
calls $1, .puts
halt
format: .asciz "%d -- %d\n"
buffer: .space 10