- In real programming scenario, its difficult to manage big programs. Because a Big program does a lot of functions. More than one programmers are required some times to develop a programming project.
- SO it is difficult to manage programmers working on the project. Because any change woould adversely affect another part, on which a different programmer is working.
- So to avoid such cases, we need to break the programs in separate pieces, and these peices should communicate with each other using well defined interfaces. This way, diffrent peices can be developed and tested independently, and making very easy for programmers to work on a project.
- So to sort out this issues, we need "FUNCTIONS". These function will break a big program into peices. Functions are basically units of codes that do a specified task on specific data. Example:
- "Word Processor"
- so word processor can have function named handle_characters.
- Data which is used by this function is "key press" and the "document curretly opened by the user". The function will modify the document according to the keypresses.
- The data items which are given to a function for processing are known as parameters.
- In our example key press and current document are the parameters for the function handle characters.
- Function's Interface: the parameters list and the processing expectation of the function is known as function's interface.
- We need to put a lot of efforts for designing funtion interfaces, because if they are called from different places in a program, then it is difficult to change them if necessary.
- But there are many cases where you cannot write functions. Such cases should be handled by the operating system itself. So the functions supplied for such cases are known as primitive functions.
- Suppose you have to write a function to draw a GUI. Then u need to use different functions for writing text, writing icons, drawing a background and so on. So the function should call other functions which are supplied by operating system itself to draw basic line or point drawing.
- programming can be treated as breaking a program into smaller peices untill you get to the primitive functions.
- Building something over primitive functions, incementally, untill you reach to a bigger picture.
- In assembly language, the primitives are same as system calls, even though system call's are not true functions.
- Now lets see how functions work:
- Functions contains many different peices:
- 1. Function Name
- 2. Function Parameters
- 3. Local Variables
- 4. Static Variables
- 5. Global Variables
- 6. Return Address
- 7. Return Value
- So coming in details:
- Functions Name: Address to the location where the funcion code starts.
- FUnction is defined using simple "Labels".
- Function Parameters: Data items which are explicitly given to a function for processing. SINE 2 . Sine is function and 2 is the parameter.
- Local Variable: "Data storage" used by a Function(during processing).
- This data storage is thrown away after processing.
- Static Variables: "Data Storage" contains fixed values. Used by functions everytime the function code is executed. Static variables are not thrown away as well.
- Global Variables: "Data Storage" outside the function. Used by functions for processing. example: A simple Text editor, can put a file on to global variable, so that the file can be used by all the functions of the text editor. Need not give the file as data item to each function.
- Return Address: It is a parameter which is invisible. It is not used inside a function directly. It guides the function to where to return executing after the function is completed. Because, in real programming scenario, a function can be called at many places, then as soon as the function is completed, it should return back to the place where it came from. In assembly, we use "call" instruction to supply a return address to the function. And we use "ret" instruction to the function which is responsible to use the return address given by "call" and get the function back to, where it came from.
- Return Value: Main method which is responsible for sending data back to main program.
- So, the way these data are stored in, and parameters and return addresses are transferred to functions, differes in programming languages. this difference is known as language's "calling convention".
- In assembly we can use any calling convention, and even we can make our own calling conventions. But to interoperate with other programming languages, we need to follow particular programming language calling conventions.
- For our session we will use, C language calling conventions. So lets start:
- To write assembly instructions, we need to know about computer stack's first.
- Each computer program, which is running, uses a region in memory(called STACK), so that functions can run properly. You all geenrally keep the things on top on which u r working, and keeps them off after ur done working with them.
- In case of computer's stack:
- Stack lives at very top addresses of Memory.
- You can push values on TOP of the STACK, using "pushl" instruction.
- Value which is pushed can be a registers value or memory value.
- In C language calling convention, STACK is the key element, where we can implement a function's local variable, parameters and return address.
- Before execution starts: a program pushes all of the parameters for a function onto the stack in reverse order, that they are documented. Then the program issues a call instruction indicating which function it wishes to start.
- Hackveda Labs
Functions In Assembly Language
a guest Dec 22nd, 2011 184 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
RAW Paste Data