UNIT II ADDRESSING MODES, INSTRUCTION SET & PROGRAMMING OF 8086 Addressing modes in 8086: Every instruction in a program has to operate on data. The process of specifying the data to be operated on by the instruction is called addressing. The various formats of specifying operands are called addressing modes. The addressing modes in the 8086 can be classified as five types. They are 1. 2. 3. 4. 5.

Register addressing mode Immediate addressing mode Data memory addressing modes Program memory addressing modes Stack memory addressing modes

Register addressing mode: In this addressing mode, the data present in register is moved or manipulated and the result is stored in register. Examples: MOV AL,BL MOV CX,BX ADD CL,BL

Immediate addressing mode: In this mode, the data is directly given in the instruction. Examples: MOV AL,50H MOV BX,23A0H MOV [SI],43C0H

Data memory addressing modes: The term Effective Address (EA) represents the offset address of the data within a segment which is obtained by different methods, depending upon the addressing mode that is used in the instruction. Direct Addressing: In this mode, the 16 bit offset address of the data within the segment is directly given in the instruction. Examples: MOV AL, [1000H] MOV BX,[2000H] Base Addressing: In this mode, the EA is the content of BX or BP register. When BX register is present in the instruction, data is taken from the data segment and if BP is present in the instruction, data is taken from the stack segment. Examples: MOV CL,[BX] : Memory address=DSx10+(BX) MOV DX,[BP] ; Memory address=SSx10+(BP) 1

Base Relative Addressing: In this mode, the EA is obtained by adding the content of the base register with an 8-bit or 16 bit displacement. The displacement is a signed number with negative values represented in two’s complement form. Examples: MOV AX,[BX+5] ; Memory address=DSx10H+(BX)+5 MOV CH,[BX-100H] ; Memory address=DSx10H+(BX)-100H Index Addressing: In this mode, the EA is the content of SI or DI register which is specified in the instruction. The data is taken from data segment. Examples: MOV BL,[SI] ;Memory address=DSx10H+SI MOV CX,[DI] ; Memory address=DSx10H+(DI) Index relative Addressing: This mode is same as base relative addressing mode except that instead of BP or BX register, SI or DI register is used. Example: MOV BX,[SI-100H] ;Memory Address=DSx10H+(SI)-100H. MOV CL,[DI+10H] ; Memory address=DSx10H+(DI)+10H. Base plus index Addressing: In this mode, the EA is obtained by adding the content of a base register and index register. Example: MOV AX,[BX+SI] ; Memory address=DSx10H+(BX)+(SI) Base Relative plus index Addressing: In this mode, the EA is obtained by adding the content of a base register, an index and a displacement. Example: MOV CX,[BX+SI+50H] ; Memory address=DSx10H+(BX)+(SI)+50H

Program memory addressing modes: Program memory addressing modes are used with JMP and CALL instructions and consist of three distinct forms namely direct, relative and indirect. The control of program execution can be transferred with in the current segment called INTRA SEGEMENT transfer and from one segment to another segment called INTER SEGMENT transfer. Direct Addressing: The direct program memory addressing stores both the segment and offset address where the control has to be transferred with the opcode. Example: JMP 32000H. Relative Addressing: The term relative here means relative to the instruction pointer (IP). Relative JMP and CALL instructions contain either an 8 bit or a 16 bit signed displacement that is added to the current instruction pointer and based on the new value of IP thus obtained, the address of the next instruction to be executed is calculated using the relation CSx10H+IP. Examples: JMP SHORT OVER JMP NEAR PTR FIND 2

Indirect Addressing: The indirect jump or CALL instructions use either any 16 bit register. Examples: JMP [BX] CALL [DI]

Stack memory addressing mode: The stack holds data temporarily and also stores return address for procedures and interrupt service routines. The stack memory is a last-in, first-out (LIFO) memory. Data are placed into the stack using PUSH instruction and taken out from the stack using POP instruction. The CALL instruction uses the stack to hold the return address for procedures and RET instruction is used to remove return address from stack. The stack segment is maintained by two registers: - The stack pointer (SP) and the stack segment register (SS). Always a word is entered into stack. - Whenever a word of data is pushed into the stack, the higher-order 8 bits of the word are placed in the memory location specified by SP-1 (i.e. at address SSx10H + SP-1)and - The lower-order 8 bits of the word are placed in the memory location specified by SP-2 in the current stack segment (SS) (i.e. at address SSx10H + SP-2). Example: PUSH AX PUSH [1234H] -

The SP is then decremented by 2. The data pushed into the stack may be either the content of a 16 bit register or segment register or 16 bit data in memory. Whenever a word is popped from the stack, the lower order 8 bits of the word are removed from the memory location specified by the SP and The higher order 8 bits of the word are removed from the memory location specified by SP+1 in the stack segment. SP is then decremented by 2. Example: POP AX POP [1234H]

