assembler directives 8086 unit-1

40
Assembler Directives 1 Assembly Level Language Programming Program – Group of instructions Programming languages : o High level language. o Assembly level language. o Low level language. Features of High level language Programming : o User Friendly. o Easy to debug. o Requires usage of compiler or Interpreter. o Requires move memory space. Features of Assembly Level Language Programming: o neither user friendly nor machine friendly but yet good to handle with. o Requires Assembler. o Less memory space required when compared with high level language programs.

Upload: bala-krishna

Post on 14-Feb-2016

259 views

Category:

Documents


5 download

DESCRIPTION

microprocessor

TRANSCRIPT

Page 1: Assembler Directives 8086 UNIT-1

Assembler Directives 1

Assembly Level Language Programming Program – Group of instructions Programming languages :

o High level language. o Assembly level language. o Low level language.

Features of High level language Programming :o User Friendly. o Easy to debug. o Requires usage of compiler or Interpreter. o Requires move memory space.

Features of Assembly Level Language Programming:o neither user friendly nor machine friendly but yet good to handle with.o Requires Assembler.o Less memory space required when compared with high level language

programs.

Page 2: Assembler Directives 8086 UNIT-1

Assembler Directives 2

Assembly Level Language Programming Features of Low Level Language Programming:

o Machine friendly and machine dependent.o Lot of human effort required and difficult to debug.o Lesser Memory space required.

Assembly level language program uses mnemonics, hence easy to understand.

Assembly level language program explains about internal data movement.

Assembly level language program use a simple assembler binary code can be generated.

Page 3: Assembler Directives 8086 UNIT-1

Assembler Directives 3

Assembly Level Language Programming

Assembly Level Language Programming Tools: Editor:o Program which allows a programmer to create a file containing

Assembly language statements.o Stores the ASCII codes for letters and numbers in successive RAM

locations.o The program in Editor is stored into a floppy or hard disk as a file.o The source file is processed with an assembler by saving the source file

with an extension suitable for the assembler. Assembler:o It is program which translates the mnemonics for instructions to

corresponding binary codes.o It reads the source file and generates two files namely object file and list

file.

Page 4: Assembler Directives 8086 UNIT-1

Assembler Directives 4

Assembly Level Language Programming Object file : (.OBJ)

o Contains the binary code of the instructions and information about the addresses of the instructions.

o The object file is then loaded into memory and run after further processing it.

List file : (.LST)o It contains assembly language program statements, the

binary codes for each instruction and offset for each instruction.

o It also gives information about any typing or syntax errors present in the program, i.e., Assembly language grammar errors.

Page 5: Assembler Directives 8086 UNIT-1

Assembler Directives 5

Assembly Level Language Programming For generating machine codes from source file the assembler

takes two passes:First Pass:o Determines the displacement of named data items, the offset of

labels…..etc.o Puts the information into symbol table. Second Pass:o Produces the binary code for each instruction and inserts the offsets…

etc which are generated during first pass.o In order to facilitate programmer to test and troubleshoot program, list

file is sent to printer.o The assembler can give information only about the syntax errors.o After generating object file and list file an linker or locator is used to

assign the physical starting addresses for the segments.

Page 6: Assembler Directives 8086 UNIT-1

Assembler Directives 6

Assembly Level Language Programming Linker:

o Linker is a program used to join several object files into one large object file.

o Linker is useful when working with modular programo While writing program for an bigger application the ALP will be large,

thus it is usually much more efficient to divide the large program into smaller modules, which can be written, tested and debugged individually.

o The modules are linked together to form a large, functioning program.o Generally the object modules for useful programs can be kept in a library

file and linked with other programs as and when needed.o The linker file and link map file generated by linker contains the binary

code for all the combined modules and the address information about the linked files respectively.

o The linker assigns only relative addresses of the program which makes program re-locatable as it can be put any where in memory to run it later.

Page 7: Assembler Directives 8086 UNIT-1

Assembler Directives 7

