Assembly Language for Beginners

(nextflipdebug2) #1

1.8 printf() with several arguments.


_f1 ENDP


Unlike MSVC 2010, MSVC 2013 allocated a/b/c variables in functionf2()in reverse order.And this is
completelycorrect, becauseC/C++standardshasnorule, inwhichorderlocalvariablesmustbeallocated
in the local stack, if at all. The reason of difference is because MSVC 2010 has one way to do it, and MSVC
2013 has supposedly something changed inside of compiler guts, so it behaves slightly different.


1.7.5 Exercises.



1.8 printf() with several arguments


Now let’s extend theHello, world!(1.5 on page 8) example, replacingprintf()in themain()function
body with this:


#include <stdio.h>


int main()
{
printf("a=%d; b=%d; c=%d", 1, 2, 3);
return 0;
};


1.8.1 x86


x86: 3 arguments


MSVC


When we compile it with MSVC 2010 Express we get:


$SG3830 DB 'a=%d; b=%d; c=%d', 00H


...


push 3
push 2
push 1
push OFFSET $SG3830
call _printf
add esp, 16 ; 00000010H

Almost the same, but now we can see theprintf()arguments are pushed onto the stack in reverse order.
The first argument is pushed last.


By the way, variables ofinttype in 32-bit environment have 32-bit width, that is 4 bytes.


So, we have 4 arguments here. 4 ∗4 = 16—they occupy exactly 16 bytes in the stack: a 32-bit pointer to
a string and 3 numbers of typeint.


When thestack pointer(ESPregister) has changed back by the
ADD ESP, Xinstruction after a function call, often, the number of function arguments could be deduced
by simply dividing X by 4.


Of course, this is specific to thecdeclcalling convention, and only for 32-bit environment.


See also the calling conventions section (6.1 on page 734).

Free download pdf