INSTRUCTION SET OF 8086: The 8086 instructions are categorized into the following main types. i. Data Copy / Transfer Instructions ii. Arithmetic and Logical Instructions iii. Branch Instructions iv. Loop Instructions v. Machine Control Instructions vi. Flag Manipulation Instructions vii. Shift and Rotate Instructions viii. String Instruction. Data Copy / Transfer Instructions : MOV : Move: This instruction copies a word or a byte of data from some source to a destination. The destination can be a register or a memory location. The source can be a register, a memory location, or an immediate number. Syntax : MOV DESTINATION, SOURCE Eg: MOV BL, 50H ; Move immediate data 50H to BL MOV CX, [BX] ; Copy word from memory at [BX] to CX MOV AX, CX ; Copy contents of CX to AX 3

PUSH: Push on the Stack This instruction pushes the contents of the specified register/memory location on to the stack. The stack pointer is decremented by 2, after each execution of the instruction. Syntax: PUSH SOURCE E.g. PUSH AX PUSH DS PUSH [5000H]

POP : Pop from Sack This instruction when executed, loads the specified register/memory location with the contents of the memory location of which the address is formed using the current stack segment and stack pointer. The stack pointer is incremented by 2. Syntax: POP DESTINATION Eg. POP AX POP DS POP [5000H]

XCHG : Exchange byte or word This instruction exchange the contents of the specified source and destination operands. Syntax : XCHAG DESTINATION, SOURCE Eg. XCHG [5000H], AX XCHG BX, AX XLAT : Translate byte using look-up table. The code to be transmitted should be stored in AL. The base address of look up table should be stored in BX. Syntax: XLAT Eg. LEA BX, TABLE1 MOV AL, 04H XLAT IN: Copy a byte or word from specified port to accumulator. Syntax: IN AL/AX, PORT/DX IN instruction has two formats. 1) Fixed port format and 2) Variable port format 4

Fixed port format: The 8 bit port address of a port is specified directly in the instruction. Eg. IN AL,03H IN AX,03H Variable port format: The port address is loaded in to the DX before using IN instruction. Eg: MOV DX,0FE12H IN AX,DX OUT: Copy a byte or word from accumulator to the specified port. Syntax: OUT PORT/DX, AL/AX OUT instruction has two formats. 1) Fixed port format and 2) Variable port format Fixed port format: The 8 bit port address of a port is specified directly in the instruction. Eg. OUT 03H,AL OUT 03H, AX Variable port format: The port address is loaded in to the DX before using OUT instruction. Eg: MOV DX,0FE12H OUT DX.AX LEA : Load effective address of operand in specified register.[reg] offset portion of address in DS Syntax: LEA reg, offset Eg: lea ax, list LDS: Load DS register and other specified register from memory. Syntax: LDS 16bitreg, mem. loc Eg. LDS si,[1234h] LES: Load ES register and other specified register from memory. Syntax: LES 16bitreg, mem. loc Eg. LES si,[1234h] LSS: Load SS register and other specified register from memory. Syntax: LSS 16bitreg, mem. loc Eg. LSS si,[1234h]

ARITHMETIC INSTRUCTIONS The 8086 provides many arithmetic operations: addition, subtraction, negation, multiplication and comparing two values. ADD : Addition :The add instruction adds the contents of the source operand to the destination operand. Syntax: ADD destination, Source Eg. ADD AX, 0100H ADD AX, BX ADD AX, [SI]

ADC : Add with Carry :This instruction performs the same operation as ADD instruction, but adds the carry flag to the result. Syntax: ADC destination, Source Eg. ADC 0100H ADC AX, BX ADC AX, [SI] SUB : Subtract: The subtract instruction subtracts the source operand from the destination operand and the result is left in the destination operand. Syntax: SUB destination, Source 5

Eg. SUB AX, 0100H SUB AX, BX SBB : Subtract with Borrow :The subtract with borrow instruction subtracts the source operand and the borrow flag (CF) which may reflect the result of the previous calculations, from the destination operand. Syntax: SBB destination, Source Eg. SBB AX, 0100H SBB AX, BX SBB AX, [5000H]

INC : Increment: This instruction increases the contents of the specified Register or memory location by 1. Immediate data cannot be operand of this instruction. INC DESTINATION Eg. INC AX INC [BX] INC [5000H] DEC : Decrement :The decrement instruction subtracts 1 from the contents of the specified register or memory location. Syntax: DEC Destination Eg. DEC AX DEC [5000H] NEG : Negate The negate instruction forms 2’s complement of the specified destination in the instruction. The destination can be a register or a memory location. This instruction can be implemented by inverting each bit and adding 1 to it. Syntax: NEG Destination Eg. NEG AL AL = 0011 0101 35H Replace number in AL with its 2’s complement AL = 1100 1011 = CBH CMP : Compare This instruction compares the source operand, which may be a register or an immediate data or a memory location, with a destination operand that may be a register or a memory location. Syntax: CMP destination, Source Eg. CMP BX, 0100H CMP AX, 0100H CMP [5000H], 0100H CMP BX, [SI] MUL :Unsigned Multiplication Byte or Word This instruction multiplies an unsigned byte or word by the contents of AL. Syntax: MUL Source Eg. MUL BH ; (AX)  (AL) x (BH) MUL CX ; (DX)(AX)  (AX) x (CX) IMUL :Signed Multiplication This instruction multiplies a signed byte in source operand by a signed byte in AL or a signed word in source operand by a signed word in AX. Syntax: IMUL Source 6

