Download presentation
Presentation is loading. Please wait.
Published byRosanna Hutchinson Modified over 9 years ago
1
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting procedures Documenting procedures The system stack The system stack Review for Midterm Exam Review for Midterm Exam
2
Passing values/addresses to/from procedures Methods: Methods: 1. Pass parameters in registers 2. Use shared memory (global variables) 3. Pass parameters on the system stack
3
1. Pass parameters in registers Set up registers before call and/or before return Set up registers before call and/or before return Generally … it’s a bad idea to pass parameters in registers Generally … it’s a bad idea to pass parameters in registers Procedure might change/restore register contents Procedure might change/restore register contents However However some Irvine library procedures require values in registers (e.g., “Receives” and “Preconditions” for ReadString) 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) 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 Set up memory contents before call and/or before return Generally … it’s a bad idea to use global variables Generally … it’s a bad idea to use global variables Procedure might change memory contents needed by other procedures (unwanted side-effects) Procedure might change memory contents needed by other procedures (unwanted side-effects) For now … we use globals. For now … we use globals. Later we will pass parameters on the system stack. Later we will pass parameters on the system stack.
5
In all cases, when a procedure is called: In all cases, when a procedure is called: be aware of preconditions be aware of preconditions What conditions must be true before the procedure can perform its task? What conditions must be true before the procedure can perform its task? be aware of what registers are changed (document!) be aware of what registers are changed (document!) save and restore registers if necessary save and restore registers if necessary
6
Saving registers If a procedure changes any registers, the calling procedure might lose important data 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 before call, and restored after return Registers may be saved by the procedure, and restored before the return Registers may be saved by the procedure, and restored before the return
7
Saving/restoring registers Methods: Methods: 1. Move register contents to named memory locations, then restore after procedure returns. 2. Use pushad and popad a) Option 1: calling procedure pushes before call, pops after return b) Option 2: procedure pushes at beginning, and pops before the return 3. Use the USES directive 4. Save selected registers on the system stack a) (Much) more later about this
8
Method 1: Save register contents in memory Example (in main): Example (in main): movaReg, eax;save registers movbReg, ebx moveax, count;set parameters movebx, OFFSET val callsomeProc moveax, aReg;restore registers movebx, bReg …
9
Methods 2: Save all registers on the system stack* PUSHAD pushes the 32-bit general-purpose registers onto the stack PUSHAD pushes the 32-bit general-purpose registers onto the stack order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI POPAD pops the same registers off the stack in reverse order POPAD pops the same registers off the stack in reverse order Note: it’s best to use 32-bit (DWORD) operands Note: it’s best to use 32-bit (DWORD) operands * More about this soon … * More about this soon …
10
Method 2: Save all registers on the system stack Example (Option 1: in calling procedure): Example (Option 1: in calling procedure): pushad;save registers callsomeProc popad;restore registers…
11
Method 2: Save all registers on the system stack Example ( Option 2: in the called procedure): Example ( Option 2: in the called procedure): calcSum PROC pushad;save registers … ;procedure body … popad;restore registers ret calcSumENDP
12
Method 2: Save all registers on the system stack Warnings: Warnings: Be sure that values don't get lost. Be sure that values don't get lost. Be sure that the system stack is properly aligned Be sure that the system stack is properly aligned More later … More later …
13
Method 3: USES Directive Specifies which registers will be preserved Specifies which registers will be preserved Saves specified registers on the system stack before any procedure statements are executed. Saves specified registers on the system stack before any procedure statements are executed. Restores registers before the procedure returns. Restores registers before the procedure returns. MASM adds (invisible) save/restore code for specified registers MASM adds (invisible) save/restore code for specified registers at the beginning and end of the procedure at the beginning and end of the procedure
14
Method 3: USES Directive Example (in the procedure): Example (in the procedure): calcSum PROC USES esi ecx … ;procedure body …ret calcSumENDP Notes: Notes: no commas no commas Be careful ! USES affects the system stack Be careful ! USES affects the system stack
15
Documenting Procedures Documentation for each procedure: Documentation for each procedure: A description of all tasks accomplished by the procedure. A description of all tasks accomplished by the procedure. Receives: A list of input parameters; state usage and requirements. Receives: A list of input parameters; state usage and requirements. Returns: A description of values returned by the procedure. Returns: A description of values returned by the procedure. Preconditions: List of requirements that must be satisfied before the procedure is called. 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 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 The operating system maintains a stack Implemented in memory Implemented in memory LIFO structure LIFO structure Managed by the CPU, using two registers Managed by the CPU, using two registers SS: address of stack segment SS: address of stack segment ESP: stack pointer (always points to “top” of stack) ESP: stack pointer (always points to “top” of stack) i.e., ESP contains the address of the top of the stack i.e., ESP contains the address of the top of the stack
19
PUSH Operation A push operation A push operation decrements the stack pointer by 4 decrements the stack pointer by 4 copies a value into the location pointed to by the stack pointer. copies a value into the location pointed to by the stack pointer. Actual decrement depends on the size of the operand Actual decrement depends on the size of the operand Note: it’s best to use 32-bit (DWORD, 4-byte) operands Note: it’s best to use 32-bit (DWORD, 4-byte) operands
20
Example PUSH AddressContents …etc 01ECh? 01F0h? 01F4h? 01F8h? 01FCh? 0200h25 Stack Segment in Memory Suppose that ecx contains 317 and esp contains 0200h. In this case, [esp] is 25 * esp:0200h [esp]:25 The next instruction is pushecx Execute push ecx esp:01FCh [esp]:317 317 *Note: [esp] means “contents of memory at the address in esp” Note: esp is decremented, then 317 is stored in the stack
21
POP Operation A pop operation A pop operation Copies value at ESP into a register or variable. Copies value at ESP into a register or variable. increments the stack pointer by 4 increments the stack pointer by 4 Actual increment depends on the size of the operand Actual increment depends on the size of the operand Note: it’s best to use 32-bit (DWORD, 4-byte) operands Note: it’s best to use 32-bit (DWORD, 4-byte) operands
22
AddressContents …etc 01ECh? 01F0h? 01F4h? 01F8h? 01FCh317 0200h25 esp:01FCh [esp]:317 esp:0200h [esp]:25 Example POP Stack Segment in Memory Suppose that esp contains 01FCh. In this case, [esp] is 317. The next instruction is popeax Execute pop eax eax now contains 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 syntax: PUSH r/m32 PUSH r/m32 PUSH immed PUSH immed POP syntax: POP syntax: POP r/m32 POP r/m32
24
Using PUSH and POP push ecx; save registers push ebx mov ecx,100h mov ebx,0 ; etc. pop ebx; restore registers pop ecx Save and restore registers when they contain important values. POP operands occur in the opposite of the order of PUSH operands.
25
Example: Nested Loop 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 Push the outer loop counter before entering the inner loop. Pop the outer loop counter when the inner loop terminates.
26
When not to push Be sure that PUSH does not hide a return address 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 Be sure that POP does not lose a return address and/or replace needed values
27
Questions? Midterm Exam Review Midterm Exam Review
28
Midterm Exam Format: Format: 40% Calculations, definitions, short answer, multiple choice 40% Calculations, definitions, short answer, multiple choice 40% MASM code tracing 40% MASM code tracing 20% Writing MASM code 20% Writing MASM code Covers: Covers: Lectures # 1 – 10 Lectures # 1 – 10 MASM programming MASM programming Programs #1 & 2 Programs #1 & 2 Homework #1 Homework #1 Quizzes #1 & 2 Quizzes #1 & 2 Calculator and one 8x11 notecard permitted (both sides) Calculator and one 8x11 notecard permitted (both sides) No sharing No sharing
29
Midterm Exam Topics How computers work How computers work CISC: microprograms CISC: microprograms Memory, CPU, registers, ALU, buses, etc. Memory, CPU, registers, ALU, buses, etc. VonNeumann architecture, Instruction Execution Cycle, pipelining, etc. VonNeumann architecture, Instruction Execution Cycle, pipelining, etc. Internal representation Internal representation Numbers, characters, instructions, addresses, etc. Numbers, characters, instructions, addresses, etc. Floating-point, hamming codes, etc. Floating-point, hamming codes, etc. External representation External representation Binary, decimal, hexadecimal Binary, decimal, hexadecimal Peripheral devices Peripheral devices Magnetic disk drives Magnetic disk drives IA-32 architecture IA-32 architecture Register names, etc. Register names, etc. Byte-ordering (little-endian) Byte-ordering (little-endian)
30
Midterm Exam Topics Assembly Language Assembly Language Related to high-level languages Related to high-level languages Related to machine languages Related to machine languages Related to architecture Related to architecture Motivations for using assembly language Motivations for using assembly language Program development Program development Modularization Modularization Incremental development/testing Incremental development/testing Debugging Debugging
31
Midterm Exam Topics MASM MASM Program structure, commenting Program structure, commenting Assembly, linking, etc. Assembly, linking, etc. Segments, directives, etc. Segments, directives, etc. Labels, identifiers, constants Labels, identifiers, constants Data types, declarations Data types, declarations BYTE, WORD, DWORD, etc. BYTE, WORD, DWORD, etc. strings strings Addressing modes Addressing modes immediate, register, direct (i.e., memory), indirect (i.e., [register]) immediate, register, direct (i.e., memory), indirect (i.e., [register]) Instructions: Instructions: mov, push, pop mov, push, pop add, sub, mul, div, inc, dec add, sub, mul, div, inc, dec cmp, jmp, j, loop cmp, jmp, j, loop call, ret call, ret
32
Midterm Exam Topics MASM MASM Control structures Control structures Procedures, procedure calls Procedures, procedure calls return address, etc return address, etc Decisions Decisions Repetition Repetition Data validation Data validation System stack System stack Procedure calls and returns Procedure calls and returns Fundamentals of push and pop Fundamentals of push and pop
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.