ECE 353 Introduction to Microprocessor Systems Week 5

  • Slides: 24
Download presentation
ECE 353 Introduction to Microprocessor Systems Week 5 Michael J. Schulte

ECE 353 Introduction to Microprocessor Systems Week 5 Michael J. Schulte

Topics Flags Register Bit manipulation n n Logical Instructions Shift/Rotate Instructions Branching n n

Topics Flags Register Bit manipulation n n Logical Instructions Shift/Rotate Instructions Branching n n Conditional Unconditional Looping Structured Programming Stack Allocation and Operation

FLAGS Register aka the Processor Status Word (PSW) A ‘flag’ is generally a marker

FLAGS Register aka the Processor Status Word (PSW) A ‘flag’ is generally a marker used to indicate or record some condition. PSW contains 6 status flags … n AF, CF, OF, PF, SF, ZF … and 3 control flags n DF, IF, TF 80 C 188 EB 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 0 0 0

PSW Status Flags The status flags reflect the result of the previous logical or

PSW Status Flags The status flags reflect the result of the previous logical or arithmetic operation. n n 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 w Can also modify directly with CLC / CMC / STC to use as a Boolean status bit. n n 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

Control Flags The control flags control certain aspects of the processor’s execution n DF

Control Flags The control flags control certain aspects of the processor’s execution n DF – direction flag w Determines the direction of pointer modifications in string operations. If DF=0, then increment pointers, otherwise decrement. n IF – interrupt enable flag w If set, the processor can recognize maskable interrupts. n TF – trap flag w If set, the processor will execute in single-step mode.

Logical Instructions Logical instructions operate bit-wise. NOT does not affect flags. Mnemonic Operands Function

Logical Instructions Logical 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

Questions? What logical operations would be necessary to n n n Check the state

Questions? What logical operations would be necessary to n n n Check the state of a single bit? Check if multiple bits are 1? Check if certain bits have desired states? Set a bit or bits? Clear a bit or bits? Toggle a bit or bits?

Bit Manipulation Clearing bit(s) - AND n n Set desired bits to 0 in

Bit Manipulation Clearing bit(s) - AND n n Set desired bits to 0 in mask All other bits in mask to 1 Setting bit(s) - OR n n Set desired bits to 1 in mask All other bits in mask to 0 Toggling bit(s) - XOR n n Set desired bits to 1 in mask All other bits in mask to 0 Read-modify-write issues

Shift Instructions Arithmetic versus logical shifts Shift count source n n n 1 CL

Shift Instructions Arithmetic versus logical shifts Shift count source n n n 1 CL Immediate byte (new to 80186 instruction set) O S Z A P C * ? Mnemonic Operands Function SHL dst, cnt Shift logical left SAL dst, cnt Shift arithmetic left SHR dst, cnt Shift logical right * ? SAR dst, cnt Shift arithmetic right * ?

Rotate Instructions Rotate count sources same as shifts Using rotate instruction to swap nibbles

Rotate Instructions Rotate count sources same as shifts Using rotate instruction to swap nibbles Execution time dependent on count O S Z A P C Mnemonic Operands Function ROL dst, cnt Rotate left ROR dst, cnt Rotate right * - - RCL dst, cnt Rotate through carry left RCR dst, cnt Rotate through carry right * - - - - * - -

Unconditional Jumps Redirect program flow by loading a new IP (and possibly a new

Unconditional Jumps Redirect program flow by loading a new IP (and possibly a new CS) value. Syntax: jmp target n n n Label attributes Target by direct addressing Target by indirect addressing Jump tables n n Be sure index is bounds-checked! FAR versus NEAR jump tables

Conditional Jumps Allow program flow to change in response to conditions. Action is based

Conditional Jumps Allow program flow to change in response to conditions. Action is based on current state of flags. Syntax: j<X> short-label n n <X> is mnemonic for condition to test. All conditional jumps are short. w Can use double jumps if target out of range. Prior instruction must be used to set flags n Examples

Looping Can use backwards conditional jumps to create a loop that can be terminated

Looping Can use backwards conditional jumps to create a loop that can be terminated n n n 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) -

Looping Can use the LOOP instructions CX is loop control variable n Warning: modifying

Looping Can use the LOOP instructions CX is loop control variable n Warning: modifying CX in the loop can be disastrous Mnemonic Operands Function O S Z A P C LOOP shrt_lbl DEC CX JNZ - - - Loop while CX <> 0 and ZF = 1 (last operation was zero) - - - Loop while CX <> 0 and ZF = 0 (last operation was not zero) - - - LOOPE shrt_lbl LOOPZ LOOPNE shrt_lbl LOOPNZ

Implementing Structured Programming Constructs Structured programming basics n n One entry point, one exit

Implementing Structured Programming Constructs Structured programming basics n n One entry point, one exit point per procedure (subroutine) Based on three basic control structures w Sequence w Selection n If, If/Else, Switch/Case w Repetition n n While Do-While Flowchart Basics

Repeated String Instructions String instructions become very useful when used with the REP prefix

Repeated String Instructions String instructions become very useful when used with the REP prefix n REP, REPE/REPZ, REPNE/REPNZ Pointer modification based on DF (CLD, STD). CX is always loop variable for repeated string instructions CMPS 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)

Records Provides a syntax for creating and accessing bit -encoded data structures. Syntax n

Records Provides a syntax for creating and accessing bit -encoded data structures. Syntax n name RECORD field_name: exp[=init_val][, …] Operations n MASK w Creates a mask for the bit-field identified n Shift w Using the bit-field name is interpreted as a right shift count to move that field to the LSB n WIDTH w Returns number of bits in a record or field

Stack Implementation Stack is a LIFO data structure. What uses the stack? n n

Stack Implementation Stack is a LIFO data structure. What uses the stack? n n n Subroutine return addresses Passed parameters Temporary memory allocation Two basic stack operations n n PUSH POP Hardware stacks vs. memory stacks n n Hardware stack Memory stack pointer w Stack pointer

80 C 188 EB Stack Operation Stack is defined by SS: SP Allocating stack

80 C 188 EB Stack Operation Stack is defined by SS: SP Allocating stack space Stack operations n n n All stack operations are 16 -bit transfers PUSH / PUSHA / PUSHF POP / POPA / POPF CALL / RETURN ENTER / LEAVE Example

Wrapping Up Homework #3 is due Friday, March 4, 2005. Exam #1 will be

Wrapping Up Homework #3 is due Friday, March 4, 2005. Exam #1 will be held on Wednesday, February 13 th, 2005 from 7: 15 to 8: 45 PM in room 1227 Engineering Hall. n n One 8. 5 x 11 sheet with original, handwritten notes. Instruction set guide will be provided. For next time week read Ch. 8. 3 -8. 5, 8. 8, 9.

Exercise Write a code fragment that implements the C function strchr, which finds a

Exercise Write 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 initial conditions: AL - character to search for DS: 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.

Mnemonic Jump if condition Flags for condition JA/JNBE Above / not below or equal

Mnemonic Jump if condition Flags for condition JA/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

Read-Modify-Write Issues

Read-Modify-Write Issues

PIC 16 F 84 Hardware Stack

PIC 16 F 84 Hardware Stack