Eg. IMUL BH ; IMUL CX ;

(AX)  (AL) x (BH) (DX)(AX)  (AX) x (CX)

CBW : Convert Signed Byte to Word: This instruction copies the sign of a byte in AL to all the bits in AH. AH is then said to be sign extension of AL. Syntax: CBW Eg. CBW AX= 0000 0000 1001 1000 Convert signed byte in AL signed word in AX. Result in AX = 1111 1111 1001 1000 CWD : Convert Signed Word to Double Word: This instruction copies the sign of a byte in AL to all the bits in AH. AH is then said to be sign extension of AL. Syntax: CWD Eg. CWD Convert signed word in AX to signed double word in DX : AX DX= 1111 1111 1111 1111 Result in AX = 1111 0000 1100 0001 DIV : Unsigned division: This instruction is used to divide an unsigned word by a byte or to divide an unsigned double word by a word. Syntax: DIV Source Eg. DIV CL ; Word in AX / byte in CL ; Quotient in AL, remainder in AH DIV CX ; Double word in DX and AX / word ; in CX, and Quotient in AX ; remainder in DX IDIV : Signed division: This instruction is used to divide a signed word by a byte or to divide a signed double word by a word. Syntax: IDIV Source Eg. IDIV CL ; Word in AX / byte in CL ; Quotient in AL, remainder in AH IDIV CX ; Double word in DX and AX / word ; in CX, and Quotient in AX ; remainder in DX AAA : ASCII Adjust After Addition The AAA instruction is executed aftr an ADD instruction that adds two ASCII coded operand to give a byte of result in AL. The AAA instruction converts the resulting contents of Al to a unpacked decimal digits. Syntax: AAA Eg. ADD AL, DL ; AAA AAS : ASCII Adjust AL after Subtraction: This instruction corrects the result in AL register after subtracting two unpacked ASCII operands. The result is in unpacked decimal format. The procedure is similar to AAA instruction except for the subtraction of 06 from AL. Syntax: AAS Eg. SUB CL, DL ; AAS AAM : ASCII Adjust after Multiplication: This instruction, after execution, converts the product available In AL into unpacked BCD format. Syntax: AAM Eg. MOV AL, 04 ; AL = 04 MOV BL ,09 ; BL = 09 MUL BL ; AX = AL*BL ; AX=24H AAM ; AH = 03, AL=06 AAD : ASCII Adjust before Division: This instruction converts two unpacked BCD digits in AH and AL to the equivalent binary number in AL. This adjustment must be made before dividing the two unpacked BCD digits in AX by an unpacked BCD byte. In the instruction sequence, this instruction appears Before DIV instruction. Syntax: AAD Eg. AX : 05 08 AAD result in AL 00 3A 58D = 3A H in AL 7

The result of AAD execution will give the hexadecimal number 3A in AL and 00 in AH. Where 3A is the hexadecimal Equivalent of 58 (decimal). DAA : Decimal Adjust Accumulator This instruction is used to convert the result of the addition of two packed BCD numbers to a valid BCD number. The result has to be only in AL. Syntax: DAA Eg. AL = 53 CL = 29 ADD AL, CL ; AL (AL) + (CL) ; ; AL 53 + 29; ; AL 7C DAA ; AL 7C + 06 (as C>9) ; AL 82 DAS : Decimal Adjust after Subtraction This instruction converts the result of the subtraction of two packed BCD numbers to a valid BCD number. The subtraction has to be in AL only. Syntax: DAS Eg. AL = 75, BH = 46 SUB AL, BH ; AL 2 F = (AL) - (BH) ; AF = 1 DAS ; AL 2 9 (as F>9, F - 6 = 9)

LOGICAL INSTRUCTIONS AND : Logical AND This instruction bit by bit ANDs the source operand that may be an immediate register or a memory location to the destination operand that may a register or a memory location. The result is stored in the destination operand. Syntax: AND Destination, Source Eg. AND AX, 0008H AND AX, BX OR : Logical OR This instruction bit by bit ORs the source operand that may be an immediate, register or a memory location to the destination operand that may a register or a memory location. The result is stored in the destination operand. Syntax: OR Destination, Source Eg. OR AX, 0008H OR AX, BX NOT : Logical Invert This instruction complements the contents of an operand register or a memory location, bit by bit. Syntax: NOT Destination Eg. NOT AX NOT [5000H] XOR : Logical Exclusive OR This instruction bit by bit XORs the source operand that may be an immediate , register or a memory location to the destination operand that may a register or a memory location. The result is stored in the destination operand. Syntax: XOR Destination, Source

8

Eg. XOR AX, 0098H XOR AX, BX TEST : Logical Compare Instruction The TEST instruction performs a bit by bit logical AND operation on the two operands. The result of this ANDing operation is not available for further use, but flags are affected. Syntax: TEST Destination, Source Eg. TEST AX, BX TEST [0500], 06H SAL/SHL : Shift Arithmetic Left/ Shift Logical Left SAL and SHL are two mnemonics for the same instruction. This instruction shifts each bit in the specified destination to the left and 0 is stored at LSB position. The MSB is shifted into the carry flag. The destination can be a byte or a word. It can be in a register or in a memory location. The number of shifts is indicated by count. Syntax: SAL / SHL destination, count.