Assembly Level Language Programmingo The linkers associated with TASM or MASM assemblers produce link files

with .EXE extension.o For running the programs on systems such as SDK-86 board, an locator

which assigns physical address to the segments in the .EXE file is used before debugging it using the debugger.

Locator : o Program used to assign the specific addresses for the segments in object

code to be loaded into memory. Debugger :

o Program which allows programmer to load object code of program into system memory, execute the program and debug it i.e ., trouble shoot it.

Page 8: Assembler Directives 8086 UNIT-1

Assembler Directives 8

Assembly Level Language Programming Functions of debugger:

o Allows the programmer to look at contents of registers and memory locations after the program is run.

o Allows to change the contents of registers and memory locations and return to the program.

o Some debuggers allow programmer to stop execution after each instruction so that programmer can check or alter memory and register contents.

o Allows a programmer to set a break point at any point in a program. Emulator:

o Mixture of hardware and software.o Usually used to test and debug the hardware and software of an external

system like prototype of microprocessor based instruments.o Emulator have a multi-wire cable which connects the host system to

prototype system.

Page 9: Assembler Directives 8086 UNIT-1

Assembler Directives 9

Start

Define Problem

Develop Algorithm

Create Source FileWith Editor

Assemble

Assembly

Errors

1

32

.ASM

.OBJ

.LST

YESNO

Page 10: Assembler Directives 8086 UNIT-1

Assembler Directives 10

LINK

Locate

1.EXE.MAP

.BIN

ExternalSystem

Load Program

RUN & TESTProgram

Load Program

Load Debugger Load Emulator

RUN & TESTProgram

4 5

YESNO

Page 11: Assembler Directives 8086 UNIT-1

Assembler Directives 11

4 5

ErrorsErrors

Stop

Use Debugger Tools To find errors

Use Debugger Tools To find errors

2 3

NONO

YES YES

Page 12: Assembler Directives 8086 UNIT-1

Assembler Directives 12

Machine Level Language Programming Generating the machine codes of program manually and execute it. Disadvantages of MLP :

o The process is complicated and time consuming.o The chances of error being committed are more at the machine level( in

hand-coding and entering the program byte-by-byte into the system).o Debugging a program at the machine-level is more difficult.o The programs are not understood by every one and the results are not

stored in user friendly form.

Page 13: Assembler Directives 8086 UNIT-1

Assembler Directives 13

Assembly Level Language Programming A program called ‘assembler’ is used to convert the mnemonics of

instructions along with the data into their equivalent object code modules, which may further be converted in executable code using the linker and loader programs.

This type of programming is called Assembly level language programming(ALP).

In ALP, the mnemonics are directly used in the user programs. The assembler performs the task of coding.

Advantages of ALP over MLP:o The Programming in assembly language is not so complicated as in

machine language because the function of coding is performed by an assembler.

o The chances of error being committed are less because the mnemonics are used instead of numerical opcodes. It is easier to enter an ALP.

o As the mnemonics are purpose suggestive, the debugging is easier.

Page 14: Assembler Directives 8086 UNIT-1

Assembler Directives 14

Assembly Level Language Programmingo The constants and address locations can be labeled with suggestive labels

hence imparting a more user friendly interface to user.Advanced assemblers provide facilities like macros, lists,……… etc making the task of programming much easier.

o The memory control is in the hands of users as in machine language.o The results may be stored in a more user-friendly form.o The flexibility of programming is more in assembly language programming

as compared to machine language programming because of advanced facilities available with the modern assemblers.

Page 15: Assembler Directives 8086 UNIT-1

Assembler Directives 15

Assembly Level Language Programming Assembly language programming (ALP) explains the way the computer

hardware and operating system work together and also about how the

application programs communicate with the operating system.

ALP helps in understanding the computer architecture and operating system.

ALP imposes rules about what is allowed and what not allowed in a program.

ALP makes the programmer understand the operating system, hardware and

data storage which is not possible with high level programming.

Assembly language programming is called low level language programming

because it is close to machine language in structure and function.

