Today’s topics Procedures The system stack Review for Midterm Exam

advertisement
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:
1.
2.
3.
Pass parameters in registers
Use shared memory (global variables)
Pass parameters on the system stack
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)
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.

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
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
Saving/restoring registers

Methods:
1.
2.
Move register contents to named memory locations,
then restore after procedure returns.
Use pushad and popad
a)
b)
3.
4.
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
a)
(Much) more later about this
Method 1: Save register contents
in memory

Example (in main):
mov
aReg, eax
mov
bReg, ebx
mov
eax, count
mov
ebx, OFFSET
call
someProc
mov
eax, aReg
mov
ebx, bReg
…
;save registers
;set parameters
val
;restore registers
Methods 2: Save all registers on
the system stack*

PUSHAD pushes the 32-bit general-purpose
registers onto the stack


POPAD pops the same registers off the stack in
reverse order


order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI
Note: it’s best to use 32-bit (DWORD) operands
* More about this soon …
Method 2: Save all registers on the
system stack

Example (Option 1: in calling procedure):
pushad
;save registers
call
someProc
popad
;restore registers
…
Method 2: Save all registers on the
system stack

Example (Option 2: in the called procedure):
calcSum PROC
pushad
…
;procedure body
…
popad
ret
calcSum ENDP
;save registers
;restore registers
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 …
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
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
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.
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
Questions on procedures?
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
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
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
esp:
esp: 01FCh
0200h
[esp]:
[esp]: 317
25
Address
…etc
01ECh
01F0h
01F4h
01F8h
01FCh
0200h
Contents
Note: esp is decremented, then 317 is stored in the stack
*Note: [esp] means “contents of memory at the address in esp”
?
?
?
?
317?
25
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
Example POP Stack Segment in
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:
esp:
0200h
01FCh
[esp]:
[esp]: 25
317
Memory
Address
…etc
01ECh
01F0h
01F4h
01F8h
01FCh
0200h
Contents
?
?
?
?
317
25
Note: 317 is copied to EAX, then then ESP is
incremented. Memory contents unchanged.
PUSH and POP Instructions
(32-bit)

PUSH syntax:



PUSH r/m32
PUSH immed
POP syntax:

POP r/m32
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
push ebx
mov
mov
; save registers
ecx,100h
ebx,0
; etc.
pop ebx
pop ecx
; restore registers
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
L1:
push ecx
; set outer loop count
; begin the outer loop
; save outer loop count
mov ecx,20
L2:
;
;
loop L2
; set inner loop count
; begin the inner loop
pop ecx
loop L1
; restore outer loop count
; repeat the outer loop
; repeat the inner loop
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
Questions?

Midterm Exam Review
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
Midterm Exam Topics

How computers work




Internal representation



Binary, decimal, hexadecimal
Peripheral devices


Numbers, characters, instructions, addresses, etc.
Floating-point, hamming codes, etc.
External representation


CISC: microprograms
Memory, CPU, registers, ALU, buses, etc.
VonNeumann architecture, Instruction Execution Cycle, pipelining,
etc.
Magnetic disk drives
IA-32 architecture


Register names, etc.
Byte-ordering (little-endian)
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
Midterm Exam Topics

MASM





Program structure, commenting
Assembly, linking, etc.
Segments, directives, etc.
Labels, identifiers, constants
Data types, declarations



Addressing modes


BYTE, WORD, DWORD, etc.
strings
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
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

Download