Eg. SAL CX, 1 SAL AX, CL

SHR : Shift Logical Right This instruction shifts each bit in the specified destination to the right and 0 is stored at MSB position. The LSB is shifted into the carry flag. The destination can be a byte or a word. It can be a register or in a memory location. The number of shifts is indicated by count. Syntax: SHR destination, count

Eg. SHR CX, 1 MOV CL, 05H SHR AX, CL SAR : Shift Arithmetic Right This instruction shifts each bit in the specified destination some number of bit positions to the right. As a bit is shifted out of the MSB position, a copy of the old MSB is put in the MSB position. The LSB will be shifted into CF. Syntax: SAR destination, count

Eg. SAR BL, 1 MOV CL, 04H SAR DX, CL 9

ROL : Rotate Left : This instruction rotates all bits in a specified byte or word to the left some number of bit positions. MSB is placed as a new LSB and a new CF. Syntax: ROL destination, count

Eg. ROL CX, 1 MOV CL, 03H ROL BL, CL

ROR : Rotate Right: This instruction rotates all bits in a specified byte or word to the right some number of bit positions. LSB is placed as a new MSB and a new CF. Syntax: ROR destination, count

Eg. ROR CX, 1 MOV CL, 03H ROR BL, CL RCL : Rotate left through carry: This instruction rotates all bits in a specified byte or word some number of bit positions to the left along with the carry flag. MSB is placed as a new carry and previous carry is place as new LSB. Syntax: RCL destination, count

Eg. RCL CX, 1 MOV CL, 04H RCL AL, CL RCR : Rotate Right through carry: This instruction rotates all bits in a specified byte or word some number of bit positions to the right along with the carry flag. LSB is placed as a new carry and previous carry is place as new MSB. Syntax: RCR destination, count

Eg. RCR CX, 1 MOV CL, 04H RCR AL, CL

BRANCH INSTRUCTIONS Branch Instructions transfers the flow of execution of the program to a new address specified in the instruction directly or indirectly. When this type of instruction is executed, the CS and IP registers get loaded with new values of CS and IP corresponding to the location to be transferred.

10

The Branch Instructions are classified into two types i. Unconditional Branch Instructions. ii. Conditional Branch Instructions. Unconditional Branch Instructions : In Unconditional control transfer instructions, the execution control is transferred to the specified location independent of any status or condition. The CS and IP are unconditionally modified to the new CS and IP. CALL : Unconditional Call This instruction is used to call a Subroutine (Procedure) from a main program. Address of procedure may be specified directly or indirectly. There are two types of procedure depending upon whether it is available in the same segment or in another segment. i. Near CALL i.e., ±32K displacement. ii. For CALL i.e., anywhere outside the segment. On execution this instruction stores the incremented IP & CS onto the stack and loads the CS & IP registers with segment and offset addresses of the procedure to be called. RET: Return from the Procedure. At the end of the procedure, the RET instruction must be executed. When it is executed, the previously stored content of IP and CS along with Flags are retrieved into the CS, IP and Flag registers from the stack and execution of the main program continues further. INT N: Interrupt Type N. In the interrupt structure of 8086, 256 interrupts are defined corresponding to the types from 00H to FFH. When INT N instruction is executed, the type byte N is multiplied by 4 and the contents of IP and CS of the interrupt service routine will be taken from memory block in 0000 segment. INTO: Interrupt on Overflow This instruction is executed, when the overflow flag OF is set. This is equivalent to a Type 4 Interrupt instruction. JMP: Unconditional Jump This instruction unconditionally transfers the control of execution to the specified address using an 8-bit or 16-bit displacement. No Flags are affected by this instruction. IRET: Return from ISR When it is executed, the values of IP, CS and Flags are retrieved from the stack to continue the execution of the main program.

11

LOOP: LOOP Unconditionally This instruction executes the part of the program from the Label or address specified in the instruction upto the LOOP instruction CX number of times. At each iteration, CX is decremented automatically and JUMP IF NOT ZERO structure. Syntax: LOOP address Conditional Branch Instructions When this instruction is executed, execution control is transferred to the address specified relatively in the instruction, provided the condition implicit in the Opcode is satisfied. Otherwise execution continues sequentially. JZ/JE Label Transfer execution control to address ‘Label’, if ZF=1. JNZ/JNE Label Transfer execution control to address ‘Label’, if ZF=0 JS Label Transfer execution control to address ‘Label’, if SF=1. JNS Label Transfer execution control to address ‘Label’, if SF=0. JO Label Transfer execution control to address ‘Label’, if OF=1. JNO Label Transfer execution control to address ‘Label’, if OF=0. JNP Label Transfer execution control to address ‘Label’, if PF=0. JP Label Transfer execution control to address ‘Label’, if PF=1. JB Label Transfer execution control to address ‘Label’, if CF=1. JNB Label Transfer execution control to address ‘Label’, if CF=0. JCXZ Label Transfer execution control to address ‘Label’, if CX=0 Conditional LOOP Instructions. LOOPZ / LOOPE Label Loop through a sequence of instructions from label while ZF=1 and CX=0. LOOPNZ / LOOPENE Label Loop through a sequence of instructions from label while ZF=1 and CX=0.