Each assembly language instruction corresponds to one machine instructions.

Page 16: Assembler Directives 8086 UNIT-1

Assembler Directives 16

Assembly Level Language Programming Mnemonic :

Short alphabetic code that literally assists the memory in remembering a CPU instruction. It may be an instruction or directive.

Operand :An instruction may contain zero, one (or) two operands.An operand may be a register, a memory variable, or an immediate

value (data).The choice of operand is usually determined by the addressing

mode.The addressing mode tells the assembler, where to find data in each

operand, i.e., whether in a register, or in a memory or as immediate data.

Instruction Mnemonic + Operand

Page 17: Assembler Directives 8086 UNIT-1

Assembler Directives 17

Assembly Level Language Programming Comment :

The beginning of comment is marked by a semi-colon (;) character. All other characters to the right are ignored by the assembler.

When a particular line is beginned with a semicolon, then whole line is treated as a comment.

When ALP is being executed using Assembler: Instruction:

Translated into machine code by the assembler. Assembler Directives:

For directing the assembler.For directives no machine code is generated and no memory

location is allocated.

Page 18: Assembler Directives 8086 UNIT-1

Assembler Directives 18

Assembler Directives And Operators Assembler Directives:

• Gives direction to the assembler.• Necessary for assembler to assemble efficiently and correctly.• Controls the generation of machine code and organizes the program.• Is an indication to the assembler about the assumed contents in the

segment registers under the various circumstances as the assembly progresses.

• Gives the programmer more control over the exact placement of data and segmentation of program than high level language program.

The hints to be given by programmer are regarding:• The required storage for a particular constant or a variable.• Logical names of segments.• Types of different routines and modules• End of file,………… etc.

Page 19: Assembler Directives 8086 UNIT-1

Assembler Directives 19

Assembler Directives And Operators The hints given to the assembler are:

1) Assembler Directives2) Operators

Assembler Directives:• The hints in the form of predefined alphabetical strings.• Assembler Directives help the assembler to correctly understand

assembly language programs to prepare the codes.Thus the directions to the assembler and pseudo operations are

known to be Assembler Directives.• The Assembler directives are not assigned with any memory locations.• The advantages of labels in assembly language programming are:

1) Program becomes more understandable.2) The tedious byte handling and manipulations are avoided.

Page 20: Assembler Directives 8086 UNIT-1

Assembler Directives 20

Assembler Directives And Operators When the program is written and assembled using assembler, then the

assembler decides the address of each label and substitutes the values for each of the constants and variables, the machine code for mnemonics and data in the assembly language program.

While assembler is assembling a program it can find out only syntax errors and in order to handle programming errors and logical errors the assembler needs some special directions or hints from the programmer.

The assembler directives are used to specify start and stop of a program, attach value to variables, allocate storage locations to input or output data, to define start and end of Segments/Procedures/Macros……….. Etc.

The assembler directives control the generation of machine code and organization of the programs but no machine codes are generated for assembler directives.

Page 21: Assembler Directives 8086 UNIT-1

Assembler Directives 21

Assembler Directives And Operators Operators:

• Operators are the hints which helps the assembler to assign a particular constant with a label or initialize particular memory locations or labels with constants is called an operator.

• The operator performs the arithmetic and logical tasks unlike directives that just direct the assembler to correctly interpret the program, to code it appropriately.

Page 22: Assembler Directives 8086 UNIT-1

Assembler Directives 22

Assembler Directives Data definition and storage allocation:

• Statements that reassign data and reserve storage have the form:Variable Mnemonic Operand,……….,Operand ; Comments(Label)• The variable is optional, but if it is present it is assigned the offset of

the first byte that is reserved by the directive.Unlike the label field, a variable must be terminated by a

blank, not a colon. The mnemonic in the statement determines the length of each operand

and is one of the following:• DB (Define Byte) : Each operand datum of the label or variable

occupies one byte location in memory.• DW(Define Word) : Each operand datum of the label or variable

