ece 353 introduction to microprocessor systems
DESCRIPTION
ECE 353 Introduction to Microprocessor Systems. Michael G. Morrow, P.E. Week 5. Flags Register Bit manipulation Logical Instructions Shift/Rotate Instructions Branching Conditional Unconditional Looping Structured Programming Stack Allocation and Operation. Objectives. FLAGS Register. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/1.jpg)
ECE 353Introduction to Microprocessor Systems
Michael G. Morrow, P.E.
Week 5
![Page 2: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/2.jpg)
Flags RegisterBit manipulation Logical Instructions Shift/Rotate Instructions
Branching Conditional Unconditional
LoopingStructured ProgrammingStack Allocation and Operation
Objectives
![Page 3: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/3.jpg)
FLAGS Registeraka the Processor Status Word (PSW)A ‘flag’ is generally a marker used to indicate or record some condition.PSW contains 6 status flags … AF, CF, OF, PF, SF, ZF
… and 3 control flags DF, IF, TF
80C188EB Processor Status Word (PSW)
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
-- -- -- --OF
DF
IF TF SF ZF --AF
-- PF -- CF
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
![Page 4: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/4.jpg)
PSW Status FlagsThe status flags reflect the result of the previous logical or arithmetic operation. AF – indicates a carry or borrow between the
high and low nibbles of the accumulator, used for BCD.
CF – indicates a carry from, or a borrow to, the MSb of an arithmetic result Can also modify directly with CLC / CMC / STC to use as
a Boolean status bit. OF – an arithmetic overflow has occurred PF – set if the operation resulted in even parity SF – set if the result is negative (i.e. MSb = 1) ZF – set if the result is zero
![Page 5: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/5.jpg)
Control FlagsThe control flags control certain aspects of the processor’s execution DF – direction flag
Determines the direction of pointer modifications in string operations. If DF=0, then increment pointers, otherwise decrement.
IF – interrupt enable flag If set, the processor can recognize
maskable interrupts. TF – trap flag
If set, the processor will execute in single-step mode.
![Page 6: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/6.jpg)
Logical InstructionsLogical instructions operate bit-wise.NOT does not affect flags.
Mnemonic
Operands
Function O S Z A P C
NOT dst Logical complement
- - - - - -
AND dst,src
Logical AND 0 ? 0
OR dst,src
Logical OR 0 ? 0
XOR dst,src
Logical exclusive OR
0 ? 0
TEST dst,src
Non-destructive AND
0 ? 0
![Page 7: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/7.jpg)
Bit ManipulationClearing bit(s) - AND Set desired bits to 0 in mask All other bits in mask to 1
Setting bit(s) - OR Set desired bits to 1 in mask All other bits in mask to 0
Toggling bit(s) - XOR Set desired bits to 1 in mask All other bits in mask to 0
Read-modify-write issues
![Page 8: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/8.jpg)
Shift InstructionsArithmetic versus logical shiftsShift count source 1 CL Immediate byte (new to 80186 instruction set)
Mnemonic
Operands
Function O S Z A P C
SHL dst, cnt
Shift logical left * ?
SAL dst, cnt
Shift arithmetic left * ?
SHR dst, cnt
Shift logical right * ?
SAR dst, cnt
Shift arithmetic right
* ?
![Page 9: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/9.jpg)
Rotate InstructionsRotate count sourcesUsing rotate instruction to swap nibblesExecution time dependent on count
Mnemonic
Operands
Function O S Z A P C
ROL dst, cnt
Rotate left * - - - -
ROR dst, cnt
Rotate right * - - - -
RCL dst, cnt
Rotate through carry left
* - - - -
RCR dst, cnt
Rotate through carry right
* - - - -
![Page 10: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/10.jpg)
Unconditional JumpsRedirect program flow by loading a new IP (and possibly a new CS) value.Syntax: jmp target Label attributes Target by direct addressing Target by indirect addressing
Jump tables Be sure index is bounds-checked! FAR versus NEAR jump tables
![Page 11: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/11.jpg)
Conditional JumpsAllow program flow to change in response to conditions.Action is based on current state of flags.Syntax: j<X> short-label <X> is mnemonic for condition to test. All conditional jumps are short.
Can use double jumps if target out of range.
Prior instruction must be used to set flags Examples
![Page 12: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/12.jpg)
LoopingCan use backwards conditional jumps to create a loop that can be terminated - By a condition After a predetermined number of iterations Or a combination of both
Mnemonic
Operands
Function O S Z A P C
INC dst Increment -
DEC dst Decrement -
CMP dst, src
Compare (dst – src, nondestructive)
-
![Page 13: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/13.jpg)
LoopingCan use the LOOP instructionsCX is loop control variable GPP: Don’t modify CX in loop
Mnemonic Operands
Function O S Z A P C
LOOP shrt_lbl
DEC CXJNZ
- - - - - -
LOOPELOOPZ
shrt_lbl
Loop while CX <> 0 and ZF = 1 (last operation was zero)
- - - - - -
LOOPNELOOPNZ
shrt_lbl
Loop while CX <> 0 and ZF = 0 (last operation was not zero)
- - - - - -
![Page 14: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/14.jpg)
Implementing Structured Programming Constructs
Structured programming basics One entry point, one exit point per
procedure (subroutine) Based on three basic control structures
Repetition Selection
If, If/Else, Switch/Case
Repetition While Do-While
Flowchart Basics
![Page 15: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/15.jpg)
Repeated String Instructions
String instructions become very useful when used with the REP prefix REP, REPE/REPZ, REPNE/REPNZ
Pointer modification based on DF (CLD, STD).CX is always loop variable for repeated string instructionsCMPS order of evaluation is reversed from CMP!
Mnemonic
Operands
Function O S Z A P C
SCAS dst_s Scan string(AL – ES:DI)
CMPS dst_s Compare string(DS:SI – ES:DI)
![Page 16: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/16.jpg)
RecordsProvides a syntax for creating and accessing bit-encoded data structures.Syntax name RECORD field_name:exp[=init_val][,…]
Operations MASK
Creates a mask for the bit-field identified Shift
Using the bit-field name is interpreted as a right shift count to move that field to the LSB
WIDTH Returns number of bits in a record or field
![Page 17: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/17.jpg)
Stack ImplementationStack is a LIFO data structure.Who uses the stack? Subroutine return addresses Passed parameters Temporary memory allocation
Two basic operations PUSH POP
Hardware stacks vs. memory stacks Stack pointer
A dedicated register to use exclusively to access the stack
![Page 18: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/18.jpg)
80C188EB Stack Operation
Stack is defined by SS:SPAllocating stack spaceStack operations All stack operations are 16-bit
transfers PUSH / PUSHA / PUSHF POP / POPA / POPF CALL / RETURN ENTER / LEAVE
Example
![Page 19: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/19.jpg)
Wrapping UpHomework #3 due Friday, October 12th
Exam 1 will be held on Thursday, October 18, 2001 from 7:15 to 8:45 PM in 132 Noland
![Page 20: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/20.jpg)
ExerciseWrite a code fragment that implements the C function strchr, which finds a given character in an ASCIIZ string.strchr scans the string in the forward direction, looking for the specified character and finds the first occurrence of the character in the string. The null-terminator is considered to be part of the string.
Assume the following conditions: AL - character to search forDS:DI - address of null-terminated string to search
(string must not start at offset of zero!)If found, set AX = equal offset of first occurrence, otherwise set AX = 0.
![Page 21: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/21.jpg)
Mnemonic Jump if condition Flags for conditionJA/JNBE Above / not below or equal (CF OR ZF) = 0
JAE/JNB Above or equal / not below CF = 0
JB/JNAE Below / not above or equal CF = 1
JBE/JNA Below or equal / not above (CF OR ZF) = 1
JC Carry CF = 1
JCXZ Jump if CX equal 0 CX = 0 (uses register)
JE/JZ Equal / zero ZF = 1
JB/JLNE Below / not less nor equal ((SF XOR OF) OR ZF) = 0
JGE/JNL Greater or equal / not less (SF XOR OF) = 0
JL/JNGE Less / not greater nor equal (SF XOR OF) = 1
JNC No carry CF = 0
JNE/JNZ Not equal / not zero ZF = 0
JNO Not overflow OF = 0
JNP/JPO Not parity / parity odd PF = 0
JNS Not sign (positive) SF = 0
JO Overflow OF = 1
JP/JPE Parity / parity even PF = 1
JS Sign (negative) SF = 1
![Page 22: ECE 353 Introduction to Microprocessor Systems](https://reader030.vdocuments.net/reader030/viewer/2022033105/568143e7550346895db06dae/html5/thumbnails/22.jpg)
Read-Modify-Write Issues
Remove bubbles for memory-
mapped I/O.