wednesday, february 9 midterm exam midterm exam wednesday, feb 22 wednesday, feb 22 program #3...

34
Wednesday, February 9 Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria are posted Due Sunday, Feb 26, before Due Sunday, Feb 26, before midnight midnight Questions? Questions?

Upload: francine-jennings

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Wednesday, February 9Wednesday, February 9 Midterm ExamMidterm Exam

Wednesday, Feb 22Wednesday, Feb 22 Program #3Program #3

Evaluation criteria are postedEvaluation criteria are posted Due Sunday, Feb 26, before Due Sunday, Feb 26, before

midnightmidnight Questions?Questions?

Page 2: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Today’s topicsToday’s topics

ProceduresProcedures Passing values to/from proceduresPassing values to/from procedures Saving registersSaving registers Documenting proceduresDocumenting procedures

The system stackThe system stack

Review for Midterm ExamReview for Midterm Exam

Page 3: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Passing values/addresses Passing values/addresses to/from proceduresto/from procedures

Methods:Methods:1.1. Pass parameters in registersPass parameters in registers

2.2. Use shared memory (global variables)Use shared memory (global variables)

3.3. Pass parameters on the system stackPass parameters on the system stack

Page 4: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

1. Pass parameters in 1. Pass parameters in registersregisters

Set up registers before call and/or before Set up registers before call and/or before returnreturn

Generally … it’s a Generally … it’s a bad ideabad idea to pass to pass parameters in registersparameters in registers Procedure might change/restore register contentsProcedure might change/restore register contents

HoweverHowever some Irvine library procedures some Irvine library procedures requirerequire values in values in

registers (e.g., “registers (e.g., “ReceivesReceives” and “” and “PreconditionsPreconditions” ” for for ReadStringReadString))

some Irvine library procedures some Irvine library procedures returnreturn values in values in registers (e.g., “registers (e.g., “ReturnsReturns” for ” for ReadIntReadInt))

Page 5: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

2. Use shared memory2. Use shared memory(global variables)(global variables)

Set up memory contents before call and/or Set up memory contents before call and/or before returnbefore return

Generally … it’s a Generally … it’s a bad ideabad idea to use global to use global variablesvariables Procedure might change memory contents Procedure might change memory contents

needed by other procedures (unwanted side-needed by other procedures (unwanted side-effects)effects)

For nowFor now … we use globals. … we use globals. Later we will pass parameters on the system Later we will pass parameters on the system

stack.stack.

Page 6: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

In all cases, when a procedure is In all cases, when a procedure is called:called: be aware of preconditions be aware of preconditions

What conditions must be true before the What conditions must be true before the procedure can perform its task?procedure can perform its task?

be aware of what registers are changed be aware of what registers are changed ((document!document!))

save and restore registers if necessary save and restore registers if necessary

Page 7: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Saving registersSaving registers If a procedure changes any If a procedure changes any

registers, the calling procedure registers, the calling procedure might lose important datamight lose important data

Registers may be saved before call, Registers may be saved before call, and restored after returnand restored after return

Registers may be saved by the Registers may be saved by the procedure, and restored before the procedure, and restored before the returnreturn

Page 8: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Saving/restoring registersSaving/restoring registers Methods:Methods:

1.1. Move register contents to named memory Move register contents to named memory locations, then restore after procedure locations, then restore after procedure returns.returns.

2.2. Use Use pushadpushad and and popadpopada)a) Option 1: calling procedure pushes before call, pops Option 1: calling procedure pushes before call, pops

after returnafter return

b)b) Option 2: procedure pushes at beginning, and pops Option 2: procedure pushes at beginning, and pops before the returnbefore the return

3.3. Use the Use the USESUSES directive directive

4.4. Save selected registers on the system stackSave selected registers on the system stacka)a) (Much) more later about this(Much) more later about this

Page 9: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Method 1Method 1: Save register : Save register contents in memorycontents in memory

Example (in main):Example (in main):

movmov aReg, eaxaReg, eax ;save registers;save registers

movmov bReg, ebxbReg, ebx

movmov eax, counteax, count ;set ;set parametersparameters

movmov ebx, OFFSET valebx, OFFSET val

callcall someProcsomeProc

movmov eax, aRegeax, aReg ;restore registers;restore registers

movmov ebx, bRegebx, bReg

……

Page 10: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Methods 2Methods 2: Save all registers : Save all registers on the on the system stack*system stack*

PUSHADPUSHAD pushes the 32-bit general- pushes the 32-bit general-purpose registers onto the stack purpose registers onto the stack order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDIorder: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI

POPADPOPAD pops the same registers off the pops the same registers off the stack in reverse orderstack in reverse order Note: it’s best to use 32-bit (DWORD) operandsNote: it’s best to use 32-bit (DWORD) operands

** More about this soon … More about this soon …

Page 11: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Method 2Method 2: Save all registers on : Save all registers on the system stackthe system stack

Example (Option 1: Example (Option 1: in calling in calling procedureprocedure):):

pushadpushad ;save ;save registersregisters

callcall someProcsomeProc

