UPLOAD

    3.8K

    EEEN311_Fall12_W3

    Published: July 18, 2018

    EEEN 311 - LOGIC CIRCUITS AND MICROPROCESSORS WEEK 3

    Comments

    EEEN311_Fall12_W3

    • 1. EEEN 311 Logic Circuits & Microprocessors EEEN 311 Logic Circuits & Microprocessors The x86 Microprocessor [Week 2]
    • 2. OBJECTIVES §State the purpose of the code segment, data segment, stack segment, and extra segment. §Explain the difference between a logical address and a physical address. §Describe the "little endian" storage convention of x86 microprocessors. §State the purpose of the stack. §Explain the function of PUSH and POP instructions. §List the bits of the flag register and briefly state the purpose of each bit. §Demonstrate the effect of ADD instructions on the flag register. §List the addressing modes of the 8086 and recognize examples of each mode. §Know how to use flowcharts and pseudocode in program development. OBJECTIVES
    • 3. INTRODUCTION TO PROGRAM SEGMENTS code segment logical/physical address INTRODUCTION TO PROGRAM SEGMENTS code segment logical/physical address §In the next code segment, CS and IP hold the logical address of the instructions to be executed. oThe following Assembly language instructions have been assembled (translated into machine code) and stored in memory. oThe three columns show the logical address of CS:IP, the machine code stored at that address, and the corresponding Assembly language code. oThe physical address is put on the address bus by the CPU to be decoded by the memory circuitry.
    • 4. INTRODUCTION TO PROGRAM SEGMENTS code segment logical/physical address INTRODUCTION TO PROGRAM SEGMENTS code segment logical/physical address Instruction "MOV AL,57" has a machine code of B057. B0 is the opcode and 57 is the operand.
    • 5. INTRODUCTION TO PROGRAM SEGMENTS code segment logical/physical address INTRODUCTION TO PROGRAM SEGMENTS code segment logical/physical address Instruction "MOV AL,57" has a machine code of B057. B0 is the opcode and 57 is the operand. The byte at address 1132:0100 contains B0, the opcode for moving a value into register AL. Address 1132:0101 contains the operand to be moved to AL.
    • 6. INTRODUCTION TO PROGRAM SEGMENTS data segment  INTRODUCTION TO PROGRAM SEGMENTS data segment §Assume a program to add 5 bytes of data, such as 25H, 12H, 15H, 1FH, and 2BH. oOne way to add them is as follows: –In the program above, the data & code are mixed together in the instructions. •If the data changes, the code must be searched for every place it is included, and the data retyped •From this arose the idea of an area of memory strictly for data
    • 7. INTRODUCTION TO PROGRAM SEGMENTS data segment  INTRODUCTION TO PROGRAM SEGMENTS data segment §In x86 microprocessors, the area of memory set aside for data is called the data segment. oThe data segment uses register DS and an offset value. oDEBUG assumes that all numbers are in hex. •No "H" suffix is required. oMASM assumes that they are in decimal. •The "H" must be included for hex data. §The next program demonstrates how data can be stored in the data segment and the program rewritten so that it can be used for any set of data.
    • 8. INTRODUCTION TO PROGRAM SEGMENTS data segment  INTRODUCTION TO PROGRAM SEGMENTS data segment §Assume data segment offset begins at 200H. oThe data is placed in memory locations: –The program can be rewritten as follows:
    • 9. INTRODUCTION TO PROGRAM SEGMENTS data segment  INTRODUCTION TO PROGRAM SEGMENTS data segment §The offset address is enclosed in brackets, which indicate that the operand represents the address of the data and not the data itself. –If the brackets were not included, as in "MOV AL,0200", the CPU would attempt to move 200 into AL instead of the contents of offset address 200. decimal. •This program will run with any set of data. •Changing the data has no effect on the code.
    • 10. INTRODUCTION TO PROGRAM SEGMENTS data segment  INTRODUCTION TO PROGRAM SEGMENTS data segment §If the data had to be stored at a different offset address the program would have to be rewritten oA way to solve this problem is to use a register to hold the offset address, and before each ADD, increment the register to access the next byte. §8088/86 allows only the use of registers BX, SI, and DI as offset registers for the data segment oThe term pointer is often used for a register holding an offset address.
    • 11. INTRODUCTION TO PROGRAM SEGMENTS data segment  INTRODUCTION TO PROGRAM SEGMENTS data segment §In the following example, BX is used as a pointer: •The INC instruction adds 1 to (increments) its operand. –"INC BX" achieves the same result as "ADD BX,1“ –If the offset address where data is located is changed, only one instruction will need to be modified.
    • 12.         INTRODUCTION TO PROGRAM SEGMENTS          data segment logical/physical address INTRODUCTION TO PROGRAM SEGMENTS data segment logical/physical address §The physical address for data is calculated using the same rules as for the code segment. oThe physical address of data is calculated by shifting DS left one hex digit and adding the offset value, as shown in coming examples
    • 13. INTRODUCTION TO PROGRAM SEGMENTS          data segment logical/physical address INTRODUCTION TO PROGRAM SEGMENTS data segment logical/physical address
    • 14. INTRODUCTION TO PROGRAM SEGMENTS          data segment logical/physical address INTRODUCTION TO PROGRAM SEGMENTS data segment logical/physical address
    • 15. INTRODUCTION TO PROGRAM SEGMENTS little endian convention INTRODUCTION TO PROGRAM SEGMENTS little endian convention §Previous examples used 8-bit or 1-byte data. oWhat happens when 16-bit data is used? §The low byte goes to the low memory location and the high byte goes to the high memory address. –Memory location DS:1500 contains F3H. –Memory location DS:1501 contains 35H. •(DS:1500 = F3 DS:1501 = 35) –This convention is called little endian vs big endian. •From a Gulliver’s Travels story about how an egg should be opened—from the little end, or the big end.
    • 16. INTRODUCTION TO PROGRAM SEGMENTSlittle endian convention INTRODUCTION TO PROGRAM SEGMENTS little endian convention §In the big endian method, the high byte goes to the low address. oIn the little endian method, the high byte goes to the high address and the low byte to the low address. §All Intel microprocessors and many microcontrollers use the little endian convention. oFreescale (formerly Motorola) microprocessors, along with some other microcontrollers, use big endian.
    • 17. INTRODUCTION TO PROGRAM SEGMENTS extra segment (ES)  INTRODUCTION TO PROGRAM SEGMENTS extra segment (ES) §ES is a segment register used as an extra data segment. oIn many normal programs this segment is not used. oUse is essential for string operations.
    • 18. INTRODUCTION TO PROGRAM SEGMENTSmemory map of the IBM PC INTRODUCTION TO PROGRAM SEGMENTS memory map of the IBM PC §The 20-bit address of 8088/86 allows 1mb (1024K bytes) of memory space with the address range 00000–FFFFF. oDuring the design phase of the first IBM PC, engineers had to decide on the allocation of the 1-megabyte memory space to various sections of the PC. •This memory allocation is called a memory map. Figure 1-3 Memory Allocation in the PC
    • 19. INTRODUCTION TO PROGRAM SEGMENTSmemory map of the IBM PC INTRODUCTION TO PROGRAM SEGMENTS memory map of the IBM PC §Of this 1 megabyte, 640K bytes from addresses 00000–9FFFFH were set aside for RAM •128K bytes A0000H– BFFFFH were allocated for video memory •The remaining 256K bytes from C0000H–FFFFFH were set aside for ROM Figure 1-3 Memory Allocation in the PC
    • 20. INTRODUCTION TO PROGRAM SEGMENTS more about RAM INTRODUCTION TO PROGRAM SEGMENTS more about RAM §In the early 80s, most PCs came with 64K to 256K bytes of RAM, more than adequate at the time oUsers had to buy memory to expand up to 640K. §Managing RAM is left to Windows because... oThe amount of memory used by Windows varies. oDifferent computers have different amounts of RAM. oMemory needs of application packages vary. §For this reason, we do not assign any values for the CS, DS, and SS registers. oSuch an assignment means specifying an exact physical address in the range 00000–9FFFFH, and this is beyond the knowledge of the user.
    • 21. INTRODUCTION TO PROGRAM SEGMENTSvideo RAM INTRODUCTION TO PROGRAM SEGMENTS video RAM §From A0000H to BFFFFH is set aside for video oThe amount used and the location vary depending on the video board installed on the PC
    • 22. 1.INTRODUCTION TO PROGRAM SEGMENTS more about ROM 1.INTRODUCTION TO PROGRAM SEGMENTS more about ROM §C0000H to FFFFFH is set aside for ROM. oNot all the memory in this range is used by the PC's ROM. §64K bytes from location F0000H–FFFFFH are used by BIOS (basic input/output system) ROM. oSome of the remaining space is used by various adapter cards (such as the network card), and the rest is free. §The 640K bytes from 00000 to 9FFFFH is referred to as conventional memory. oThe 384K bytes from A0000H to FFFFFH are called the UMB (upper memory block).
    • 23. INTRODUCTION TO PROGRAM SEGMENTS function of BIOS ROM INTRODUCTION TO PROGRAM SEGMENTS function of BIOS ROM §There must be some permanent (nonvolatile) memory to hold the programs telling the CPU what to do when the power is turned on oThis collection of programs is referred to as BIOS. §BIOS stands for basic input-output system. oIt contains programs to test RAM and other components connected to the CPU. oIt also contains programs that allow Windows to communicate with peripheral devices. oThe BIOS tests devices connected to the PC when the computer is turned on and to report any errors.
    • 24. THE STACK what is a stack? why is it needed? THE STACK what is a stack? why is it needed? §The stack is a section of read/write memory (RAM) used by the CPU to store information temporarily. oThe CPU needs this storage area since there are only a limited number of registers. •There must be some place for the CPU to store information safely and temporarily. §The main disadvantage of the stack is access time. oSince the stack is in RAM, it takes much longer to access compared to the access time of registers. §Some very powerful (expensive) computers do not have a stack. oThe CPU has a large number of registers to work with.
    • 25. THE STACK how stacks are accessed THE STACK how stacks are accessed §The stack is a section of RAM, so there must be registers inside the CPU to point to it. oThe SS (stack segment) register. oThe SP (stack pointer) register. •These registers must be loaded before any instructions accessing the stack are used. §Every register inside the x86 can be stored in the stack, and brought back into the CPU from the stack memory, except segment registers and SP. oStoring a CPU register in the stack is called a push. oLoading the contents of the stack into the CPU register is called a pop.
    • 26. THE STACK how stacks are accessed THE STACK how stacks are accessed §The x86 stack pointer register (SP) points at the current memory location used as the top of the stack. oAs data is pushed onto the stack it is decremented. oAs data is popped off the stack into the CPU, it is incremented. §When an instruction pushes or pops a general-purpose register, it must be the entire 16-bit register. oOne must code "PUSH AX". •There are no instructions such as "PUSH AL" or "PUSH AH".
    • 27. THE STACK how stacks are accessed THE STACK how stacks are accessed §The SP is decremented after the push is to make sure the stack is growing downward from upper addresses to lower addresses. oThe opposite of the IP. (instruction pointer) §To ensure the code section & stack section of the program never write over each other, they are located at opposite ends of the RAM set aside for the program. oThey grow toward each other but must not meet. •If they meet, the program will crash.
    • 28. THE STACK pushing onto the stack THE STACK pushing onto the stack §As each PUSH is executed, the register contents are saved on the stack and SP is decremented by 2.
    • 29. THE STACK pushing onto the stack THE STACK pushing onto the stack §For every byte of data saved on the stack, SP is decremented once. Since the push is saving the contents of a 16-bit register, it decrements twice.
    • 30. THE STACK pushing onto the stack THE STACK pushing onto the stack §In the x86, the lower byte is always stored in the memory location with the lower address. 24H, the content of AH, is saved in the memory location with the address 1235. AL is stored in location 1234.
    • 31. THE STACK popping the stack THE STACK popping the stack §With every pop, the top 2 bytes of the stack are copied to the x86 CPU register specified by the instruction & the stack pointer is incremented twice. While the data actually remains in memory, it is not accessible, since the stack pointer, SP is beyond that point.
    • 32. THE STACK logical vs physical stack address THE STACK logical vs physical stack address §The exact physical location of the stack depends on the value of the stack segment (SS) register and SP, the stack pointer. oTo compute physical addresses for the stack, shift left SS, then add offset SP, the stack pointer register. –Windows assigns values for the SP and SS.
    • 33. THE STACKa few more words about x86 segments THE STACK a few more words about x86 segments §Can a single physical address belong to many different logical addresses? oObserve the physical address value of 15020H. •Many possible logical addresses represent this single physical address: –An illustration of the dynamic behavior of the segment and offset concept in the 8086 CPU.
    • 34. THE STACKa few more words about x86 segments THE STACK a few more words about x86 segments §When adding the offset to the shifted segment register results in an address beyond the maximum allowed range of FFFFFH, wrap-around will occur.
    • 35. THE STACKoverlapping THE STACK overlapping §In calculating the physical address, it is possible that two segments can overlap.
    • 36. FLAG REGISTER FLAG REGISTER §Many Assembly language instructions alter flag register bits & some instructions function differently based on the information in the flag register. §The flag register is a 16-bit register sometimes referred to as the status register. oAlthough 16 bits wide, only some of the bits are used. •The rest are either undefined or reserved by Intel.
    • 37. 1.6        FLAG REGISTER 1.6 FLAG REGISTER §Six flags, called conditional flags, indicate some condition resulting after an instruction executes. –These six are CF, PF, AF, ZF, SF, and OF. –The remaining three, often called control flags, control the operation of instructions before they are executed.
    • 38. FLAG REGISTER bits of the flag register FLAG REGISTER bits of the flag register §Flag register bits used in x86 Assembly language programming, with a brief explanation each: oCF (Carry Flag) - Set when there is a carry out, from d7 after an 8- bit operation, or d15 after a 16-bit operation. •Used to detect errors in unsigned arithmetic operations. oPF (Parity Flag) - After certain operations, the parity of the result's low-order byte is checked. •If the byte has an even number of 1s, the parity flag is set to 1; otherwise, it is cleared. oAF (Auxiliary Carry Flag) - If there is a carry from d3 to d4 of an operation, this bit is set; otherwise, it is cleared. •Used by instructions that perform BCD (binary coded decimal) arithmetic.
    • 39. FLAG REGISTER bits of the flag register FLAG REGISTER bits of the flag register §Flag register bits used in x86 Assembly language programming, with a brief explanation each: –ZF (Zero Flag) - Set to 1 if the result of an arithmetic or logical operation is zero; otherwise, it is cleared. –SF (Sign Flag) - Binary representation of signed numbers uses the most significant bit as the sign bit. •After arithmetic or logic operations, the status of this sign bit is copied into the SF, indicating the sign of the result. –TF (Trap Flag) - When this flag is set it allows the program to single-step, meaning to execute one instruction at a time. •Single-stepping is used for debugging purposes.
    • 40. FLAG REGISTER bits of the flag register FLAG REGISTER bits of the flag register §Flag register bits used in x86 Assembly language programming, with a brief explanation each: –IF (Interrupt Enable Flag) - This bit is set or cleared to enable/disable only external maskable interrupt requests. –DF (Direction Flag) - Used to control the direction of string operations. –OF (Overflow Flag) - Set when the result of a signed number operation is too large, causing the high-order bit to overflow into the sign bit. •Used only to detect errors in signed arithmetic operations.
    • 41. FLAG REGISTER flag register and ADD instruction FLAG REGISTER flag register and ADD instruction §Flag bits affected by the ADD instruction: oCF (carry flag); PF (parity flag); AF (auxiliary carry flag). oZF (zero flag); SF (sign flag); OF (overflow flag).
    • 42. FLAG REGISTER flag register and ADD instruction FLAG REGISTER flag register and ADD instruction §Flag bits affected by the ADD instruction: oCF (carry flag); PF (parity flag); AF (auxiliary carry flag). oZF (zero flag); SF (sign flag); OF (overflow flag).
    • 43. FLAG REGISTER flag register and ADD instruction §It is important to note differences between 8- and 16-bit operations in terms of impact on the flag bits. oThe parity bit only counts the lower 8 bits of the result and is set accordingly. FLAG REGISTER flag register and ADD instruction
    • 44. FLAG REGISTER flag register and ADD instruction §The carry flag is set if there is a carry beyond bit d15 instead of bit d7. oSince the result of the entire 16-bit operation is zero (meaning the contents of BX), ZF is set to high. FLAG REGISTER flag register and ADD instruction
    • 45. FLAG REGISTER flag register and ADD instruction §Instructions such as data transfers (MOV) affect no flags. FLAG REGISTER flag register and ADD instruction
    • 46. FLAG REGISTER use of the zero flag for looping FLAG REGISTER use of the zero flag for looping §A widely used application of the flag register is the use of the zero flag to implement program loops. oA loop is a set of instructions repeated a number of times.
    • 47. FLAG REGISTER use of the zero flag for looping §As an example, to add 5 bytes of data, a counter can be used to keep track of how many times the loop needs to be repeated. –Each time the addition is performed the counter is decremented and the zero flag is checked. •When the counter becomes zero, the zero flag is set (ZF = 1) and the loop is stopped. FLAG REGISTER use of the zero flag for looping
    • 48. FLAG REGISTER use of the zero flag for looping §Register CX is used to hold the counter. o BX is the offset pointer. •(SI or DI could have been used instead) FLAG REGISTER use of the zero flag for looping
    • 49. FLAG REGISTER use of the zero flag for looping § AL is initialized before the start of the loop oIn each iteration, ZF is checked by the JNZ instruction •JNZ stands for "Jump Not Zero“, meaning that if ZF = 0, jump to a new address. •If ZF = 1, the jump is not performed, and the instruction below the jump will be executed. FLAG REGISTER use of the zero flag for looping
    • 50. FLAG REGISTER use of the zero flag for looping § JNZ instruction must come immediately after the instruction that decrements CX. oJNZ needs to check the effect of "DEC CX" on ZF. •If any instruction were placed between them, that instruction might affect the zero flag. FLAG REGISTER use of the zero flag for looping
    • 51. x86 ADDRESSING MODES x86 ADDRESSING MODES §The CPU can access operands (data) in various ways, called addressing modes. oThe number of addressing modes is determined when the microprocessor is designed & cannot be changed §The x86 provides seven distinct addressing modes: –1 - Register –2 - Immediate –3 - Direct –4 - Register indirect –5 - Based relative –6 - Indexed relative –7 - Based indexed relative
    • 52. x86 ADDRESSING MODES register addressing mode  x86 ADDRESSING MODES register addressing mode §Register addressing mode involves use of registers to hold the data to be manipulated. oMemory is not accessed, so it is relatively fast. §Examples of register addressing mode: –The the source & destination registers must match in size. •Coding "MOV CL,AX" will give an error, since the source is a 16-bit register and the destination is an 8-bit register.
    • 53. x86 ADDRESSING MODES immediate addressing mode x86 ADDRESSING MODES immediate addressing mode §In immediate addressing mode, as the name implies, when the instruction is assembled, the operand comes immediately after the opcode. oThe source operand is a constant. §This mode can be used to load information into any of register except the segment and flag registers.
    • 54. x86 ADDRESSING MODES immediate addressing mode x86 ADDRESSING MODES immediate addressing mode §To move information to the segment registers, the data must first be moved to a general-purpose register, then to the segment register.
    • 55. x86 ADDRESSING MODES direct addressing mode x86 ADDRESSING MODES direct addressing mode §In direct addressing mode, the data is in some memory location(s). oIn most programs, the data to be processed is often in some memory location outside the CPU. oThe address of the data in memory comes immediately after the instruction.
    • 56. x86 ADDRESSING MODES direct addressing mode x86 ADDRESSING MODES direct addressing mode •The address of the operand is provided with the instruction, as an offset address. –Calculate the physical address by shifting left the DS register and adding it to the offset: §Note the bracket around the address. –If the bracket is absent, executing the command will give an error, as it is interpreted to move the value 2400 (16-bit data) into register DL. •An 8-bit register.
    • 57. x86 ADDRESSING MODES register indirect addressing mode x86 ADDRESSING MODES register indirect addressing mode §In register indirect addressing mode, the address of the memory location where the operand resides is held by a register. oThe registers used for this purpose are SI, DI, and BX. §If these three registers are used as pointers, they must be combined with DS in order to generate the 20-bit physical address. –Notice that BX is in brackets. –The physical address is calculated by shifting DS left one hex position and adding BX to it.
    • 58. x86 ADDRESSING MODES register indirect addressing mode x86 ADDRESSING MODES register indirect addressing mode §The same rules apply when using register SI or DI. •Example 1-16 shows 16-bit data.
    • 59. x86 ADDRESSING MODES based relative addressing mode –Alternatives are "MOV CX,[BX+10]" or "MOV CX,10[BX]" •Again the low address contents will go into CL and the high address contents into CH. x86 ADDRESSING MODES based relative addressing mode §In based relative addressing mode, base registers BX & BP, and a displacement value, are used to calculate the effective address. oDefault segments used for the calculation of the physical address (PA) are DS for BX and SS for BP.
    • 60. x86 ADDRESSING MODES based relative addressing mode x86 ADDRESSING MODES based relative addressing mode §In the case of the BP register: –Alternatives are "MOV AL,[BP+5]" or "MOV AL,5[BP]". • BP+5 is called the effective address since the fifth byte from the beginning of the offset BP is moved to register AL.
    • 61. x86 ADDRESSING MODES indexed relative addressing mode x86 ADDRESSING MODES indexed relative addressing mode §The indexed relative addressing mode works the same as the based relative addressing mode. oExcept that registers DI & SI hold the offset address.
    • 62. x86 ADDRESSING MODES indexed relative addressing mode x86 ADDRESSING MODES indexed relative addressing mode §The indexed relative addressing mode works the same as the based relative addressing mode. oExcept that registers DI & SI hold the offset address.
    • 63. x86 ADDRESSING MODES based indexed addressing mode x86 ADDRESSING MODES based indexed addressing mode §By combining based & indexed addressing modes, a new addressing mode is derived called the based indexed addressing mode. –The coding of the instructions can vary. –One base register and one index register are used.
    • 64. x86 ADDRESSING MODES segment overrides x86 ADDRESSING MODES segment overrides §The x86 CPU allows the program to override the default segment and use any segment register. oIn "MOV AL,[BX]", the physical address of the operand to be moved into AL is DS:BX. •To override that default, specify the desired segment in the instruction as "MOV AL,ES:[BX]"
    • 65. x86 ADDRESSING MODESsummary x86 ADDRESSING MODES summary