1 / 24

Introduction to Machine And Assembly Language

Okay. Math turned out to be easy on the processorsWe understand procedures in a basic sense (CALL, RET)But how can we do more with procedures?Quick refresher: endless.asm. Two Ways to View the World. Many people like to use the MASM macros (like LOCAL) to make programming easierI personally lik

eternity
Download Presentation

Introduction to Machine And Assembly Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Introduction to Machine And Assembly Language Advanced Procedures: Stack Parameters Dr. Richard Ford

    2. Okay… Math turned out to be easy on the processors We understand procedures in a basic sense (CALL, RET) But how can we do more with procedures? Quick refresher: endless.asm

    3. Two Ways to View the World Many people like to use the MASM macros (like LOCAL) to make programming easier I personally like complete control over everything and so write my code natively Your mileage may vary

    4. LOCAL Variables A local variable is a variable that is created, used and destroyed within a single procedure You should be comfortable with the idea of variable scope from HLLs So far, all variables we have used in assembly were in .data – these are global variables (what does that mean?)

    5. Local Advantages Easier debugging, as the lifetime of the variable is contained Memory efficient – no persistent storage as memory is allocated as needed Same variable name can occur in multiple PROCS without creating a name clash

    6. The LOCAL Directive Simple to use LOCAL varlist Varlist is a list of variable definitions separated by commas. It may span more than one line Example: MySub PROC LOCAL var1: BYTE MySub PROC LOCAL temp: DWORD, SwapFlag: BYTE

    7. Assembly Code (1) Code: BubbleSort PROC LOCAL temp: DWORD, SwapFlag: BYTE ; ret BubbleSort ENDP

    8. Assembly Code (2) Machine code: BubbleSort: push ebp mov ebp, esp add esp, 0FFFFFFF8h mov esp, ebp pop ebp ret

    9. Reserving Stack Space Look in Irvine32.inc .stack 4096 – reserve 4K for stack space If PROCs are nested, stack space is cumulative That is, the stack space used is the sum of the stack space used by each proc! Be warned: stack space can get used up quite quickly

    10. Stack Parameters There’s really only two practical ways to pass data between procs Register Parameters Stack Parameters

    11. Example: DumpMem pushad mov esi, OFFSET array mov ecx, LENGTHOF array mov ebx, TYPE array call DumpMem popad

    12. Example: DumpMem(2) Be warned: your DumpMem works by Register, but it could work like this: push OFFSET array push LENGTHOF array push TYPE array call DumpMem

    13. Why Learn Stack Parameters? It seems to be more complex than register values But what happens if you want to pass a lot of parameters? And… nearly all HLLs pass values on the stack

    14. Invoking INVOKE MASM provides an evil^H^H^H^H easy way to call using the stack: INVOKE INVOKE procedureName [, arglist]

    15. INVOKE Example .data val1 DWORD 12345h val2 DWORD 54321h .code INVOKE AddTwo, val1, val2 push val2 push val1 Call AddTwo Also, see params.asm

    16. INVOKE Parms…

    17. ADDR Operator Pass a pointer when calling the procedure Works out type (near, far etc) on the fly Example: .data myArray BYTE 50 DUP(?) .code INVOKE FillArray, ADDR myArray

    18. PROC revisited Remember PROC? There’s more… Label PROC parameter_1, parameter_2, . . parameter_n Each parameter is of the form paramName: type

    19. Examples AddTwo PROC, val1: DWORD, val2: DWORD … AddTwo ENDP FillArray PROC, pArray: PTR BYTE … FillArray ENDP

    20. PROTO Directive MASM requires a prototype for each procedure PROTO MySub INVOKE MySub MySub PROC/MySub ENDP Or: you can just have MySub PROC somewhere before the first INVOKE MySub

    21. USES Automatically preserves the value of a register in a PROC Example: ArraySum PROC USES esi, ecx

    22. Subtle Point What does this do? .data mydata WORD 1000h .code main PROC INVOKE Sub1, myData exit main ENDP Sub1 PROC someData: WORD mov someData, 0 ret Sub1 ENDP

    23. Pass by… Reference: Pass a pointer to the data (use ADDR) Value: Pass the value When would you use one and not the other?

    24. Troubleshooting Make sure you’re not popping or pushing the wrong numbers of parameters Make sure you’re passing the right size of variables Make sure you’re passing by reference when you want to be

    25. Next Class The last really tricky part of assembly After this, it’s really mostly just practice Stack Frames

More Related