occupies one Word or two memory locations where low-order byte is stored in first location and the higher-order byte is stored in second location.

Page 23: Assembler Directives 8086 UNIT-1

Assembler Directives 23

Assembler Directives DD (Define Double Word) : Each operand datum is two words long with

low-order word followed by high-order word. DQ(Define Quad Word) : Each operand data is 4 words long i.e. 8 bytes and

is stored starting from lowest byte to higher bytes. DT(Define Ten Bytes) : Each operand datum is 10 bytes long and is stored

starting from the lowest byte to higher bytes. The character string is stored in between single quotes and each character’s

ASCII codes are saved(stored) in successive locations(i.e. the first character goes into the first byte assigned to the variable, then second character of the string is stored at second byte and so on…………)

The character string is defined or pre-assigned using byte type. The character string can be defined using DW and DD also, but they are

rarely used as the bytes are reversed and also string operands in a DW or DD cannot exceed two characters in length.

Page 24: Assembler Directives 8086 UNIT-1

Assembler Directives 24

Assembler Directives

When an question mark (?) is used along with mnemonic as second mnemonic, it does not pre-assign any value but, the appropriate amount of space is reserved.

DUP operator is used along with data size defining mnemonics to duplicate multiple locations with the specified value in the braces.ex: DUP(0) fills 0’s in all the variable locations.

Types of numbers used in data statements: Binary : ex 11100101B• Negative number is represented in 2’s complement sign-magnitude

form.• Any binary number is represented using a suffix of character ‘B’. Decimal : ex 20/ 20D / -20 / -20D• Any decimal number is represented using a suffix of character ‘D’.• Decimal is default data type.

Page 25: Assembler Directives 8086 UNIT-1

Assembler Directives 25

Assembler Directives Hexadecimal : ex: 30H

• Always any hexadecimal number is represented using a suffix of character ‘H’.

• Always a ‘0’(zero) is to be appended in front of a hexadecimal number that starts with alphabet.

• An decimal data type is converted to equivalent hexadecimal number and then stored in the memory locations.

BCD : ex: 37Hthe difference between an hexadecimal number and an BCD number can be made only from its usage.

ASCII : ex: ‘Array’• An ASCII value is always written in single quotes.• An variable of ASCII type will have the ASCII equivalent value of the

operand is stored in the memory.• An Alphanumeric character string given in single quotes as ASCII string

is case sensitive.

Page 26: Assembler Directives 8086 UNIT-1

Assembler Directives 26

Assembler Directives Program termination directive: END

END Label• As an END statement is needed to signal the end of a high-level language

program, the end directive indicate the end of a set of assembler language code.

• The label appears only in the termination of the main program in a program structure,although all separately assembled program modules must conclude with an END directive.

• The label must also appear in the instruction to be executed first, the point at which the program begins.

• Specifically, the address associated with this label is the address that the loader branches to after the program has been loaded and is ready for execution.

• Thus the END directive is put after the last statement of the program to tell the assembler that it is the end of the program module/file.

• Any statement after an END directive is ignored by the assembler.

Page 27: Assembler Directives 8086 UNIT-1

Assembler Directives 27

Assembler Directives Procedure Definitions directives:

PROC & ENDP PROC

• The PROC directive is used to identify the start of the procedure.

• The PROC directive follows a name given to the procedure.• After the PROC directive the label NEAR or FAR is used to

specify the type of the procedure. ENDP

The ENDP directive is used along with the name of the procedure to indicate the end of a procedure to the assembler.

Page 28: Assembler Directives 8086 UNIT-1

Assembler Directives 28

Assembler Directives

Alignment Directives:• EVEN: This directive forces the address of the next byte to be even.• ORG :

ORG Constant Expression• The ORG directive causes the next byte to be associated with the byte

number expressed by the constant expressionORG 1000HX DW 2002H[1000H] = 2002H• When an program is being assembled by the assembler (data

declarations or instruction statements) an location counter keeps track of how many bytes it is from the start of a segment at any time.

