Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department,...

92
Microprocesso rs Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University

Transcript of Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department,...

Page 1: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

MicroprocessorsSunday, Mar. 30

Dr. Asmaa FaroukFaculty of Engineering, Electrical Department,

Assiut University

Page 2: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty 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.

Page 3: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today

• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.

Page 4: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 5: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 6: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 7: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 8: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 9: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 10: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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):

Page 11: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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):

Page 12: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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):

Page 13: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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):

Page 14: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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):

Page 15: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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):

Page 16: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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):

Page 17: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 18: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Example (1):

Page 19: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

More about Segments in the 80x86• Flag Register and ADD Instruction: (Cont.)Flag Register and ADD Instruction: (Cont.)– Example (2):

Page 20: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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:

Page 21: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 22: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 23: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 24: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 25: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today

• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.

Page 26: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 27: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 28: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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:

Page 29: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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:

Page 30: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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”.

Page 31: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 32: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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:

Page 33: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 34: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 35: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 36: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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:

Page 37: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 38: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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).

Page 39: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 40: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 41: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 42: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

8086 Addressing Modes6.6. Indexed Relative Addressing Mode: (Cont.)Indexed Relative Addressing Mode: (Cont.)– Example:

Page 43: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

8086 Addressing Modes6.6. Indexed Relative Addressing Mode: (Cont.)Indexed Relative Addressing Mode: (Cont.)– Example:

Page 44: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 45: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 46: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 47: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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]”.

Page 48: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

8086 Addressing Modes

Page 49: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today

• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.

Page 50: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 51: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today

• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.

Page 52: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 53: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 54: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 55: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Directives and a Sample Program• Assembly Language Instructions: (Cont.)Assembly Language Instructions: (Cont.)– Examples of directives: DB, END, ENDP.

Page 56: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 57: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Directives and a Sample Program• Model Definition: Model Definition: – Two comments, then the .MODEL directive.

Page 58: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Directives and a Sample Program• Model Definition: (Cont.)Model Definition: (Cont.)– This directive selects the size of the memory model.

Page 59: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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

Page 60: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 61: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 62: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 63: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 64: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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)).

Page 65: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 66: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Directives and a Sample Program• Segments of a Program: (Cont.)Segments of a Program: (Cont.)– Code segment:

• Defined by the .CODE directive.

Page 67: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 68: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 69: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 70: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 71: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 72: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 73: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 74: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

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.

Page 75: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Topics of Today

• More About Segments in the 80x86. (Cont.)• 80x86 Addressing Modes.• Assembly Language Programming.• Directives and a Sample Program.• More Sample Programs.

Page 76: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• Program 2-1: (Add 5 bytes of data):Program 2-1: (Add 5 bytes of data):

More Sample Programs

Page 77: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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).

Page 78: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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).

Page 79: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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.

Page 80: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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).

Page 81: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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).

Page 82: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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.

Page 83: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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

Page 84: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• Various Approaches to Program 2-1:Various Approaches to Program 2-1:

More Sample Programs

Page 85: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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.

Page 86: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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.

Page 87: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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).

Page 88: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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.

Page 89: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• Program 2-3: (Transfer 6 bytes of data):Program 2-3: (Transfer 6 bytes of data):

More Sample Programs

Page 90: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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).

Page 91: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

• 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.

Page 92: Microprocessors Sunday, Mar. 30 Dr. Asmaa Farouk Faculty of Engineering, Electrical Department, Assiut University.

Questions?Questions?