Title: Tuesday, February 5
1Tuesday, 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)
2Todays topics
- Example development with procedures
- Data validation
- More procedures, parameter passing
- The system stack
3The 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
4Write main to decide what variables and
procedures are needed
- intro
- getData a and b
- calculate sum
- showResults a, b, and sum
5Write procedure stubs, test
- Procedures
- intro rules
- getData a, b, prompt
- calculate a, b, sum
- showResults a, b, sum, outputID
- Test for syntax
6Declare 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
7Implement procedures, test
- Best to implement in this order
- output
- input
- calculation
- others
- Test/debug/document each procedure as it is
completed - Do the housekeeping
8Caution
- Avoid duplicate labels
- Be sure to set required registers before calling
library procedures. - Be aware of register changes in procedures
9Local 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
10MASM programming
- Use Irvines examples
- See the MASM Programming Guide
- Check the Resources page, MASM
- See in-class example demo2.asm
11Data 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
12Passing 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.)
13Saving 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
14Method 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
-
15Method 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
16Method 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
17Method 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
18Method 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
19Method 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
20Method 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
21Runtime 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)
22PUSH 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
23Example 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
24POP 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
25Example 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
26PUSH and POP Instructions
- PUSH syntax
- PUSH r/m16
- PUSH r/m32
- PUSH imm32
- POP syntax
- POP r/m16
- POP r/m32
27Questions?
- Keep moving on Program 2
- Due Thursday