Passing Arguments to a Function The sample program in Example 5.4 shows how the stack frame is used to pass arguments to a function.The code simply creates some local stack variables, fills them with values, and passes them to a function called callex().The callex() function takes the supplied arguments and prints them to the screen. Example 5.4 Stack and Passing Parameters to a Function /* stack2.c */ #include #include int callex(char *buffer, int int1, int int2) { /* This prints the input variables to the screen:*/ printf("%s %d %d\n",buffer,int1, int2); return 1; } int main(int argc, char **argv) { Stack Overflows • Chapter 5 173 char buffer[15]="Hello Buffer"; /* a 15-byte character buffer with 12 characters filled/* int int1=1, int2=2; /* two four-byte integers */ callex(buffer,int1,int2); /*call our function*/ return 1; /*leaves the main function*/ } You need to compile this example in MSVC in a console application in Release mode or in GCC without optimizations. Example 5.5 shows a direct IDA Pro disassembly of the callex() and main() functions, to demonstrate how a function looks after it has been compiled. Notice how the buffer variable from main() is passed to callex() by reference. In other words, callex() gets a pointer to buffer rather than its own copy, as is supposed to happen in C.This means that anything that is done to change buffer while in callex() will also affect buffer in main(), since they are the same variable. Write Assembly Code for stack2.c
Passing Arguments to a Function
The sample program in Example 5.4 shows how the stack frame is used to pass
arguments to a function.The code simply creates some local stack variables, fills
them with values, and passes them to a function called callex().The callex() function takes the supplied arguments and prints them to the screen.
Example 5.4 Stack and Passing Parameters to a Function
/* stack2.c */
#include <stdlib.h>
#include <stdio.h>
int callex(char *buffer, int int1, int int2)
{
/* This prints the input variables to the screen:*/
printf("%s %d %d\n",buffer,int1, int2);
return 1;
}
int main(int argc, char **argv)
{
Stack Overflows • Chapter 5 173
char buffer[15]="Hello Buffer"; /* a 15-byte character buffer
with
12 characters filled/*
int int1=1, int2=2; /* two four-byte integers */
callex(buffer,int1,int2); /*call our function*/
return 1; /*leaves the main function*/
}
You need to compile this example in MSVC in a console application in
Release mode or in GCC without optimizations. Example 5.5 shows a direct
IDA Pro disassembly of the callex() and main() functions, to demonstrate how a
function looks after it has been compiled. Notice how the buffer variable from
main() is passed to callex() by reference. In other words, callex() gets a pointer to
buffer rather than its own copy, as is supposed to happen in C.This means that
anything that is done to change buffer while in callex() will also affect buffer in
main(), since they are the same variable.
Write Assembly Code for stack2.c
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps with 3 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)