daily pastebin goal

Functions In Assembly Language

a guest Dec 22nd, 2011 190 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 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.
  2. 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.
  4. 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.
  6. 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:
  7. "Word Processor"
  8. so word processor can have function named handle_characters.
  9. 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.
  11. The data items which are given to a function for processing are known as parameters.
  12. In our example key press and current document are the parameters for the function handle characters.
  14. Function's Interface: the parameters list and the processing expectation of the function is known as function's  interface.
  16. 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.
  18. 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.
  19. 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.
  20. So
  21. programming can be treated as breaking a program into smaller peices untill you get to the primitive functions.
  22. or
  23. Building something over primitive functions, incementally, untill you reach to a bigger picture.
  25. In assembly language, the primitives are same as system calls, even though system call's are not true functions.
  27. Now lets see how functions work:
  28. Functions contains many different peices:
  30. 1. Function Name
  31. 2. Function Parameters
  32. 3. Local Variables
  33. 4. Static Variables
  34. 5. Global Variables
  35. 6. Return Address
  36. 7. Return Value
  38. So coming in details:
  40. Functions Name: Address to the location where the funcion code starts.
  41.                 FUnction is defined using simple "Labels".
  43. Function Parameters: Data items which are explicitly given to a function for processing. SINE 2 . Sine is function and 2 is the parameter.
  45. Local Variable: "Data storage" used by a Function(during processing).
  46.                 This data storage is thrown away after processing.
  48. Static Variables: "Data Storage" contains fixed values. Used by functions everytime the function code is executed. Static variables are not thrown away as well.
  50. 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.
  52. 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.
  54. Return Value: Main method which is responsible for sending data back to main program.
  56. 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".
  57. 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.
  59. For our session we will use, C language calling conventions. So lets start:
  61. To write assembly instructions, we need to know about computer stack's first.
  63. 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.
  64. In case of computer's stack:
  65. Stack lives at very top addresses of Memory.
  66. You can push values on TOP of the STACK, using "pushl" instruction.
  67. Value which is pushed can be a registers value or memory value.
  69. In C language calling convention, STACK is the key element, where we can implement a function's local variable, parameters and return address.
  70. 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.
  73. Hackveda Labs
RAW Paste Data