STRING MANIPULATION INSTRUCTIONS A series of data byte or word available in memory at consecutive locations, to be referred as Byte String or Word String. A String of characters may be located in consecutive memory locations, where each character may be represented by its ASCII equivalent. The 8086 supports a set of more powerful instructions for string manipulations for referring to a string, two parameters are required. I. Starting and End Address of the String. II. Length of the String. The length of the string is usually stored as count in the CX register. The incrementing or decrementing of the pointer, in string instructions, depends upon the Direction Flag (DF) Status. If it is a Byte string 12

operation, the index registers are updated by one. On the other hand, if it is a word string operation, the index registers are updated by two. REP : Repeat Instruction Prefix This instruction is used as a prefix to other instructions, the instruction to which the REP prefix is provided, is executed repeatedly until the CX register becomes zero (at each iteration CX is automatically decremented by one). i.REP – repeat operation while CX≠0 ii. REPE / REPZ - repeat operation while equal / zero. iii. REPNE / REPNZ - repeat operation while not equal / not zero. These are used for CMPS, SCAS instructions only, as instruction prefixes. MOVSB / MOVSW :Move String Byte or String Word Suppose a string of bytes stored in a set of consecutive memory locations is to be moved to another set of destination locations. The starting byte of source string is located in the memory location whose address may be computed using SI (Source Index) and DS (Data Segment) contents. The starting address of the destination locations where this string has to be relocated is given by DI (Destination Index) and ES (Extra Segment) contents. CMPSB / CMPSW : Compare String Byte or String Word The CMPS instruction can be used to compare two strings of byte or words. The length of the string must be stored in the register CX. If both the byte or word strings are equal, zero Flag is set.The REP instruction Prefix is used to repeat the operation till CX (counter) becomes zero or the condition specified by the REP Prefix is False. SCANSB / SCANSW : Scan String Byte or String Word This instruction scans a string of bytes or words for an operand byte or word specified in the register AL or AX. The String is pointed to by ES:DI register pair. The length of the string s stored in CX. The DF controls the mode for scanning of the string. Whenever a match to the specified operand, is found in the string, execution stops and the zero Flag is set. If no match is found, the zero flag is reset. LODSB / LODSW : Load String Byte or String Word The LODS instruction loads the AL / AX register by the content of a string pointed to by DS : SI register pair. The SI is modified automatically depending upon DF, If it is a byte transfer (LODSB), the SI is modified by one and if it is a word transfer (LODSW), the SI is modified by two. No other Flags are affected by this instruction. STOSB / STOSW : Store String Byte or String Word The STOS instruction Stores the AL / AX register contents to a location in the string pointer by ES : DI register pair. The DI is modified accordingly, No Flags are affected by this instruction. The direction Flag controls the String instruction execution, The source index SI and Destination Index DI are modified after each iteration automatically. If DF=1, then the execution follows autodecrement mode, SI and DI are decremented automatically after each iteration. If DF=0, then the execution follows autoincrement mode. In this mode, SI and DI are incremented automatically after each iteration.

13

FLAG MANIPULATION AND A PROCESSOR CONTROL INSTRUCTIONS These instructions control the functioning of the available hardware inside the processor chip. These instructions are categorized into two types: 1. Flag Manipulation instructions. 2. Machine Control instructions. FLAG MANIPULATION INSTRUCTIONS The Flag manipulation instructions directly modify some of the Flags of 8086. i. CLC – Clear Carry Flag. ii. CMC – Complement Carry Flag. iii. STC – Set Carry Flag. iv. CLD – Clear Direction Flag. v. STD – Set Direction Flag. vi. CLI – Clear Interrupt Flag. vii. STI – Set Interrupt Flag. MACHINE CONTROL INSTRUCTIONS HLT: The halt instruction stops the execution of all instructions and places the processor in a halt state. An interrupt or a reset signal will cause the processor to resume execution from the halt state. LOCK: The lock instruction asserts for the processor an exclusive hold on the use of the system bus. It activates an external locking signal ( ) of the processor and it is placed as a prefix in front of the instruction for which a lock is to be asserted. The lock will function only with the XCHG, ADD, OR, ADC, SBB, AND, SUB, XOR, NOT, NEG, INC and DEC instructions, when they involve a memory operand. An undefined opcode trap interrupt will be generated if a LOCK prefix is used with any instruction not listed above. NOP: No operation. This instruction is used to insert delay in software delay programs. ESC: This instruction is used to pass instructions to a coprocessor such as the 8087, which shares the address and data bus with an 8086. Instructions for the coprocessor are represented by a 6- bit code embedded in the escape instruction. WAIT: When this instruction is executed, the 8086 checks the status of TEST input pin and if the input is high, it enters an idle condition in which it does not do any processing. The 8086 will remain in this state until the 8086’s input pin is made low or an interrupt signal is received on the INTR or NMI pins

8086 ASSEMBLER DIRECTIVES

