270 likes | 285 Views
Learn about bits, bytes, binary numbers, negative binary representation, hexadecimal arithmetic, ASCII codes, processor architectures, segment addressing, and register operations in computer engineering.
E N D
CS 301 Fall 2001 – Chapter 1 Slides by Prof. Hartman, following “IBM PC Assembly Language Programming” by Peter Abel
Bits, Bytes, and Words • A bit (binary digit) is 0 or 1 • byte = 8 bits • word = 2 bytes (16-bit) • doubleword = 2 words (32-bit) • quadword = 4 words (64-bit) • kilobyte (K) = 210 (1024) bytes • megabyte (M) = 220 (1048576) bytes
Binary numbers • Bit # 7 6 5 4 3 2 1 0 • Value: 128 64 32 16 8 4 2 1 • Example: 01000101 = 69
Negative Binary Numbers • One’s complement: flip the bits – 1 becomes 0, 0 becomes 1. • Two’s complement: take one’s complement, then add one. This is how the computer represents the negative. • Why? Because it works.Try -0, -1, and -13. (Check that adding -13 to 13 gives you 0) • Actually, it works because when you add anything to its one’s complement, you get all ones. Adding one to that gives you 0 (with a carry out).
Hexadecimal representation • Base 16: Digits are 0123456789ABCDEF • Write with a trailing h: 4A1Fh
Hex and Binary Arithmetic • Just as decimal. Add corresponding columns, if the result is higher than the base, then carry. • To subtract, either use two’s complement to find the negative and then add, or do as in base 10 (“borrow” instead of “carry”)
ASCII code • 01000001 – Is it 65 or A? • However you look at it. • Appendix B in the text
Pipelining and other speedups • Each more advanced processor does more at once (pipelining) • Branch prediction (because processors are faster than memory)
Execution Unit and Bus Interface Unit • Execution Unit: registers, control unit (CU), arithmetic and logic unit (ALU) – executes instructions • BIU: brings instructions to EU, manages segment registers and instruction queue.
Internal Memory • RAM – random access memory • ROM – read only memory • 808x processors that handle more than one byte at a time store the most significant byte in the high memory address and the low order byte in the low address.
Segments and Addressing • In real mode the three main segments are • Code Segment – instructions. Indexed by the CS register. • Data Segment – contains data, constants, and work areas. Indexed by the DS register. • Stack Segment – contains data and addresses to be temporarily saved, or for use by called subroutines. Indexed by the SS register.
Segment Boundaries • A segment register is 16 bits and contains the starting address of a segment – besides CS, DS, SS, others are ES (extra segment), and (on 80386 and later) FS and GS. • Because segments begin on “paragraph” boundaries (divisible by 10H) the designers didn’t store the smallest byte in the segment register, so if DS is 9FAH then the data segment begins at 9FA0H.
Segment Offsets • All memory locations within a segment are relative to the segment’s starting address. The distance in bytes is expressed as an offset. In real mode, an offset is two bytes. Consider a data segment beginning at 38E0H. The DS contains 38EH. If an instruction references a location with an offset of 32H bytes (0032H), the processor adds: 38E[0]H + 0032H = 3912H and looks in address 3912H.
Segment Registers • 8086/8088 – segment registers are 16-bit and operate in real mode. FFFF[0]H is 1 megabyte. • 80286 – Real mode is same as 8086. In protected mode, 24-bit addressing provides addresses up to FFFFF[0]H, or 16 megabytes by using segment register as index into memory that points at segment. • 80386/486/Pentium – Real mode again same as 8086. In protected mode, 48-bit addresses can address up to 4 gigabytes by using segment register as index into memory that points at segment.
Segment Registers 2 • CS register – contains starting address of the code segment. Along with IP, [CS:IP] gives the address of the instruction to be fetched for execution. • DS register – contains starting address of the data segment. Used with offsets included in instructions.
Segment Registers 3 • SS register – For the stack segment, used as [SS:SP]. Also (in subroutines) [SS:BP]. • ES register – Used (as [ES:DI]) by some string operations to handle memory addressing. • FS and GS registers – Additional extra segment registers introduced with 80386 processor.
Pointer Registers • The 32 bit pointer registers are EIP, ESP, and EBP. Their rightmost 16 bits are called IP, SP, and BP. • Pointer registers are offsets to be combined with segment registers. IP is used with CS, SP and BP are used with SS.
General Registers • The 32-bit registers are EAX, EBX, ECX, EDX. Their rightmost 16 bits are AX, BX, etc. Of those 16 bits, the leftmost byte (high bits) are AH, BH, etc. and the rightmost byte (low bits) are AL, BL, etc.
General Registers 2 • AX – the primary accumulator is used for I/O and most arithmetic (such as multiply, divide, and translate). Many instructions generate more efficient machine code if the reference AX rather than some other register. • BX – the base register. BX is the only general purpose register that can be used as an index to extend addressing. BX is also often used for computation and with SI or SI as a base register.
General Registers 3 • CX is called the count register. It is often used as a loop index, or a value to shift bits left or right. • DX is called the data register. Some I/O operations require it, and large multiplication and division operations use it along with AX as a pair.
Index Registers • ESI and EDI are the 32-bit index registers, their rightmost 16 bits are SI and DI respectively. • SI is the “source index register” associated with DS for some string operations • DI is the “destination index register” associated with ES for some string operations.
Flag Register • The 32-bit EFLAGS contains bits indicating the status of various activities. The rightmost 16 bits of EFLAGS is the FLAGS register, nine of its 16 bits indicate the status and results of processing. Many instructions change the status of these flags, other instructions test the flags to determine later action.
Flag bits 1 • OF (overflow) Indicates overflow of the leftmost bit during arithmetic. • DF (direction) Indicates left or right for moving or comparing string data. • IF (interrupt) Indicates whether external interrupts are being processed or ignored. • TF (trap) Permits operation of the processor in single step mode.
Flag bits 2 • SF (sign) Contains the resulting sign of an arithmetic operation (1=negative) • ZF (zero) Indicates when the result of arithmetic or a comparison is zero. (1=yes) • AF (auxiliary carry) Contains carry out of bit 3 into bit 4 for specialized arithmetic. • PF (parity) Indicates the number of 1 bits that result from an operation.
Flag bits 3 • CF (carry) Contains carry from leftmost bit following arithmetic, also contains last bit from a shift or rotate operation.
Hardware Interrupts • Some events cause the processor to stop what it is doing and act immediately on something else. Usually these are normal such as keyboard input, but sometimes they are critical, such as divide by zero. There are also software interrupts, such as a request by the program to display data on the screen. After an interrupt is processed the processor returns to what it was doing.