Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how printf works internally.? [duplicate]

Tags:

c

assembly

Possible Duplicate:
C/C++ function definitions without assembly

hi again, I want to know how printf works internally...by internally I mean what underlying system calls/ISRs etc mechanism is used...and not about the variable argument list...

Reason:I am using FASM but there is little there for console i/o, I know I can use printf available from the c library(I don't know how but that's a different point)

thanks.

like image 485
rsjethani Avatar asked Nov 09 '10 20:11

rsjethani


People also ask

How does printf work in assembly?

Printf in Assembly To call printf from assembly language, you just pass the format string in rdi as usual for the first argument, pass any format specifier arguments in the next argument register rsi, then rdx, etc.

How do printf work?

The printf function (the name comes from “print formatted”) prints a string on the screen using a “format string” that includes the instructions to mix several strings and produce the final string to be printed on the screen.

How does printf work on the stack?

Arguments are passed on the stack when a C function is called. The printf function contains a format specifier which is the first argument to the function, the pointer to the rest of the arguments is set pointing to after that argument by advancing it sizeof(char*) bytes.

Is printf a valid identifier name?

You create an identifier by specifying it in the declaration of a variable, type, or function. In this example, result is an identifier for an integer variable, and main and printf are identifier names for functions.


1 Answers

The write(2) system call is used with the file descriptor set to STDOUT (its value is 1).

To invoke a system call from assembly, the eax register has to hold the id of the system call (I think the particular number of write() is 3) and the rest of the registers (ebx, ecx, ...) have to contain the arguments. Then doing an int 80h will switch the control from your process to the kernel routine that handles system calls.

The above is platform-specific, but virtually all Unix-like operating systems work like that.

like image 152
Blagovest Buyukliev Avatar answered Sep 21 '22 01:09

Blagovest Buyukliev