1 / 32

Today’s topics

Today’s topics. Procedures Passing values to/from procedures Saving registers Documenting procedures The system stack Review for Midterm Exam. Passing values/addresses to/from procedures. Methods: Pass parameters in registers Use shared memory (global variables)

hyatt-burt
Download Presentation

Today’s topics

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. Today’s topics • Procedures • Passing values to/from procedures • Saving registers • Documenting procedures • The system stack • Review for Midterm Exam

  2. Passing values/addresses to/from procedures • Methods: • Pass parameters in registers • Use shared memory (global variables) • Pass parameters on the system stack

  3. 1. Pass parameters in registers • Set up registers before call and/or before return • Generally … it’s a bad idea to pass parameters in registers • Procedure might change/restore register contents • However • some Irvine library procedures require values in registers (e.g., “Receives” and “Preconditions” for ReadString) • some Irvine library procedures return values in registers (e.g., “Returns” for ReadInt)

  4. 2. Use shared memory(global variables) • Set up memory contents before call and/or before return • Generally … it’s a bad idea to use global variables • Procedure might change memory contents needed by other procedures (unwanted side-effects) • For now … we use globals. • Later we will pass parameters on the system stack.

  5. In all cases, when a procedure is called: • be aware of preconditions • What conditions must be true before the procedure can perform its task? • be aware of what registers are changed (document!) • save and restore registers if necessary

  6. Saving registers • If a procedure changes any registers, the calling procedure might lose important data • Registers may be saved before call, and restored after return • Registers may be saved by the procedure, and restored before the return

  7. Saving/restoring registers • Methods: • Move register contents to named memory locations, then restore after procedure returns. • Use pushad and popad • Option 1: calling procedure pushes before call, pops after return • Option 2: procedure pushes at beginning, and pops before the return • Use the USES directive • Save selected registers on the system stack • (Much) more later about this

  8. Method 1: Save register contents in memory • Example (in main): mov aReg, eax ;save registers mov bReg, ebx mov eax, count ;set parameters mov ebx, OFFSET val call someProc mov eax, aReg ;restore registers mov ebx, bReg …

  9. Methods 2: Save all registers on the system stack* • PUSHAD pushes the 32-bit general-purpose registers onto the stack • order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI • POPAD pops the same registers off the stack in reverse order • Note: it’s best to use 32-bit (DWORD) operands • * More about this soon …

  10. Method 2: Save all registers on the system stack • Example (Option 1: in calling procedure): pushad ;save registers call someProc popad ;restore registers …

  11. Method 2: Save all registers on the system stack • Example (Option 2: in the called procedure): calcSum PROC pushad ;save registers … ;procedure body … popad ;restore registers ret calcSum ENDP

  12. Method 2: Save all registers on the system stack • Warnings: • Be sure that values don't get lost. • Be sure that the system stack is properly aligned • More later …

  13. Method 3: USES Directive • Specifies which registers will be preserved • Saves specified registers on the system stack before any procedure statements are executed. • Restores registers before the procedure returns. • MASM adds (invisible) save/restore code for specified registers • at the beginning and end of the procedure

  14. Method 3: USES Directive • Example (in the procedure): calcSum PROC USES esi ecx … ;procedure body … ret calcSum ENDP • Notes: • no commas • Be careful !USES affects the system stack

  15. Documenting Procedures • Documentation for each procedure: • A description of all tasks accomplished by the procedure. • Receives: A list of input parameters; state usage and requirements. • Returns: A description of values returned by the procedure. • Preconditions: List of requirements that must be satisfied before the procedure is called. • Registers changed: List of registers that may have different values than they had when the procedure was called If a procedure is called without satisfying its preconditions, the procedure's creator makes no promise that it will work.

  16. Procedure Documentation (example) ;calculate: procedure to calculate ; the summation of integers from ; a to b. ;receives: none (a, b, sum are global) ;returns: sum = a+(a+1)+ ... +b ;preconditions: a <= b ;registers changed: eax,ebx,ecx

  17. Questions on procedures?

  18. System Stack(Runtime Stack) • The operating system maintains a stack • Implemented in memory • LIFO structure • Managed by the CPU, using two registers • SS: address of stack segment • ESP: stack pointer (always points to “top” of stack) • i.e., ESP contains the address of the top of the stack

  19. PUSH Operation • A push operation • decrements the stack pointer by 4 • copies a value into the location pointed to by the stack pointer. • Actual decrement depends on the size of the operand • Note: it’s best to use 32-bit (DWORD, 4-byte) operands

  20. Example PUSH Stack Segment in Memory Suppose that ecx contains 317 and esp contains 0200h. In this case, [esp] is 25 * The next instruction is push ecx Execute push ecx 317 esp: 01FCh [esp]:317 esp: 0200h [esp]: 25 Note: esp is decremented, then 317 is stored in the stack *Note: [esp] means “contents of memory at the address in esp”

  21. POP Operation • A pop operation • Copies value at ESP into a register or variable. • increments the stack pointer by 4 • Actual increment depends on the size of the operand • Note: it’s best to use 32-bit (DWORD , 4-byte) operands

  22. Example POP Stack Segment in Memory Suppose that esp contains 01FCh. In this case, [esp] is 317. The next instruction is pop eax Execute pop eax eax now contains 317 esp: 0200h [esp]:25 esp: 01FCh [esp]: 317 Note: 317 is copied to EAX, then then ESP is incremented. Memory contents unchanged.

  23. PUSH and POP Instructions(32-bit) • PUSH syntax: • PUSH r/m32 • PUSH immed • POP syntax: • POP r/m32

  24. Using PUSH and POP Save and restore registers when they contain important values. POP operands occur in the opposite of the order of PUSH operands. push ecx ; save registers push ebx mov ecx,100h mov ebx,0 ; etc. pop ebx ; restore registers pop ecx

  25. Example: Nested Loop Push the outer loop counter before entering the inner loop. Pop the outer loop counter when the inner loop terminates. mov ecx,100 ; set outer loop count L1: ; begin the outer loop push ecx ; save outer loop count mov ecx,20 ; set inner loop count L2: ; begin the inner loop ; ; loop L2 ; repeat the inner loop pop ecx ; restore outer loop count loop L1 ; repeat the outer loop

  26. When not to push • Be sure that PUSH does not hide a return address • Be sure that POP does not lose a return address and/or replace needed values

  27. Questions? • Midterm Exam Review

  28. Midterm Exam • Format: • 40% Calculations, definitions, short answer, multiple choice • 40% MASM code tracing • 20% Writing MASM code • Covers: • Lectures # 1 – 10 • MASM programming • Programs #1 & 2 • Homework #1 • Quizzes #1 & 2 • Calculator and one 8x11 notecard permitted (both sides) • No sharing

  29. Midterm Exam Topics • How computers work • CISC: microprograms • Memory, CPU, registers, ALU, buses, etc. • VonNeumann architecture, Instruction Execution Cycle, pipelining, etc. • Internal representation • Numbers, characters, instructions, addresses, etc. • Floating-point, hamming codes, etc. • External representation • Binary, decimal, hexadecimal • Peripheral devices • Magnetic disk drives • IA-32 architecture • Register names, etc. • Byte-ordering (little-endian)

  30. Midterm Exam Topics • Assembly Language • Related to high-level languages • Related to machine languages • Related to architecture • Motivations for using assembly language • Program development • Modularization • Incremental development/testing • Debugging

  31. Midterm Exam Topics • MASM • Program structure, commenting • Assembly, linking, etc. • Segments, directives, etc. • Labels, identifiers, constants • Data types, declarations • BYTE, WORD, DWORD, etc. • strings • Addressing modes • immediate, register, direct (i.e., memory), indirect (i.e., [register]) • Instructions: • mov, push, pop • add, sub, mul, div, inc, dec • cmp, jmp, j<condition>, loop • call, ret

  32. Midterm Exam Topics • MASM • Control structures • Procedures, procedure calls • return address, etc • Decisions • Repetition • Data validation • System stack • Procedure calls and returns • Fundamentals of push and pop

More Related