• The usage of EVEN directive tells the assembler to increment the location counter to the next EVEN address if it is not already at even address.

Page 29: Assembler Directives 8086 UNIT-1

Assembler Directives 29

Assembler Directives

When the directive EVEN is used in data segment then the location counter is simply be incremented to the next even address is necessary.

When the EVEN directive is used in code segment, the location counter will be incremented to the next even address if necessary. A NOP instruction will be inserted in the location incremented over.EVEN Num1 DW 4

Assigning names to expressions:If an expression appears several times in a program, it is sometimes more convenient to give it a name and refer to it by the name, which also permit a short name to replace a lengthy expression, and also will be meaningful which helps programmers and others to remember as well as understand them while reading.

Expression_name EQU Expression

Page 30: Assembler Directives 8086 UNIT-1

Assembler Directives 30

Assembler Directives Expression_name: any valid identifier or label Expression: Have the format of any valid operand or that evaluates to a

constant or any valid mnemonic.Normally assigning names to expressions is done in the beginning

to the program EQU (equate) : The directive EQU is used to assign a label with a value or a

symbol.The use of this directive is just to reduce the recurrence of the

numerical values or constants in a program code.While assembling, whenever the assembler comes across the label,

it substitutes the numerical value for that label and finds out the equivalent code.

Using the EQU directive, even an instruction mnemonic can be assigned with a label which can then be used in program in place of that mnemonic.

Page 31: Assembler Directives 8086 UNIT-1

Assembler Directives 31

Assembler Directives Segment definition directives:

One of the tasks that an assembler should perform is to assign the offsets of the labels and variables as it translates the instructions into machine language, the same information has to be passed to the linker such that various segments and modules are put together to form a program.

In order to explain the exact structure of each segment such that variable and label offsets are assigned by the assembler

The data,extra data, or stack segment normally has the form:Segment_name SEGMENT

storage definition directivesallocation and alignment directives

Segment_name ENDS

Page 32: Assembler Directives 8086 UNIT-1

Assembler Directives 32

Assembler Directives

A code segment normally has the form of Segment_name SEGMENT

Instructions andInstruction-related directives

Segment_name ENDS The directives SEGMENT and ENDS defines the starting and ending

boundaries of a segment and the Segment_name is identifier of the segment. The definitions,allocations and directives between SEGMENT and ENDS

directives are contained within that particular segment. In addition to specifying boundaries for the assembler to translate the

instructions, it must know the exact correspondence between the segments and the segment registers, which allows the assembler to check for certain types of errors and inconsistencies i.e., whether a variable is defined in the appropriate data or stack segment.

Page 33: Assembler Directives 8086 UNIT-1

Assembler Directives 33

Assembler Directives

The assignments of the segments to the segment registers are made using the directive ASSUME whose format is:

ASSUME Assignment,………..,AssignmentWhere Assignment will be of the form:

Segment_register_name : Segment_name It is important to note that the assume directive does not load

the segment addresses into the corresponding segment registers. The segment addresses can not directly loaded into segment

registers, so general purpose registers are used for this purpose. When an logical segment is set-up, a name is to be given as a

label for the segment and the label cannot have spaces within it, but an underscore can be used.

Instruction mnemonic or assembler directive can not be used as label.

Page 34: Assembler Directives 8086 UNIT-1

Assembler Directives 34

Assembler Directives A logical segment is not usually given a physical starting address when it is

declared. After the program is assembled and linked with other assembled program modules, it is then assigned the physical address where it will be loaded in the memory to run.

Additional terms/indicators like PUBLIC,WORD…….etc are added along with the segment directive statement to indicate some special way in which we want the assembler to treat the segment.• Public: To indicate that this can be used with other segments with same

name from other assembly modules, when the modules are linked together.

• Word: Contents of this segment located on the next available word(even) address when absolute addresses given to it, else it will be loaded on the next available paragraph(16-bytes) address.

When the assembler starts reading a segment by default the location counter is automatically set to 0000H, the ORG directive allows programmer to set the location counter to a desired value at any point in the program.

