340 likes | 515 Views
CET 3510 - Microcomputer Systems Technology Lecture 6. Dr. José M. Reyes Álamo. Outline. Review: Statement Labels Unconditional Jumps Conditional Jumps. Statement Labels. A low level control structure usually transfers control from one point in your program to another.
E N D
CET 3510 - Microcomputer Systems TechnologyLecture 6 Dr. José M. Reyes Álamo
Outline • Review: • Statement Labels • Unconditional Jumps • Conditional Jumps
Statement Labels • A low level control structure usually transfers control from one point in your program to another. • Transfer destination is typically specified of using a statement label. • A statement label consists of a valid HLA identifier and a colon. • Don’t have to be declared before you use it • Syntax aLabel:
Operations with Labels • Transfer control to a label via a jump (goto) instruction • Call a label via the CALL instruction, • Take the address of a label • Use the address-of operator & • Use the command load effective address lea Syntax: lea( reg32, Memory_operand );
Unconditional Jump (JMP) • The jmp(jump) instruction unconditionally transfers control to another point in the program. • There are three forms of this instruction one direct jump, and two indirect in the following forms: • jmp label; • jmp( reg32 ); • jmp( mem32 );
Direct Jump • Direct jump specifies the target using a label • The label is usually on the same line as an executable instruction or appears on a line preceding an executable machine instruction. • The direct jump instruction is the most commonly used. • Equivalent to a GOTO statement • Syntax: … jmplaterInPgm; … laterInPgm: …
Register Indirect Jump • Transfers control to the instruction whose address is specified in the 32-bit general purpose register. • You must load the specified register with the address of some machine instruction prior to the execution of the JMP. • Syntax: … mov(&laterInPgm, ebx); jmp (ebx); … laterInPgm: …
Memory Indirect Jump • Memory indirect fetches a dword value from the specified memory location and transfers control to the instruction at the address specified by the contents of the memory location. • Similar to the register indirect JMP except the address appears in a memory location rather than in a register. • Syntax: << statements >> LabelPtr:dword := &stmtLabel; … jmp (LabelPtr); … stmtLabel: …
Conditional Jump • Unconditional jmpprovides transfer of control but does not allow you to make decisions. • Conditional jumps handle this task. • Conditional jumps are the basic tool for creating loops (i.e. while, for, repeat) and other conditionally executable statements (i.e. if, switch) • Syntax jcc label; cc indicated the type of test you are performing.
Conditional Jump Tests • The conditional jumps test one or more flags in the EFLAGS register to see if they match a particular pattern. • If the flags match, the control jumps to the target label. • If the match fails, the CPU ignores the conditional jump and execution continues with the next instruction. • Most of the time, conditional jumps follow the execution of a cmpinstruction as the cmp sets the EFLAGS register allowing tests for less than, greater than, equality, etc.
Bit String • A bit string is a contiguous sequence of one or more bits • A bit string does not have to start or end at any special point. • A bit string could start in bit seven of one byte in memory and continue through to bit six of the next byte. • A bit string could begin in bit 30 of EAX, and continue from bit zero through 17 of ECX. • In memory, bits must be physically contiguous • In registers, the application defines the continuation register.
Bit Set • A bit set is a collection of bits, not necessarily contiguous within some larger data structure. • e.g. 0..3, 7, 12, 24, and 31 from some double word object forms a set of bits. • Usually, we will limit bit sets to some reasonably sized container object. • Normally, container object sizes are about 32 or 64 bits • Bit strings are special cases of bit sets.
Bit Run • A bit run is a sequence of bits with all the same value. • A run of zeros is a bit string containing all zeros. • A run of ones is a bit string containing all ones. • The first set bit is the position in a bit string of the first bit containing a one • i.e., the first bit set to ‘1’. • The first clear bit is the position in a bit string of the first bit containing a zero • i.e., the first bit set to ‘0’. • The last set bit is the last bit position in a bit string containing a ‘1’; followed by a run of zeros. • The last clear bit is the last bit position in a bit string containing a ‘0’; followed by a run of ones.
Bit Offset • A bit offset is the number of bits from some boundary position (usually a byte boundary) to the specified bit. • Bits are numbered starting from zero at the boundary location. • If the offset is less than 32, then the bit offset is the same as the bit number in a byte, word, or double word value.
Bit Mask • A mask is a sequence of bits used to manipulate certain bits in another value. • e.g. mask 0000_1111_0000 used with AND, clears all the bits except bits 4-7 • mask 0000_1111_0000 used with OR, sets the bits 4-7 and preserves the bits 0-3 and 8-11
Bit Manipulation • Bit manipulation generally consists of six activities: • Setting bits • Clearing bits • Inverting bits • Testing and comparing bits • Extracting bits • Inserting bits • Bit manipulation instructions: • AND • OR • XOR • NOT • Shift • Rotate
Bit Manipulation: AND • The AND instruction provides the ability to strip away unwanted bits from a bit sequence and replace them with zeros. • e.g. suppose that a bit string consumes bits 12-24 of EAX. We can isolate it by setting all other bits to zero and using the and instruction: and( %1_1111_1111_1111_0000_0000_0000, eax ); • Most programs use AND to clear bits that are not part of the desired bit string.
Bit Manipulation: AND • To check if the bits 12-24 of EAX contain $12F3 you could use the following code: • Or a more sophisticated one: • Most of the time the bit string is aligned to position zero:
Bit Manipulation: OR • OR instruction allows you to mask unwanted bits. • OR instruction does not let you clear bits but it allows you to set bits. • The OR instruction is especially useful for inserting a bit set into some other bit string: • Clear all the bits surrounding your bit set in the source operand. • Clear all the bits in the destination operand where you wish to insert the bit set. • OR the bit set with the destination operand.
Bit Manipulation: OR • Assume that you have a value in bits 0-12 of EAX that you want to insert into bits 12-24 of EBX without affecting any of the other bits in EBX. • First you strip bits 13-31 EAX; then strip out bits 12-24 in EBX. Next, shift the bits in EAX to positions 12-24. Finally, OR the values of EAX and EBX
Bit Manipulation: OR • When working with bit masks, it is poor software engineering to use literal numeric constants. • You should always create symbolic constants instead to produce code that is easier to read and maintain.
NOT and XOR • NOT: Used to invert all the bits. Not very interesting. • XOR: gives you the ability to invert selected bits belonging to a bit set. • XOR lets you manipulate known data in just about any way imaginable. • Can accomplish the same results as with AND or OR, but with two advantages: • You are not limited to forcing the field to all zeros or all ones, you can have combinations; • You can manipulate other bits in the destination operand at the same time.
XOR Example • Suppose that you know that one field contains 1010 that you want to force to zero and another field contains 1000 that you wish to increment by one. • You cannot accomplish both operations with a single AND or OR instruction, but you can do this with a single XOR instruction • How? XOR the first field with %1010 and the second field with %0001. • Warning: This trick only works if you know the current value of a bit set within the destination operand.
How EFLAGS is affected • AND, OR, and XOR affect the EFLAGs register as follows: • These instructions always clear the carry and overflow flags. • These instructions set the sign flag if the result has a one in the H.O. bit; they clear it otherwise. • These instructions set/clear the zero flag depending on whether the result is zero. • These instructions set the parity flag if there are an even number of set bits in the L.O. byte of the destination operand, clear the parity flag if there are an odd number of one bits in the L.O. byte of the destination operand.
About the Parity Flag • Parity is a very simple error detection scheme. • The idea was to count the number of set bits in a character and include an extra bit in the transmission to indicate whether that character contained an even or odd number of set bits. • The receiving end of the transmission would also count the bits and verify that the extra “parity” bit indicated a successful transmission. • The purpose of the parity flag is to help compute the value of this extra bit. • The 80x86 AND, OR, and XOR instructions set the parity bit if the L.O. byte of their operand contains an even number of set bits. • Not used that much any more.
Bit Manipulation: TEST • After executing AND/OR/XOR, the zero flag result is one of the most important. • Many programs often reference this flag after the AND instruction. • Intel added the instruction TEST that logically AND two results and set the flags without affecting either instruction operand.
Bit Manipulation: TEST • There are three main uses of the zero flag after executing AND or TEST • Checking to see if a particular bit in an operand is set. • Checking to see if at least one of several bits in a bit set is one. • Checking to see if an operand is zero.
Carry Flag as a Bit Accumulator • Many of these instructions to manipulate bit will set the carry flag: • RCL, RCR • JC, JNC • SETC, SETNC • These are rarely used but are available if needed.