Tuesday, February 5 - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Tuesday, February 5

Description:

Local and Global Labels. Procedures should terminate by executing a ... Send/return values in registers. Passing by reference. Send address (offset) in register ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 28
Provided by: paul313
Category:

less

Transcript and Presenter's Notes

Title: Tuesday, February 5


1
Tuesday, February 5
  • Quiz 2, Prog 1
  • Scores are posted
  • Pick up
  • Program 2
  • Due Thursday, February 7, before midnight
  • Do not delay !
  • This program is NOT easy (like Program 1)

2
Todays topics
  • Example development with procedures
  • Data validation
  • More procedures, parameter passing
  • The system stack

3
The Plan
  • Write main to decide what variables and
    procedures are needed
  • Write procedure stubs, test
  • Declare variables, test
  • Implement procedures one at a time, test

4
Write main to decide what variables and
procedures are needed
  • intro
  • getData a and b
  • calculate sum
  • showResults a, b, and sum

5
Write procedure stubs, test
  • Procedures
  • intro rules
  • getData a, b, prompt
  • calculate a, b, sum
  • showResults a, b, sum, outputID
  • Test for syntax

6
Declare variables,write procedure stubs, test
  • For now, its OK to use global variables
  • Variables
  • a, b, sum DWORD
  • rules, prompt, outputID BYTE (string)
  • Test for syntax

7
Implement procedures, test
  • Best to implement in this order
  • output
  • input
  • calculation
  • others
  • Test/debug/document each procedure as it is
    completed
  • Do the housekeeping

8
Caution
  • Avoid duplicate labels
  • Be sure to set required registers before calling
    library procedures.
  • Be aware of register changes in procedures

9
Local and Global Labels
  • Procedures should terminate by executing a ret
    statement
  • Bad style to jmp outside a procedure
  • Assembly language permits some bad coding styles
  • However full credit programs dont use bad style

10
MASM programming
  • Use Irvines examples
  • See the MASM Programming Guide
  • Check the Resources page, MASM
  • See in-class example demo2.asm

11
Data validation
  • Check input to verify that it satisfies the
    specifications and preconditions
  • One form of interactive data validation
  • Repeat user-input until a valid value arrives
  • Pseudo-code example
  • valid true
  • repeat
  • get value
  • if value is not in range
  • valid false
  • give error message
  • until valid

12
Passing parameters in registers
  • Passing by value
  • Send/return values in registers
  • Passing by reference
  • Send address (offset) in register
  • Procedure can use register indirect addressing
    mode
  • e.g., ebx
  • If a register parameter should not be changed
  • Save register contents before calling a procedure
  • Restore register contents after the procedure
    returns
  • Remember which registers (if any) should be
    changed by the procedure
  • e.g., new value returned in a register (ReadInt,
    etc.)

13
Saving registers
  • Methods
  • Move register contents to named memory locations,
    then restore after procedure returns.
  • Use pushad before the call, and use popad
    after the call
  • Use the USES operator

14
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

15
Method 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
  • PUSHA and POPA do the same for 16-bit registers
    AX, BX, CX, DX, SP, BP, SI, DI
  • Note its best to use 32-bit (DWORD) operands
  • More about this soon

16
Method 2 Save all registers on the system stack
  • Example (option 1 in main)
  • pushad save registers
  • mov eax, count set parameters
  • mov ebx, OFFSET val
  • call someProc
  • popad restore registers

17
Method 2 Save all registers on the system stack
  • Example (option 2 in the procedure)
  • calcSum PROC
  • pushad save registers
  • procedure body
  • popad restore registers
  • ret
  • calcSum ENDP

18
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

19
Method 3 USES Operator
  • Specifies the registers that will be preserved
  • Saves registers before any procedure statements
    are executed.
  • Restores registers before the procedure returns.
  • MASM adds save/restore code for specified
    registers
  • at the beginning and end of the procedure

20
Method 3 USES Operator
  • Example (in the procedure)
  • calcSum PROC USES esi ecx
  • procedure body
  • ret
  • calcSum ENDP
  • Notes
  • no commas
  • Be careful ! USES affects the system stack

21
Runtime Stack(System Stack)
  • The operating system maintains a stack
  • Implemented in memory
  • LIFO structure
  • Managed by the CPU, using two registers
  • SS (stack segment)
  • ESP (stack pointer)

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

23
Example PUSH
Stack Segment in Memory
Suppose that ecx contains 317 and esp contains
0200h. Note that esp is 25.
The next instruction is push ecx
Execute push ecx
esp 0200h esp 25
esp 01FCh esp 317
317
Note esp means contents of memory at the
address in esp
24
POP Operation
  • A pop operation
  • Copies value at ESP into a register or variable.
  • increments the stack pointer by 4 (or 2)
  • Actual increment depends on the size of the
    operand
  • Note its best to use 32-bit (DWORD , 4-byte)
    operands

25
Example POP
Stack Segment in Memory
Suppose that esp contains 01FCh. Note that esp
is 317.
The next instruction is pop eax
Execute pop eax
eax now contains 317
esp 01FCh esp 317
esp 0200h esp 25
Memory contents unchanged
26
PUSH and POP Instructions
  • PUSH syntax
  • PUSH r/m16
  • PUSH r/m32
  • PUSH imm32
  • POP syntax
  • POP r/m16
  • POP r/m32

27
Questions?
  • Keep moving on Program 2
  • Due Thursday
Write a Comment
User Comments (0)
About PowerShow.com