An assembler is a program used to convert an assembly language program into equivalent machine language program. The assembler finds the address of each label and substitutes the value for each of the constants and variables in the assembly language program during the assembly process, to generate the machine language code. While doing these things, the assembler may find out syntax errors and it is reported to programmer at the end of assembly process. The logical errors or other programming errors are not found by the assembler. For completing all the above tasks, an assembler needs some commands from the programmer namely the required storage class for a particular constant or a variable such as byte, word or double word, logical name 14

of the segments such as CODE or STACK or DATA segment, type of different procedures or routines such as FAR or NEAR or PUBLIC or EXTRN and end of a segment, etc. These types of commands are given to the assembler using some predefined alphabetical strings called assembler directives which help the assembler to correctly generate the machine codes for the assembly language program. DB, DW, DD, DQ and DT: The directives DB (Define Byte), DW (Define Word), DD (Define Double Word), DQ (Define Quad Word) and DT (Define Ten Bytes) are used to reserve one byte, one word (i.e. 2 Bytes), one double word (i.e. 2 words), one Quad word (i.e. 4 words) and ten bytes in memory respectively for storing constant, variables or string. Examples: DATA1 DB 20H ; Reserve one byte for storing DATA1 and assign the value 20H to it. ARRAY1 DB 10H,20H,30H ;reserve three bytes for storing ARRAY1 and initialize it with the values 10H, 20H, 30H. CITY DB "MADURAI” ;The ASCII code of the characters specified within double quotes are stored under the array or list named CITY. DATA2 DW 1020H ; Reserve one word for storing DATA2 and assign the value 1020H to it. ARRAY2 DW 1030h,2000h,3000h,4000h ;Reserve 4 words for storing ARRAY2 and initialize it with the specified values. EQU - The directive EQU (Equivalent) is used to assign a value to a data name. Examples: NUMBER EQU 50H; assign the value 50H to NUMBER

PROGRAM ORGANIZATION DIRECTIVES: The 8086 programs are organized as a collection of logical segments. The directives used to organize the program segments are: SEGMENT, ASSUME, ENDS etc. SEGMENT: The directive ‘SEGMENT’ is used to indicate the beginning of a logical segment. The directive segment follows the name of the segment. To end the segment, SEGMENT must be ended with ENDS statement. Syntax:

DATA SEGMENT ; ; ; ; program data definition ; ; ; DATA ENDS

ENDS: The directive ENDS informs the assembler the end of the segment. The directives ENDS and SEGMENT must enclose the segment data or code of the program. Syntax:

Segmentname ENDS. 15

ASSUME: The directive assume informs the assembler the name of the logical segment that should be used for a specified segment when program is loaded and the processor segment registers should point to the respective logical segments. Syntax: Ex:

ASSUME

segreg : segname, ……….., segreg : segname.

i) ASSUME CS : _code ii) ASSUME CS : _code, DS : _Data, SS : _stack

END: End of program: Program termination directive and it is used to inform the assembler the physical end of the program. The statement after the directive END will be ignored by the assembler. VALUE RETURNING ATTRIBUTE DIRECTIVES: The task of programming can be made easier by assigning assembler to compute the size of the data items it is performed using the directives LENGTH, SIZE, OFFSET and TYPE. LENGTH: The directive LENGTH informs the assembler about the number of elements in a data item such as an array. If an array is defined with DB then it returns the number of bytes allocated to a variable. If an array is defined with DW then it returns the number of words allocated to the array variable. Syntax:

LENGTH

Variablename

SIZE: The directive size is same as length except that it returns the number of bytes allocated to the data item instead of the number of elements in it. Syntax:

SIZE variablename

OFFSET: The directive OFFSET informs the assembler to determine the displacement of the specified variable with respect to the base of the segment. It is usually used to load the offset of a variable into the register using this OFFSET value a variable can be referenced using indexed addressing modes. Syntax:

OFFSET VariableName

SEG: SEGMENT: The directive SEG is used to determine the segment in which specified data item is defined. Syntax:

SEG

VariableName

TYPE: The directive type is used to determine the type of data type. The assembler allocates one byte to DB. Two bytes to DW and 4 bytes to DD. Syntax:

TYPE Variable name

ORG: Origin: This directive directs the assembler to start the memory allocation for a segment. Synatx: ORG address Eg: ORG 2000H SHORT: This directive is used to indicate the assembler that only one byte is required to code the displacement i.e 8 bit displacement for a jump. Eg: JMP short up 16

NEAR: This directive is used to indicate the assembler that only two bytes are required to code the displacement i.e 16 bit displacement for a jump. Eg: JMP near up FAR: This directive is used to indicate the assembler that four bytes are required to code the displacement i.e 32 bit displacement for a jump. Eg: JMP far up PTR: The PTR (Pointer) operator is used to declare the type of a label, variable or memory operand. The operator PTR is prefixed by either BYTE or WORD. If the prefix is BYTE then the particular label, variable or memory operand is treated as an 8-bit quantity, while if WORD prefix is used, then it is treated as a 16-bit quantity. Example: INC BYTE PTR [SI]-Increments byte contents of memory location addressed by SI. GLOBAL: The labels, variables, constants or procedures declared GLOBAL may be used by other modules of the program. The following statement declares the procedure ROOT as a GLOBAL label. Eg: ROOT PROC

