News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

What is the Procedure model ?

Started by hutch--, January 01, 2005, 04:30:11 AM

Previous topic - Next topic

hutch--

Built directly into the processor are instructions that branch from any particular instruction to another block of code and when it has been executed, return to the following instruction after the one that called it. The two instruction normally used to do this are CALL and RET. This basic mechanism built directly into the processor is where the procedure model comes from, it is not just another high level invention.


    instruction
    instruction   --->   branch to and execute another block of code ->-,
    instruction <---  return to the following instruction -----<-----'
    instruction


At the lowest level a procedure is called as follows.


    call label
    ; next instruction


The procedure that it is calling looks like this,


  label:
    ; executable code here
    ret


What happens when you use the CALL instruction is it saves the location of the next instruction on the stack then branches to the address specified with the name "label" where the processor executes the instructions within that procedure until it encounters the RET instruction.

When it find the RET instruction, the processor gets the last address saved on the stack and returns to the next instruction after the the original CALL instruction that produced the branch.

This mechanism works well for simple cases but in most instances you need to be able to pass parameters to the procedure as well and this usually means the construction of what is called a stack frame.

While a coded fully manually stack frame is a bit complicated, MASM has a method that both automates procedures built with a stack frame and performs some testing on the parameters to catch mistakes in the number or size. It is done to a simple set of rules that make procedure a lot easier to write and a lot more reliable and it has the added advantage that you can call the procedure you write this way in much the same way as you do in a higher level language.

It takes a little bit more work but you get easier to use more reliable code by doing it.

First you write what is called a PROTOTYPE which tells the assembler what the procedure name is and how many arguments (parameters) it uses and what size the arguments are. Now if you need a procedure that takes two arguments and they are 32 bit in size, you write a PROTOTYPE like this,


    MyProc PROTO :DWORD,:DWORD


You write the PROTOTYPE up near the front of an assembler file after the main include files and libraries but BEFORE the .DATA or .CODE operators.

You then write the procedure as follows,


MyProc proc arg1:DWORD,arg2:DWORD

    ; Write your executable code here

    ret

MyProc endp


Once this is done, when you assemble the code you have written, the assembler checks the prototype you have written against the procedure you have written and if the procedure is different, it will tell you that you have a programming mistake and where it is in your code. The other advantage of using the procedure model is you can call the procedure using MASM's "INVOKE" operator so that the code is closer to high level style code and thus easier to read.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php