Defining and Using Procedures PDF
Document Details
Uploaded by WorthyAllusion9016
SZABIST
Tags
Summary
This document details assembly language procedures, explaining how to create, document, and use them. It includes examples, showing how to calculate sums, and demonstrate nested procedure calls and instructions like CALL and RET.
Full Transcript
Defining and Using Procedures Creating Procedures Documenting Procedures Example: SumOf Procedure CALL and RET Instructions Nested Procedure Calls Local and Global Labels Procedure Parameters Flowchart Symbols USES Operator Creating Procedu...
Defining and Using Procedures Creating Procedures Documenting Procedures Example: SumOf Procedure CALL and RET Instructions Nested Procedure Calls Local and Global Labels Procedure Parameters Flowchart Symbols USES Operator Creating Procedures Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java or C++ function Following is an assembly language procedure named sample: sample PROC.. ret sample ENDP Documenting Procedures Suggested documentation for each procedure: A description of all tasks accomplished by the procedure. Receives: A list of input parameters; state their usage and requirements. Returns: A description of values returned by the procedure. Requires: Optional list of requirements called preconditions that must be satisfied before the procedure is called. If a procedure is called without its preconditions having been satisfied, the procedure's creator makes no promise that it will work. Example: SumOf Procedure ;--------------------------------------------------------- SumOf PROC ; ; Calculates and returns the sum of three 32-bit integers. ; Receives: EAX, EBX, ECX, the three integers. May be ; signed or unsigned. ; Returns: EAX = sum, and the status flags (Carry, ; Overflow, etc.) are changed. ; Requires: nothing ;--------------------------------------------------------- add eax,ebx add eax,ecx ret SumOf ENDP CALL and RET Instructions The CALL instruction calls a procedure pushes offset of next instruction on the stack copies the address of the called procedure into EIP The RET instruction returns from a procedure pops top of stack into EIP CALL-RET Example (1 of 2) main PROC 00000020 call MySub 0000025 is the offset of the 00000025 mov eax,ebx instruction immediately. following the CALL. instruction main ENDP MySub PROC 00000040 is the offset of 00000040 mov eax,edx the first instruction inside. MySub. ret MySub ENDP CALL-RET Example (2 of 2) The CALL instruction 00000025 ESP 00000040 pushes 00000025 onto the stack, and loads EIP 00000040 into EIP The RET instruction 00000025 ESP 00000025 pops 00000025 from the stack into EIP EIP (stack shown before RET executes) Nested Procedure Calls main PROC.. By the time Sub3 is called, the call Sub1 exit stack contains all three return main ENDP addresses: Sub1 PROC. (ret to main). (ret to Sub1) call Sub2 ret Sub1 ENDP (ret to Sub2) ESP Sub2 PROC.. call Sub3 ret Sub2 ENDP Sub3 PROC.. ret Sub3 ENDP Local and Global Labels A local label is visible only to statements inside the same procedure. A global label is visible everywhere. main PROC jmp L2 ; error L1: ; global label exit main ENDP sub2 PROC @L2: ; local label jmp L1 ; ok ret sub2 ENDP