GLOBAL

LOCAL: The variables, labels, constants and procedures declared LOCAL in a module are to be used only by that module. Syntax: LOCAL label, variable Eg: LOCAL op1, array

MODULAR PROGRAMMING In a program it may be necessary to perform a particular task repeatedly. The formulation of complex programs from numerous complex sequences called program modules, each of which performs a well defined task is referred to as modular programming. Large program can be broken down into small segments called modules. Each module implements a specific function and has its own code segment and data segment. The assembler directives Procedures and Macros are useful in situations. PROCEDURE: A procedure is a group instructions that usually performs one task. It forms a reusable section of the software, which is stored in the memory, but used as often as necessary. A procedure is a sequence of instruction, which can be employed repeatedly, within a longer program. CALL: Call instruction is used to call a procedure. When a microprocessor executes a CALL instruction stores the contents of IP on to the stack in case of ±16 bit displacement and CS and IP values are stored in the stack in case of ±32 bit displacement. Then CS and IP loaded with the address of the procedure so that control transfers to the specified procedure executes corresponding set of instructions. Examples: CALL avg NEAR ; indicates that the displacement is ± 16 bit that is intra segment procedure CALL salary FAR ; indicates that the displacement is ± 32 bit that is intra segment procedure RET: RET is the last instruction in the procedure. At the end of the procedure, the value stored in the stack is loaded back in the IP register to return execution to the calling program so that the control is transferred to the main program. Two more instructions RETF and RETN are provided for return from far/near procedure. PROC: The PROC directive indicates the start of a named procedure. Also, the type NEAR or FAR specify the type of the procedure. Example: SQUARE_ROOT PROC NEAR 17

The above statement indicates the beginning of a procedure named SQUARE_ROOT, which is to be called by a program located in the same segment. The FAR directive is used for the procedures to be called by the programs present in some other code segment other than the code segment in which the above procedure is present. For example SALARY PROC FAR indicates the beginning of a FAR type procedure named SALARY. ENDP – The ENDP directive is used to indicate the end of a procedure. To mark the end of a particular procedure, the name of the procedure may appear as a prefix with the directive ENDP. Example: SALARY PROC NEAR …….. …….. …….. SALARY ENDP EXTRN & PUBLIC: The directive EXTRN (external) informs the assembler that the procedures, label/labels and names declared after this directive has/have already defined in some other segments and in the segments where that procedures, label/labels and names actually appear, they must be declared public, using the PUBLIC directive. Example1: MODULE1 SEGMENT PUBLIC SQUARE_ROOT SQUARE_ROOT PROC FAR ………… ; code of SQUARE_ROOT procedure ………… SQUARE_ROOT ENDP MODULE1 ENDS MODULE2 SEGMENT EXTRN SQUARE_ROOT FAR ………………. ; code of MODULE2 ………………. CALL SQUARE_ROOT ……………… MODULE2 ENDS

MACRO and ENDM: A macro is a group instructions that performs a task. It is inserted in the program during the assembly process. Macro instructions are in the program by the assembler at a point where they are invoked by using their name. A macro is a sequence of code that needs to be written only once, but whose basic structure can be repeated several times within a module by giving it a name. Defining a MACRO: A MACRO can be defined anywhere in a program using the directives MACRO and ENDM. The label prior to the MACRO is the macro name which is used in the main program wherever needed. The ENDM directive marks the end of the instructions or statements assigned with the macro name. Example: CALCULATE MACRO MOV AX, [BX] ADD AX, [BX+2] MOV [SI], AX ENDM

18

Using parameters in a macro definition, the programmer specifies the parameters of the macro those are likely to be changed each time the macro is called. The above macro (CALCULATE) can be modified to calculate the result for different sets of data and store it in different memory locations as given below: CALCULATE MACRO OPERAND, RESULT MOV BX, OFFSET OPERAND MOV AX, [BX] ADD AX, [BX+2] MOV SI, OFFSET RESULT MOV [SI], AX ENDM The parameters OPERAND and RESULT can be replaced by OPERAND1, RESULT1 and OPERAND2, RESULT2 while calling the above macro as shown below: ……………………….. CALCULATE OPERAND1, RESULT1 ………………………. ……………………… CALCULATE OPERAND2, RESULT2 ……………………….

COMPARISON OF FEATURES OF PROCEDURE AND MACRO: PROCEDURE

MACRO

Called during execution

Inserted during assembly process

Assembled and executed separately

Cannot be executed separately

Reduces memory requirements

No changes in memory requirements

May be anywhere and in any segment

It must be defined in the same program

Requires a special call instruction

Using the name is enough

Program control is transferred

Program control is not transferred

Can be used by any assembler

Used if assembler has a supports for Macro features Parameters are passed as a part of the statement that calls MACRO

Parameters are passed through register, memory or stack Machine code is put only once in memory

Machine code is generated each time when called

Accessed by CALL and return mechanism During program execution

Accessed during assembly process when a name given to it is defined

REFER NOTE BOOK AND LAB MANUAL FOR ASSEMBLY LANGUAGE PROGRAMS 19

UNIT II MPI NEW 22022018.pdf