Page 35: Assembler Directives 8086 UNIT-1

Assembler Directives 35

Assembler Directives

LABEL :Used to give a name to the current value in the location counter.

INCLUDE : (Include Source Code From File)Used to tell the assembler to insert a block of source code from the

named file into current source module. GROUP: (Group Related Segments)

The group directive is used to tell the assembler to group the logical segments named after the directive into one logical group segment.

This directive allows the contents of all the segments to be accessed from the same group segment base.

The assembler sends a message to the linker telling it to link the segments so that the segments are physically in the same 64K-Byte segment.

Page 36: Assembler Directives 8086 UNIT-1

Assembler Directives 36

Assembler Directives PUBLIC:

Any label/variable name referred to in other modules must be declared public in the module in which it is defined.

The public directive is used to tell the assembler that a specified name or label will be accessed from other modules.

GLOBAL : (Declare symbols as Public/Extern)The GLOBAL directive is used in the place of a PUBLIC /

EXTERN directive.The GLOBAL directive is used to make the symbol/variable

available to other modules. EXTERN :

Used to tell the assembler that the names or labels following the directive are in some other assembly module.

LOCAL : The labels, variables, constants or procedures declared LOCAL in a

module are to be used only by that particular module.

Page 37: Assembler Directives 8086 UNIT-1

Assembler Directives 37

Assembler Directives

NAME : (Logical Name Of A Module)The NAME directive is used to assign a name to an assembly

language program module. The module,may now be referred to by its declared name.

The names, if selected to be suggestive, may point out the functions of the different modules and hence may help in documentation.

FAR PTR : This directive indicates to the assembler that the label following

FAR PTR is not available within the same segment and the address of the label is of 32-bits i.e., 2-bytes offset followed by 2-bytes segment address.

NEAR PTR : This directive indicates that the label following NEAR PTR is in

the same segment and needs only 16-bits i.e., 2 bytes offset to address it.

Page 38: Assembler Directives 8086 UNIT-1

Assembler Directives 38

Operators SHORT :

Used to tell the assembler that only a 1-byte displacement is needed to code a jump instruction.

Using the SHORT operator saves 1-byte of memory by telling the assembler that it needs to reserve only 1-byte for this particular jump.

The destination must be in the range of –128 bytes to +127 bytes from the address of the instruction after the jump.

LENGTH :Returns the number of units assigned to a variable (not available

in MASM) SIZE :

Same as the LENGTH operator,except that it returns the number of bytes instead of the number of units.

Page 39: Assembler Directives 8086 UNIT-1

Assembler Directives 39

Operators ‘+’ and ‘-’ operators:

These operators represent arithmetic addition and subtraction respectively and are typically used to add or subtract displacements( 8-bit or 16-bit ) to base or index registers and stack or base pointer registers.

SEG :Causes the segment address of the variable or label to be inserted as

an immediate data or operand (although the actual insertion is made by the linker)

If the data_seg is assigned to the block of memory beginning at 05000H and opr1 is in data_seg, then the instruction

MOV BX, SEG opr1 would put 0500 in BX register. TYPE :

Tells the assembler to determine the data type of a specified variable/label and replaces the TYPE label by the decided data type.

The assembler determines the number of bytes in the type of the variable,ex: for the word type variable the data type is 2.

ex: MOV AX , TYPE STR will load 1 into AX if STR is a byte array and 2 is STR is a word array

Page 40: Assembler Directives 8086 UNIT-1

Assembler Directives 40

Operators OFFSET : Offset of the label

Tells the assembler to determine the offset or displacement of a named data item (variable) or procedure from the start of the segment which contains it.

This operator is usually used to load the offset of a variable into a register so that the variable can be accessed with one of the indexed addressing modes.

PTR : ( Pointer)Used to assign a specific type to a variable or a label .It is necessary to do so in any instruction where the type of the

operand is not clear.The PTR operator can be used to override the declared type of

variable and also to clarify our intentions when we use indirect jump instructions.