ALT by Bilal Ahmed Shaik - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

img176.png

 

  Assembly Procedures

 

  Procedures or subroutines are very important in assembly language, as the assembly language programs tend to be large in size. Procedures are identified by a name. Following this name, the body of the procedure is described, which perform a well-defined job. End of the procedure is indicated by a return statement.

 

  Syntax:

 

  Following is the syntax to define a procedure:

 

img177.png

  The procedure is called from another function by using the CALL instruction. The CALL instruction should have the name of the called procedure as argument as shown below:

 

img178.png 

  The called procedure returns the control to the calling procedure by using the RET instruction.

 

  Example:

 

  Let us write a very simple procedure named sum that adds the variables stored in the ECX and ED X register and returns the sum in the EAX register:

 

img179.png

img180.png 

  When the above code is compiled and executed, it produces fol owing result:

 

img181.png 

  Stacks Data Structure:

 

  A stack is an array-like data structure in the memory in which data can be stored and removed from a location called the 'top' of the stack. The data need to be stored is 'pushed' into the stack and data to be retrieved is 'popped' out from the stack. Stack is a LIFO data structure, i.e., the data stored first is retrieved last.

 

  Assembly language provides two instructions for stack operations: PUSH and POP. These instructions have syntaxes like:

 

img182.png  The memory space reserved in the stack segment is used for implementing stack. The registers SS and ESP (or SP) are used for implementing the stack. The top of the stack, which points to the last data item inserted into the stack is pointed to by the SS:ESP register, where the SS register points to the beginning of the stack segment and the SP (or ESP) gives the offset into the stack segment.

 

  The stack implementation has the fol owing characteristics:

   Only words or doublewords could be saved into the stack, not a byte.

   The stack grows in the reverse direction i.e., toward the lower memory address

   The top of the stack points to the last item inserted in the stack; it points to the lower byte of the last word inserted.

 

  As we discussed about storing the values of the registers in the stack before using them for some use; it can be done in fol owing way:

 

img183.png

img184.png

  EXAMPLE:

 

  The following program displays the entire ASCII character set. The main program calls a procedure named display, which displays the ASCII character set.

img185.png