The stack segment is maintained by two registers: - The stack pointer (SP) and the stack segment register (SS). Always a word is entered into stack. - Whenever ...

157KB Sizes 1 Downloads 121 Views

Recommend Documents

UNIT III MPI new.pdf
If an interrupt has been requested, the 8086 processes it by performing the following series of steps: a) Pushes the content of the flag register onto the stack to ...

UNIT II -
Mercantile Transactions Using Credit. Cards. • Two major components compromise credit card transactions in this process: electronic authorization and ...

UNIT II MAD Notes.pdf
UNIT II MAD Notes.pdf. UNIT II MAD Notes.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying UNIT II MAD Notes.pdf.

Planner NurseryT0 II Unit III.pdf
Planner NurseryT0 II Unit III.pdf. Planner NurseryT0 II Unit III.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying Planner NurseryT0 II Unit III.pdf.

Unit II - The Political Process.pdf
Explain how James Madison in Federalist #10 defined factions. Why did Madison fear factions,. and how was the Constitution designed to cure the “mischief of ...

OpenCUDA+MPI - GitHub
A Framework for Heterogeneous GP-GPU Cluster Computing. Kenny Ballou ... Parallel: Processing concurrently. Distributed: Processing over many computers.

OpenCUDA+MPI - GitHub
CPUs consist of a small number of cores (microprocessors) that are best at .... sands) of hosts (nodes), and executing application computations in parallel ... the unused CPU and GPU cycles on a computer to do scientific computing [10]. .... Rajagopa

MPI by Godse.pdf
Page 1. Whoops! There was a problem loading more pages. Retrying... MPI by Godse.pdf. MPI by Godse.pdf. Open. Extract. Open with. Sign In. Main menu.

Open MPI development - GitHub
Jan 29, 2015 - (ad d_ co… om pi_sh ow. _a ll_m ca_ pa rams op al_p rog ress_ set_e ... 1.0E+01. 1.0E+02. 1.0E+03. 1.0E+04. M emory. Inc rease in. M. C. A. _P. M. L_ ..... Express. PCI. Express. Comm. Engine. (Packet. Processing). Comm.

OpenCUDA + MPI - GitHub
Feb 15, 2013 - Who Uses Distributed Computing? Google – Page Indexing. Created Map-Reduce. Facebook – Data Mining. Universities. Many Others. Ballou.

MPI by Godse.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. MPI by Godse.

1ML - People at MPI-SWS
Jun 18, 2015 - explicit injection into and projection from first-class core values, accompanied ... lift the function (and potentially a lot of downstream code) to the.

OpenCUDA+MPI - GitHub
May 3, 2013 - Add process “scheduler” to best utilize available computing resources. Add Cluster ... Host to Device Memory Copies. Device to Host Memory ...

OpenCUDA+MPI - GitHub
Sample/ Test Problem Development. Results — Vector Summation ... Profile (Analyze) solutions. Develop framework ... Provisioning. Software. Configurations.

Unit I: Introduction to Interfacing Unit II: Legacy DOS ... -
Define Machine cycle with respect to microprocessor? 6. What do we ... Explain batch file? 5. What is ... What is PSP? Draw and explain the structure of PSP? 3.

Unit I: Introduction to Interfacing Unit II: Legacy DOS ... -
List feature of 8086 microprocessor? 2. List default offset pare of segment registers in 8086? 3. Why address and data bus are multiplexed in microprocessors?

Unit II CV Raman & Sam Pitroda.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Main menu.

IC Unit 05 Part -II Oscillators all.pdf
IC Unit 05 Part -II Oscillators all.pdf. IC Unit 05 Part -II Oscillators all.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying IC Unit 05 Part -II Oscillators ...

UNIT II MEDIA ACCESS & INTERNET WORKING.pdf
UNIT II MEDIA ACCESS & INTERNET WORKING.pdf. UNIT II MEDIA ACCESS & INTERNET WORKING.pdf. Open. Extract. Open with. Sign In. Main menu.

Unit II CV Raman & Sam Pitroda.pdf
... below to open or edit this item. Unit II CV Raman & Sam Pitroda.pdf. Unit II CV Raman & Sam Pitroda.pdf. Open. Extract. Open with. Sign In. Main menu.

unit 4 project appraisal-ii (economic feasibility)
During the course of our discussion in the last unit on technical feasibility analysis, the concept of ..... Output of Secondary Education, health,. These benefits may.

unit 11 appraisal of public enterprise performance-ii
ENTERPRISE PERFORMANCE-II. Objectives. After going through this unit you should be able to: • Evaluate the performance of state level public enterprises;. • Learn about the growth, investment and financial status of SLPEs;. • Get acquainted wit

Unit-6 World War II - Causes and Consequences (Emergence of ...
... League of Nations provided. Page 2 of 16 .... Unit-6 World War II - Causes and Consequences (Emergence of Super Powers).pdf. Unit-6 World War II - Causes ...

Unit Type Unit Charter Organization Unit Leader Unit Leader Phone ...
Unit Leader E-mail. Boy Scout Troop. 152. First United Methodist Church, ... Keith Hanselman. 330-929-6679 [email protected]. Boy Scout Troop.