popadpopad ;restore ;restore registersregisters ……

Page 12: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Method 2Method 2: Save all registers on : Save all registers on the system stackthe system stack

Example (Example (Option 2: Option 2: in the called in the called procedureprocedure):):

calcSum calcSum PROC PROC

pushadpushad ;save registers;save registers

……

;procedure body;procedure body

……

popadpopad ;restore registers;restore registers

retret

calcSumcalcSum ENDPENDP

Page 13: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Method 2Method 2: Save all registers on : Save all registers on the system stackthe system stack

Warnings:Warnings: Be sure that values don't get Be sure that values don't get

lost.lost. Be sure that the system stack is Be sure that the system stack is

properly alignedproperly aligned More later …More later …

Page 14: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Method 3Method 3: : USESUSES Directive Directive Specifies which registers will be Specifies which registers will be

preservedpreserved Saves specified registers on the Saves specified registers on the

system stack before any procedure system stack before any procedure statements are executed.statements are executed.

Restores registers before the Restores registers before the procedure returns.procedure returns.

MASM adds (invisible) save/restore MASM adds (invisible) save/restore code for specified registers code for specified registers at the beginning and end of the at the beginning and end of the

procedureprocedure

Page 15: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Method 3Method 3: : USESUSES Directive Directive Example (in the procedure):Example (in the procedure):calcSum calcSum PROC PROC USESUSES esi ecx esi ecx

……;procedure body;procedure body

……retret

calcSumcalcSum ENDPENDP Notes:Notes:

no commasno commas Be careful !Be careful ! USESUSES affects the system affects the system

stackstack

Page 16: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Documenting ProceduresDocumenting Procedures Documentation for each procedure:Documentation for each procedure:

A A descriptiondescription of all tasks accomplished by the procedure. of all tasks accomplished by the procedure. ReceivesReceives:: A list of input parameters; state usage and A list of input parameters; state usage and

requirements.requirements. ReturnsReturns:: A description of values returned by the A description of values returned by the

procedure.procedure. PreconditionsPreconditions:: List of requirements that must be List of requirements that must be

satisfied before the procedure is called.satisfied before the procedure is called. Registers changedRegisters changed: List of registers that may have : List of registers that may have

different values than they had when the procedure was different values than they had when the procedure was calledcalled

If a procedure is called without satisfying its preconditions, the procedure's creator makes no promise that it will work.

Page 17: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Procedure Documentation Procedure Documentation (example)(example)

;;calculate: procedure to calculate calculate: procedure to calculate

; the summation of integers from; the summation of integers from

; a to b.; a to b.

;receives: none (a, b, sum are global);receives: none (a, b, sum are global)

;returns: sum = a+(a+1)+ ... +b;returns: sum = a+(a+1)+ ... +b

;preconditions: a <= b;preconditions: a <= b

;registers changed: eax,ebx,ecx;registers changed: eax,ebx,ecx

Page 18: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Questions on procedures?Questions on procedures?

Page 19: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

System StackSystem Stack(Runtime Stack)(Runtime Stack)

The operating system maintains a The operating system maintains a stackstack Implemented in memory Implemented in memory LIFO structureLIFO structure

Managed by the CPU, using two registersManaged by the CPU, using two registers SS: address of stack segmentSS: address of stack segment ESP: stack pointer (always points to “top” of ESP: stack pointer (always points to “top” of

stack)stack) i.e., ESP contains the address of the top of the i.e., ESP contains the address of the top of the

stack stack

Page 20: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

PUSHPUSH Operation Operation A A pushpush operation operation

decrementsdecrements the stack pointer by 4 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 operandActual decrement depends on the size of the operand Note: it’s best to use 32-bit (DWORD, 4-byte) operandsNote: it’s best to use 32-bit (DWORD, 4-byte) operands

Page 21: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Example PUSHExample PUSH

AddressAddress ContentsContents

……etcetc

01ECh01ECh ??

01F0h01F0h ??

01F4h01F4h ??

01F8h01F8h ??

01FCh01FCh ??

0200h0200h 2525

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

push ecx

Execute push ecx

esp: 01FCh

[esp]: 317

317317

*Note*Note:: [esp] [esp] means “contents of memory at the means “contents of memory at the addressaddress in in espesp””

Note: esp is decremented, then 317 is stored in the stack

Page 22: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

POP OperationPOP Operation

A A poppop operation operation Copies value at ESP into a register or Copies value at ESP into a register or

variable.variable. incrementsincrements the stack pointer by 4 the stack pointer by 4

Actual increment depends on the size Actual increment depends on the size of the operandof the operand Note: it’s best to use 32-bit (DWORD , 4-Note: it’s best to use 32-bit (DWORD , 4-

byte) operandsbyte) operands

Page 23: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

AddressAddress ContentsContents

……etcetc

01ECh01ECh ??

01F0h01F0h ??

01F4h01F4h ??

01F8h01F8h ??

01FCh01FCh 317317

0200h0200h 2525esp: 01FCh

[esp]: 317

esp: 0200h

[esp]: 25

Example POPExample POPStack Segment in

