Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department,...
-
Upload
jonas-dalton -
Category
Documents
-
view
214 -
download
1
Transcript of Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department,...
MicroprocessorsSunday, Mar. 30
Dr. Asmaa FaroukFaculty of Engineering, Electrical Department,
Assiut University
Topics of Today• Reading:Reading:– Mazidi:• Section 1.5: More about Segments in the 80x86. (Cont.)• Section 1.6: 80x86 Addressing Modes.• Section 2.0: Assembly Language Programming.• Section 2.1: Directives and a Sample Program.• Section 2.3: More Sample Programs.
Topics of Today
• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.
More about Segments in the 80x86• Flag Register:Flag Register:– A 16-bit register, sometimes referred to as the
status register.• Although 16-bits wide, only some of its bits are used.• The rest are either undefined or reserved by Intel.
– Many Assembly language instructions change flag register bits.• Some instructions function differently based on the
information in the flag register.
More about Segments in the 80x86• Flag Register: (Cont.)Flag Register: (Cont.)– Six flags, called conditional flags, indicate some
conditions resulting after an instruction executes.
More about Segments in the 80x86• Flag Register: (Cont.)Flag Register: (Cont.)– Three flags, called control flags, control the
operation of instructions before they are executed.
More about Segments in the 80x86• Bits of the Flag Register:Bits of the Flag Register:– Bits used in x86 Assembly language programming,
with a brief explanation each:• CF (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.
• PF (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.
More about Segments in the 80x86• Bits of the Flag Register: (Cont.)Bits of the Flag Register: (Cont.)– Bits used in x86 Assembly language programming,
with a brief explanation each:• AF (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 arithmetic.
• ZF (Zero Flag): Set to 1 if the result of an arithmetic or logical operation is zero; otherwise, it is cleared.
More about Segments in the 80x86• Bits of the Flag Register: (Cont.)Bits of the Flag Register: (Cont.)– Bits used in x86 Assembly language programming,
with a brief explanation each:• 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 bit is
copied into the SF, indicating the sign of the result.
• 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.
More about Segments in the 80x86• Flag Register and ADD Instruction:Flag Register and ADD Instruction:– Flag bits that are affected by the ADD instruction:• CF (carry flag); PF (parity flag); AF (auxiliary carry flag).• ZF (zero flag); SF (sign flag); OF (overflow flag).
– Example (1):
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Flag bits that are affected by the ADD instruction:• CF (carry flag); PF (parity flag); AF (auxiliary carry flag).• ZF (zero flag); SF (sign flag); OF (overflow flag).
– Example (1):
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Flag bits that are affected by the ADD instruction:• CF (carry flag); PF (parity flag); AF (auxiliary carry flag).• ZF (zero flag); SF (sign flag); OF (overflow flag).
– Example (1):
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Flag bits that are affected by the ADD instruction:• CF (carry flag); PF (parity flag); AF (auxiliary carry flag).• ZF (zero flag); SF (sign flag); OF (overflow flag).
– Example (1):
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Flag bits that are affected by the ADD instruction:• CF (carry flag); PF (parity flag); AF (auxiliary carry flag).• ZF (zero flag); SF (sign flag); OF (overflow flag).
– Example (1):
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Flag bits that are affected by the ADD instruction:• CF (carry flag); PF (parity flag); AF (auxiliary carry flag).• ZF (zero flag); SF (sign flag); OF (overflow flag).
– Example (1):
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Flag bits that are affected by the ADD instruction:• CF (carry flag); PF (parity flag); AF (auxiliary carry flag).• ZF (zero flag); SF (sign flag); OF (overflow flag).
– Example (2):
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Same concepts are applied for 16-bit addition.– Differences between 8- and 16-bit operations in
terms of impact on the flag bits.• The parity bit (PF) only counts the lower 8 bits of the
result and is set accordingly.• The carry flag (CF) is set if there is a carry beyond bit d15
instead of bit d7.
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Example (1):
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Example (2):
More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Instructions such as MOV (data transfers) affect no
flags.– Example:
More about Segments in the 80x86• Use of Zero Flag for Looping:Use of Zero Flag for Looping:– A widely used application of the flag register is the
use of the zero flag to implement program loops.• A loop is a set of instructions repeated a number of times.
– 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.
More about Segments in the 80x86• Use of Zero Flag for Looping: (Cont.)Use of Zero Flag for Looping: (Cont.)– Example:
– Register CX is used to hold the counter.• BX is the offset pointer.
More about Segments in the 80x86• Use of Zero Flag for Looping: (Cont.)Use of Zero Flag for Looping: (Cont.)– Example:
– AL is initialized before the start of the loop.• In 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.
More about Segments in the 80x86• Use of Zero Flag for Looping: (Cont.)Use of Zero Flag for Looping: (Cont.)– Example:
– JNZ instruction must come immediately after the instruction that decrements CX.• JNZ needs to check the effect of "DEC CX" on ZF.
– If any instruction were placed between them, that instruction might affect the zero flag.
Topics of Today
• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.
8086 Addressing Modes• The CPU can access operands (data) in various
ways called addressing modes.• The 8086 provides seven addressing modes:
1. Register.2. Immediate.3. Direct.4. Register indirect.5. Based relative.6. Indexed relative.7. Based indexed relative.
8086 Addressing Modes1.1. Register Addressing Mode:Register Addressing Mode:– Use registers to hold data to be manipulated.– Memory is not accessed, so this mode is very fast.– Example:
• Source and destination registers should match in the size.– Otherwise, it will give an error.
8086 Addressing Modes2.2. Immediate Addressing Mode:Immediate Addressing Mode:– As the name implies, the operand comes
immediately after the opcode.• The source operand is a constant.
– This mode can be used to load information into any register except the segment and flag registers.
– Example:
8086 Addressing Modes2.2. Immediate Addressing Mode: (Cont.)Immediate Addressing Mode: (Cont.)– To move information to the segment registers, the
data must first be moved to a general-purpose register, then to the segment register.
– Example:
– This will be illegal:
8086 Addressing Modes2.2. Immediate Addressing Mode: (Cont.)Immediate Addressing Mode: (Cont.)– The letter H is used to indicate hexadecimal data.– If hexadecimal data begin with a letter, the
assembler requires the data start with a 0.• To represent the hexadecimal value F2, 0F2H is used in
assembly language.
– Decimal data are represented as it is and require no special codes or adjustments.• An example is the 100 decimal in the instruction:
“MOV AL,100”.
8086 Addressing Modes2.2. Immediate Addressing Mode: (Cont.)Immediate Addressing Mode: (Cont.)– An ASCII-coded character or characters may be
depicted in the immediate form if the ASCII data are enclosed in apostrophes.
– Binary data are represented if the binary number is followed by the letter B.
8086 Addressing Modes3.3. Direct Addressing Mode: Direct Addressing Mode: – In this mode, data is in some memory location(s).• Used in most programs (data to be processed is often in
some memory location outside the CPU).• The address of the data in memory comes immediately
after the instruction.
– 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:
8086 Addressing Modes3.3. Direct Addressing Mode: (Cont.)Direct Addressing Mode: (Cont.)– Example:
– Note the bracket around the address.• If the bracket is absent, executing the command will give
an error, as it will be interpreted as to move the value 2400 (16-bit data) into register DL, which is an 8-bit register.
8086 Addressing Modes3.3. Direct Addressing Mode: (Cont.) Direct Addressing Mode: (Cont.) – The MOV instruction transfers data between a
memory location within the data segment, and the AL (8-bit), AX (16-bit), or EAX (32-bit) registers.• Usually a 3-byte long instruction.
– Example: MOV AL, DATA loads AL from the data segment memory location DATA (e.g., 1234H).• DATA is a symbolic memory location, while 1234H is the
actual hexadecimal location.
8086 Addressing Modes4.4. Register Indirect Addressing Mode:Register Indirect Addressing Mode:– In this mode, the address of the memory location
where the operand resides is held by a register.• The 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.
– Example:
• Notice that BX is in brackets.• The physical address is calculated by shifting DS left one
hex position and adding BX to it.
8086 Addressing Modes4.4. Register Indirect Addressing Mode: (Cont.)Register Indirect Addressing Mode: (Cont.)– The same rules apply when using register SI or DI.
– The following example shows 16-bit data:
8086 Addressing Modes4.4. Register Indirect Addressing Mode: (Cont.)Register Indirect Addressing Mode: (Cont.)– The data segment (DS) is used by default with
register indirect addressing or any other mode that uses BX, DI, or SI to address memory.
– If the BP register addresses memory, the stack segment is used by default.• These settings are considered the default for these four
index and base registers.
– For the 80386 and above:• EBP addresses memory in the stack segment by default.• EAX, EBX, ECX, EDX, EDI, and ESI address memory in the
data segment by fault.
8086 Addressing Modes4.4. Register Indirect Addressing Mode: (Cont.)Register Indirect Addressing Mode: (Cont.)– Indirect addressing allows a program to refer to
tabular data (tables or arrays) located in memory.– To accomplish this task:• Load the starting location of the table (array) into the BX
register with a MOV immediate instruction.• Use register indirect addressing to store the samples
sequentially (50 for example).
8086 Addressing Modes5.5. Based Relative Addressing Mode:Based Relative Addressing Mode:– In this mode, base registers BX & BP, or a
displacement value, are used to calculate the effective address.• Default segments used for the calculation of the physical
address are DS for BX and SS for BP.
• 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.
8086 Addressing Modes5.5. Based Relative Addressing Mode: (Cont.)Based Relative Addressing Mode: (Cont.)– 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.
8086 Addressing Modes6.6. Indexed Relative Addressing Mode:Indexed Relative Addressing Mode:– This mode works the same as the based relative
addressing mode, except that registers DI & SI hold the offset address.
8086 Addressing Modes6.6. Indexed Relative Addressing Mode: (Cont.)Indexed Relative Addressing Mode: (Cont.)– Example:
8086 Addressing Modes6.6. Indexed Relative Addressing Mode: (Cont.)Indexed Relative Addressing Mode: (Cont.)– Example:
8086 Addressing Modes7.7. Based Indexed Addressing Mode:Based Indexed Addressing Mode:– By combining based & indexed addressing modes:
one base register and one index register are used.• The coding of the instructions can vary.
8086 Addressing Modes7.7. Based Indexed Addressing Mode: (Cont.)Based Indexed Addressing Mode: (Cont.)– The base register often holds the beginning
location of a memory (e.g., array).– The index register holds the relative position of an
element in the array.• Whenever BP addresses memory data, both the stack
segment register and BP generate the effective address.
8086 Addressing Modes7.7. Based Indexed Addressing Mode: (Cont.)Based Indexed Addressing Mode: (Cont.)– A major use is to address elements in a memory
array.– To accomplish this:• Load the BX register (base) with the beginning address of
the array and the DI register (index) with the element number to be accessed.
8086 Addressing Modes• Segment Overrides:Segment Overrides:– The x86 CPU allows the program to override the
default segment and use any segment register.• Example: In "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]”.
8086 Addressing Modes
Topics of Today
• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.
Assembly Language Programming• An Assembly language program is a series of
statements, or lines.– Either Assembly language instructions, or
statements called directives.• Directives (pseudo-instructions) give directions to the
assembler about how it should translate the Assembly language instructions into machine code.
• Assembly language instructions consist of four fields: [label:] mnemonic [operands][;comment][label:] mnemonic [operands][;comment]– Brackets indicate that the field is optional.• Do not type in the brackets.
Topics of Today
• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.
Directives and a Sample Program• Assembly Language Instructions:Assembly Language Instructions:
[label:] mnemonic [operands][;comment]– The label field allows the program to refer to a line
of code by name.• The label field cannot exceed 31 characters.
– A label must end with a colon when it refers to an opcode generating instruction.
Directives and a Sample Program• Assembly Language Instructions: (Cont.)Assembly Language Instructions: (Cont.)
[label:] mnemonic [operands][;comment]– The mnemonic (instruction) and operand(s) fields
together accomplish the tasks for which the program was written.
– The mnemonic opcodes are ADD and MOV.
Directives and a Sample Program• Assembly Language Instructions: (Cont.)Assembly Language Instructions: (Cont.)
[label:] mnemonic [operands][;comment]– The mnemonic (instruction) and operand(s) fields
together accomplish the tasks for which the program was written.
– "AL,BL" and "AX,6764" are the operands.• Instead of a mnemonic and operand, these fields could
contain assembler pseudo-instructions, or directives.• Directives do not generate machine code and are used
only by the assembler as opposed to instructions.
Directives and a Sample Program• Assembly Language Instructions: (Cont.)Assembly Language Instructions: (Cont.)– Examples of directives: DB, END, ENDP.
Directives and a Sample Program• Assembly Language Instructions: (Cont.)Assembly Language Instructions: (Cont.)
[label:] mnemonic [operands][;comment]– The comment field begins with a ";" and may be at
the end of a line or on a line by themselves.• The assembler ignores comments.
– Comments are optional, but highly recommended to make it easier to read and understand the program.
Directives and a Sample Program• Model Definition: Model Definition: – Two comments, then the .MODEL directive.
Directives and a Sample Program• Model Definition: (Cont.)Model Definition: (Cont.)– This directive selects the size of the memory model.
Directives and a Sample Program• Model Definition: (Cont.)Model Definition: (Cont.)– Other the options for the memory model
are: .SMALL, .MEDIUM, .COMPACT, .HUGE, .TINY and .LARGE
Directives and a Sample Program• Segment Definition:Segment Definition:– Every line of an Assembly language program must
correspond to one x86 CPU segment register.• CS (code segment); DS (data segment).• SS (stack segment); ES (extra segment).
– The simplified segment definition format uses three simple directives: ".CODE" ".DATA" ".STACK”• Which correspond to the CS, DS, and SS registers.
Directives and a Sample Program• Segments of a Program:Segments of a Program:
– The stack segment defines storage for the stack.– The data segment defines the data the program will
use.– The code segment contains Assembly language
instructions.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Stack segment:
• This directive reserves 64 bytes of memory for the stack.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Data segment:
• The data segment (in this program) defines three data items: DATA1, DATA2, and SUM.• Data items defined in the
data segment will be accessed in the code segment by their labels.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Data segment:
• The DB (Define Byte) directive is used by the assembler to allocate memory in byte-sized chunks.• Memory can be allocated in
different sizes (e.g., DW (Define Word)).
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Data segment:
• DATA1 and DATA2 are given initial values in the data section.• SUM is not given an initial
value, but storage is set aside for it.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment:
• Defined by the .CODE directive.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment:
• First line of the segment after the .CODE directive is the PROC directive.
• A procedure is a group of instructions designed to accomplish a specific function.• A code segment is
organized into one or several small procedures to make the program more structured.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment:
• First line of the segment after the .CODE directive is the PROC directive.
• The PROC directive may have the option FAR or NEAR.• DOS requires the entry
point to the user program to be a FAR procedure.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment:
• Every procedure must have a name defined by the PROC directive, followed by the assembly language instructions, and closed by the ENDP directive. • The PROC and ENDP
statements must have the same label.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment:
• Before the OS (DOS) passes control to the program so it may execute, it assigns segment registers values.• When the program begins
executing, only CS and SS have the proper values (by DOS).• DS and ES values are
initialized by the program.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment: Example:
• The program loads AL & BL with DATA1 & DATA2, ADDs them together, and stores the result in SUM.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment: Example:
• The last instructions, "MOV AH,4CH" & "INT 21H“, return control to the operating system.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment: Example:
• The last two lines end the procedure & program.• The label for ENDP (MAIN)
matches the label for PROC.
Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment:
• A sample shell: fill in it with the instructions and data for your program.
Topics of Today
• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.
• Program 2-1: (Add 5 bytes of data):Program 2-1: (Add 5 bytes of data):
More Sample Programs
• Analysis of Program 2-1: Analysis of Program 2-1:
More Sample Programs
• Load the value 05 into the CX register.–Used by the program as a
counter for iterations (looping).
• Analysis of Program 2-1: Analysis of Program 2-1:
More Sample Programs
• Load into BX the offset address assigned to DATA.• The assembler starts at offset
0000 and uses memory for the data, then assigns the next available offset memory for SUM (in this case, 0005).
• Analysis of Program 2-1: Analysis of Program 2-1:
More Sample Programs
• Adds the contents of the memory location pointed at by the register BX to AL.
• Analysis of Program 2-1: Analysis of Program 2-1:
More Sample Programs
• Increments the pointer by adding 1 to BX. This will cause BX to point to the next data item. (next byte).
• Analysis of Program 2-1: Analysis of Program 2-1:
More Sample Programs
• Decrement (subtract 1 from) the CX counter (will set the zero flag high if CX becomes zero).
• Analysis of Program 2-1: Analysis of Program 2-1:
More Sample Programs
• Jump back to the label AGAIN as long as the zero flag is indicating that CX is not zero.–Will not jump only after the zero
flag has been set high by the "DEC CX" instruction (CX becomes zero).–When CX becomes zero, this
means that the loop is completed and all five numbers have been added to AL.
• Various Approaches to Program 2-1:Various Approaches to Program 2-1:– Variations of Program 2-1 clarify the use of
addressing modes, and show that the x86 can use any general-purpose register for arithmetic and logic operations.
More Sample Programs
• Various Approaches to Program 2-1:Various Approaches to Program 2-1:
More Sample Programs
• Program 2-2: (Add 4 words of data):Program 2-2: (Add 4 words of data):
More Sample Programs
• The 16-bit data (a word) is stored with the low-order byte first ("little endian“). E.g., 234DH, 4DH will be stored at address 0000H and 23H at 0001H.
• Program 2-2: (Add 4 words of data):Program 2-2: (Add 4 words of data):
More Sample Programs
• The address pointer is incremented twice, since the operand being accessed is a word (two bytes).• The program could have used "ADD
DI,2" instead of using "INC DI" twice.
• Program 2-2: (Add 4 words of data):Program 2-2: (Add 4 words of data):
More Sample Programs
• Load the pointer (address) for the memory allocated for the label SUM.
• ORG directive tells the assembler that the address of next memory location (SUM) will be at 0010H (instead of 0008H, after input data).
• Program 2-2: (Add 4 words of data):Program 2-2: (Add 4 words of data):
More Sample Programs
• Moves the contents of register BX to memory locations with offsets 0010H and 0011H.
• Program 2-3: (Transfer 6 bytes of data):Program 2-3: (Transfer 6 bytes of data):
More Sample Programs
• Analysis of Program 2-3:Analysis of Program 2-3:
More Sample Programs
• C4 was coded in the data segments as 0C4H, (0 is required if the first digit is a hex letter A through F).
• Analysis of Program 2-3:Analysis of Program 2-3:
More Sample Programs
• The program uses registers SI & DI as pointers to the data items being manipulated.• SI is a pointer to the data
items to be copied (source pointer).• DI points to the location the
data is copied to (destination pointer).
• At each iteration of the loop, both data pointers are incremented to point to the next byte.
Questions?Questions?