MemorySuppose that esp contains01FCh.In this case, [esp] is 317.

The next instruction is

pop eax

Execute pop eax eax now contains 317

Note: 317 is copied to EAX, then then ESP is incremented. Memory contents unchanged.

Page 24: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

PUSH and POP InstructionsPUSH and POP Instructions(32-bit)(32-bit)

PUSH syntax:PUSH syntax: PUSH PUSH r/m32r/m32 PUSH PUSH immedimmed

POP syntax:POP syntax: POP POP r/m32r/m32

Page 25: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Using PUSH and POPUsing PUSH and POP

push ecx ; save registerspush ebx

mov ecx,100hmov ebx,0

; etc.

pop ebx ; restore registerspop ecx

Save and restore registers when they contain important values. POP operands occur in the opposite of the order of PUSH operands.

Page 26: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Example: Nested LoopExample: Nested Loop

mov ecx,100 ; set outer loop countL1: ; begin the outer loop

push ecx ; save outer loop count

mov ecx,20 ; set inner loop countL2: ; begin the inner loop

;;loop L2 ; repeat the inner loop

pop ecx ; restore outer loop countloop 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.

Page 27: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

When When notnot to push to push

Be sure that Be sure that PUSHPUSH does not hide a does not hide a return addressreturn address

Be sure that Be sure that POPPOP does not lose a does not lose a return address and/or replace needed return address and/or replace needed valuesvalues

Page 28: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Questions?Questions?

Midterm Exam ReviewMidterm Exam Review

Page 29: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Midterm ExamMidterm Exam Wednesday, February 16Wednesday, February 16 Format:Format:

40% Calculations, definitions, short answer, multiple 40% Calculations, definitions, short answer, multiple choicechoice

40% MASM code tracing40% MASM code tracing 20% Writing MASM code20% Writing MASM code

Covers:Covers: Lectures # 1 – 11Lectures # 1 – 11 MASM programmingMASM programming

Programs #1 & 2Programs #1 & 2

Homework #1Homework #1 Quizzes #1 & 2Quizzes #1 & 2

Calculator and one 4x6 notecard permitted Calculator and one 4x6 notecard permitted No sharingNo sharing

Page 30: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Midterm Exam TopicsMidterm Exam Topics How computers workHow computers work

CISC: microprogramsCISC: microprograms Memory, CPU, registers, ALU, buses, etc.Memory, CPU, registers, ALU, buses, etc. VonNeumann architecture, Instruction Execution Cycle, VonNeumann architecture, Instruction Execution Cycle,

pipelining, etc.pipelining, etc. Internal representationInternal representation

Numbers, characters, instructions, addresses, etc.Numbers, characters, instructions, addresses, etc. Floating-point, hamming codes, etc.Floating-point, hamming codes, etc.

External representationExternal representation Binary, decimal, hexadecimalBinary, decimal, hexadecimal

Peripheral devicesPeripheral devices Magnetic disk drivesMagnetic disk drives

IA-32 architectureIA-32 architecture Register names, etc.Register names, etc. Byte-ordering (little-endian)Byte-ordering (little-endian)

Page 31: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Midterm Exam TopicsMidterm Exam Topics

Assembly LanguageAssembly Language Related to high-level languagesRelated to high-level languages Related to machine languagesRelated to machine languages Related to architectureRelated to architecture Motivations for using assembly languageMotivations for using assembly language

Program developmentProgram development ModularizationModularization Incremental development/testingIncremental development/testing DebuggingDebugging

Page 32: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Midterm Exam TopicsMidterm Exam Topics MASMMASM

Program structure, commentingProgram structure, commenting Assembly, linking, etc.Assembly, linking, etc. Segments, directives, etc.Segments, directives, etc. Labels, identifiers, constantsLabels, identifiers, constants Data types, declarationsData types, declarations

BYTE, WORD, DWORD, etc.BYTE, WORD, DWORD, etc. stringsstrings

Addressing modesAddressing modes immediate, register, direct (i.e., memory), indirect immediate, register, direct (i.e., memory), indirect

(i.e., [register])(i.e., [register]) Instructions:Instructions:

mov, push, popmov, push, pop add, sub, mul, div, inc, decadd, sub, mul, div, inc, dec cmp, jmp, j<condition>, loopcmp, jmp, j<condition>, loop call, retcall, ret

Page 33: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Midterm Exam TopicsMidterm Exam Topics MASMMASM

Control structuresControl structures Procedures, procedure callsProcedures, procedure calls

return address, etcreturn address, etc DecisionsDecisions RepetitionRepetition Data validationData validation

System stackSystem stack Procedure calls and returnsProcedure calls and returns Fundamentals of push and popFundamentals of push and pop

Page 34: Wednesday, February 9 Midterm Exam Midterm Exam Wednesday, Feb 22 Wednesday, Feb 22 Program #3 Program #3 Evaluation criteria are posted Evaluation criteria

Questions?Questions?Program #2Program #2

Due SundayDue Sunday

Program #3Program #3

Due Sunday++Due Sunday++

Midterm ExamMidterm Exam

Next WednesdayNext Wednesday