CHAPTER 4 Assembler directives and operators COMPILED BY: TIGABU YAYA Assembler Directives and Operators • To make the programming on microprocessor simpler, assemblers are used. The main role of the assembler is to convert the assembly language program in to the machine language program and in 8086 since the hand coding is a tedious job mostly people follow the assembler for programming the microprocessor. • For completing the task of the assembly, assembler needs some hints from the programmer i.e. the required storage of particular constant or variable, logical names of the segments, types of different subroutines or modules, end of file etc. • Such hints are given by the assembler directives. • Another type of hint which helps the assembler to assign a particular constant with a label or initialize particular memory locations or labels with constants is an operator. 2 Assembler directives are specific for a particular assembler However all the popular assemblers like the Intel 8086 macro assembler, the turbo assembler and the IBM macro assembler use common assembler directives 3 8086 Microprocessor Assembler Directives Instructions to the Assembler regarding the program being executed. Control the generation of machine codes and organization of the program; No machine codes are generated for assembler directives. Also called ‘pseudo instructions’ Used to › specify the start and end of a program › attach value to variables › allocate storage locations to input/ output data › define start and end of segments, procedures, macros etc.. 4 Assemble Directives SEGMENT ENDS SEGMENT : Used to indicate the beginning of a code/ group of data/ group of instructions to be put together in a particular segment and is called logical segment . Used to indicate that the start of a logical segment. Preceding the segment directive is the name you want to give to the segment Can use labels upto 31 characters , Spaces not allowed but underscore can be used in between General form: Segnam SEGMENT … … … … … … Program code or Data Defining Statements Segnam ENDS User defined name of the segment 5 Assembler Directives(contd.) Assume • Used to tell the assembler the names of the logical segments to be assumed for different segments used in program. • You must tell the assembler that what to assume for any segment you use in the program. • Example, ASSUME CS: CODE tells the assembler that the instructions for the program are in segment named CODE, and hence the CS register is to be loaded with the segment address allotted by the operating system for label CODE. • ASSUME DS:DATA indicates that data items related to the program are available in logical segment named DATA. Assembler Directives(contd.) Another Format .CODE: This directive provides shortcut in definition of the code segment. General format for this directive is as shown below .CODE[name] The name is optional. It is basically specified to distinguish different code segments, when there are multiple code segments in the program. .DATA: This directive provides shortcut in definition of the data segment. Naming Data and addresses Programs work with three general categories of data - Constants ,Variables and Addresses Constants ,Variables and Addresses can be given Names in your programs and assembler can use these names to find a desired data or address when referred to in a program. Directives are for storage requirement of a particular constant or a variable Logical names of segments Types of different routines and modules End of file etc 8 Assemble Directives ---for variables DB, DW, DD, DQ, DT: These directives are used to define different types of variables, or to set aside one or more storage locations of corresponding data type in memory Their definitions are as follows: DB – Define Byte … used to declare a byte type variable or to store a byte in memory location. DW – Define Word … used to define a variable of type word or to reserve storage location of type word in memory DD - Define Double word--- used to declare a variable of type double word DQ – Define Quad words --- used to declare a variable 4 words in length DT – Define Ten Bytes --- used to declare a variable which is 10 bytes in length. 9 DUP • Allows a sequence of storage locations to be defined or reserved . If you need to declare a large array you can use DUP operator. It also assigns/intializes values to this location • The syntax for DUP: format: Name Data_Type number DUP ( values ) • number - number of duplicate to make (any constant value). • value - expression that DUP will duplicate. • LIST DB 4 DUP(9) is an alternative way of declaring: LIST DB 9, 9, 9, 9, DUP • Allows a sequence of storage locations to be defined or reserved . If you need to declare a large array you can use DUP operator. • The syntax for DUP: format: Name Data_Type number DUP ( values ) • number - number of duplicate to make (any constant value). • value - expression that DUP will duplicate. Example: • LIST DB 4 DUP(9) is an alternative way of declaring: • LIST DB 9, 9, 9, 9, • Only used as an operand of a define directive Array DB 40 DUP (?) ; 40 words, uninitialized Mesg DW 10h DUP (0) ; 16 words, initialized as 0 12 13 8086 Microprocessor Assemble Directives DW DD Define Word Define a word type (16-bit) variable Reserves two consecutive memory locations to each variable Range : 0000H – FFFFH for unsigned value; 0000H – 7FFFH for positive value and 8000H – FFFFH for negative value General form : variable DW value/ values The DD directive is used to declare a variable of type double word or to reserve memory locations which can be accessed as type double word (Define Double word) The DQ directive is used to tell the assembler to declare a variable 4 words in length or to reverse 4 words of storage in memory (Define Quadword) 14 Arrays • Any consecutive storage locations of the same size can be called an array Arrays • Arrays can be seen as chains of variables. A text string is an example of a byte array, each character is presented as an ASCII code value (0..255). • Here are some array definition examples: a DB 48h, 65h, 6Ch, 6Ch, 6Fh, 00h b DB 'Hello', 0 b is an exact copy of the a array, when compiler sees a string inside quotes it automatically converts it to set of bytes. You can access the value of any element in array using square brackets, for example: • MOV AL, a[3] 16 Equal Sign Directive • name = expression – expression must be numeric – these symbols may be redefined at any time maxint = 7FFFh count = 1 DW count count = count * 2 DW count EQU Directive EQU – Equate • Used to give a name to some value or to a symbol. • It is just to reduce the recurrence of the numerical values or constants in a program code • Format: name EQU expression – expression can be string or numeric – Use < and > to specify a string EQU – these symbols cannot be redefined later in the program sample EQU 7Fh;assigns 7FH with label sample aString EQU <1.234> message EQU <This is a message> Assembler Directives and Operators • 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 referred to by its declared name. • LABEL: label – The Label directive is used to assign a name to the current content of the location counter. When the assembly process starts, the assembler initializes a location counter to keep track of memory location assigned to the program. 19 Assembler Directives(contd.) END – End the program • To tell the assembler to stop fetching the instruction and end the program execution . – To tell the assembler that this is the end of the program module. • The assembler ignores any source lines after an END directive. • ENDP – END Procedure – It indicates the end of the procedure (subroutine) to the assembler. • ENDS – END Segment – This directive is used with the name of the segment to indicate the end of that logical segment. EVEN – Align on Even memory address • Tells the assembler to increment the location counter to the next even address if it is not already at an even address. Assembler Directives and Operators • EVEN – • If the word is at even address 8086 can read a memory in 1 bus cycle. – If the word starts at an odd address, the 8086 will take 2 bus cycles to get the data. – A series of words can be read much more quickly if they are at even address. – When EVEN is used the location counter will simply incremented to next address and NOP instruction is inserted in that incremented location. Example: DATA1 SEGMENT ; Location counter will point to 0009 after assembler reads next statement SALES DB 9 DUP(?) ;declare an array of 9 bytes EVEN ; increment location counter to 000AH RECORD DW 100 DUP( 0 ) ;Array of 100 words will start from an even address for quicker read DATA1 ENDS 21 Assembler Directives and Operators • OFFSET – It is an operator which tells the assembler to determine the offset or displacement of a named data item(variable) from the start of the segment which contains it. DATA SEGMENTS LIST DB 10H DATA ENDS CODE SEGMENT MOV SI, OFFSET LIST ; this is the same as, LEA SI, LIST CODE ENDS 22 Assembler Directives and Operators SEG: Segment – The segment operator returns the segment part of a label or variable’s address. – For example: for the definition below DATA SEGMENT NUM1 DB 0F2H NUM2 DB 2EH DATA ENDS The statements: MOV AX, DATA MOV DS, AX Can be replaced as: MOV AX, SEG NUM1 MOV DS, AX 23 Assembler Directives and Operators .MODEL – Provides short-cuts in defining segments. – It initializes memory model before defining any segment. – The memory model can be: TINY, SMALL, MEDIUM, COMPACT OR LARGE. – We can choose the memory model on our requirement referring following table Model Code segments Data segments Tiny Both are fit in a single 64KB Segment Small One One Medium Multiple One Compact One Multiple Large Multiple Multiple 24 Assembler Directives and Operators $ – A '$' is used to symbolically represent the current value of the location at any point. – This trick with the $ sign allows you to load the number of string elements in CX symbolically, rather than having to manually count the number. 25 Assembly Language Example Programs Write an assembly language program to transfer a block of bytes from one memory location to another memory location by using string instructions ASSUME CS:CODE, DS:DATA DATA SEGMENT STR1 DB 'COMPUTER ENGINEERING' LEN EQU $-STR1 STR2 DB LEN DUP(0) DATA ENDS CODE SEGMENT START:MOV AX,@DATA MOV DS, AX MOV SI, OFFSET STR1; LEA SI, STR1 LEA DI, STR2 ; MOV DI, OFFSET STR2 MOV CX, LEN BACK: MOV AL, [SI] MOV [DI], AL INC SI INC DI DEC CX JNZ BACK HLT CODE ENDS END START 26 Assembly Language Example Programs The above program can be again written in another format as follows In the above program replace the statements, DEC CX & JNZ BACK with LOOP BACK, and it becomes as follows. ASSUME CS:CODE, DS:DATA DATA SEGMENT STR1 DB 'COMPUTER ENGINEERING' LEN EQU $-STR1 STR2 DB LEN DUP(0) DATA ENDS CODE SEGMENT START:MOV AX,@DATA MOV DS, AX MOV SI, OFFSET STR1; LEA SI, STR1 LEA DI, STR2 ; MOV DI, OFFSET STR2 MOV CX, LEN BACK: MOV AL, [SI] MOV [DI], AL INC SI INC DI LOOP BACK HLT CODE ENDS END START o 27 Assembly Language Example Programs The above program can be again written in another format as follows .MODEL SMALL .DATA STR1 DB 'COMPUTER ENGINEERING' LEN EQU $-STR1 STR2 DB LEN DUP(?) .CODE START:MOV AX,@DATA MOV DS, AX MOV SI, OFFSET STR1; LEA SI, STR1 LEA DI, STR2 ; MOV DI, OFFSET STR2 MOV CX, LEN BACK: MOV AL, [SI] MOV [DI], AL INC SI INC DI LOOP BACK HLT END START 28 Assembly Language Example Programs The above program can be again written in another format as follows .MODEL SMALL .DATA STR1 DB 'COMPUTER ENGINEERING' LEN EQU $-STR1 STR2 DB LEN DUP(?) .CODE START:MOV AX,@DATA MOV DS, AX MOV ES, AX MOV SI, OFFSET STR1; LEA SI, STR1 LEA DI, STR2 ; MOV DI, OFFSET STR2 MOV CX, LEN CLD BACK: MOVSB LOOP BACK HLT END START 29 Assembly Language Example Programs The above program can be again written in another format as follows o By using the prefix instruction, REP, you can remove the LOOP BACK statement .MODEL SMALL .DATA STR1 DB 'COMPUTER ENGINEERING’ LEN EQU $-STR1 STR2 DB LEN DUP(?) .CODE START:MOV AX,@DATA MOV DS, AX MOV AX, 2000H MOV ES, AX MOV SI, OFFSET STR1; LEA SI, STR1 LEA DI, STR2 ; MOV DI, OFFSET STR2 MOV CX, LEN CLD REP MOVSB HLT END START 30 Assembly Language Example Programs The above program can be again written in another format as follows o In the above program, ES and DS were equals, i.e., They are on the same segment. But, DS and ES can also have different values as follows. .MODEL SMALL .DATA STR1 DB 'COMPUTER ENGINEERING' LEN EQU $-STR1 STR2 DB LEN DUP(?) .CODE START:MOV AX,@DATA MOV DS, AX MOV AX, 2000H MOV ES, AX MOV SI, OFFSET STR1; LEA SI, STR1 LEA DI, STR2 ; MOV DI, OFFSET STR2 MOV CX, LEN CLD REP MOVSB HLT END START 31 Assembly Language Example Programs 32 Assembly Language Example Programs • An alternative method for the program of addition of two numbers is given below:- • Please note that in this method there is no need to have two different segments of memory. In next few slides we will discuss few assembly language programs.33 • Assembly Language Example Programs 34 Assembler Directives and Operators ORG: Originate – This directive directs the assembler to start the memory allocation for a particular segment (data, code, or stack) form the declared offset address in the ORG statement. – While starting the assembly process for a memory segment, the assembler initializes a location counter (LC) to keep track of the allotted offset addresses for the segment. – When the ORG directive is not mentioned , LC is initialized with the offset address 0000h. When the ORG directive is mentioned at the beginning of the statement, LC is initialized with the offset address specified in the ORG directive. 35 Assembler Directives and Operators ORG – Originate – The ORG statement changes the starting offset address of the data. – When the ORG directive is not mentioned , LC is initialized with the offset address 0000h. – When the ORG directive is mentioned at the beginning of the statement, LC is initialized with the offset address specified in the ORG directive. – Example: • ORG 100h o When this directive is placed at the beginning of the data segment, the next data storage starts from the offset address 0100h within the data segment. o If it is placed in the code segment, the location counter is initialized with 0100h and the first instruction is stored from the offset address 0100h within the code segment. 36 8086 Microprocessor Assemble Directives EXAMPLES ORG 1000H Informs the assembler that the statements following ORG 1000H should be stored in memory starting with effective address 1000H LOOP EQU 10FEH Value of variable LOOP is 10FEH _SDATA In this data segment, effective address of memory location assigned to A will be 1200H and that of B will be 1202H and 1203H. SEGMENT ORG 1200H A DB 4CH EVEN B DW 1052H _SDATA ENDS 37 Assembler Directives and Operators • PUBLIC – The PUBLIC directive is used along with the Extern directive. • This informs the assembler that the labels, variables constants, or procedures declared PUBLIC may be accessed by other assembly modules to form their codes, but while using the PUBLIC declared labels, variables, constants or procedures, the user must declare them externals using the extern directive. • EXTERN: External – The directive EXTERN informs the assembler that the names, procedures and labels declared after this directive have already been defined in some other assembly language modules. 38 Assembler Directives and Operators • GLOBAL – The labels, variables, constants or procedures declared GLOBAL may be used by other modules of the program. Once a variable is declared GLOBAL, it can be used by any module in the program. • LOCAL – The labels, variables, constants or procedures declared LOCAL in a module are to be used only by the particular module. – LOCAL a, b, DATA, ARRAY, ROUTIN • GROUP: Group the Related Segment – This directive is used to form logical groups of segments with similar purpose or type. This directive is used to inform the assembler to form a logical group of the following segment names. • PROGRAM GROUP CODE, DATA, STACK 39 Assembler Directives and Operators • PROC: Procedure – The PROC directives marks the start of named procedure in the statement. – Also the types NEAR or FAR specify the type the procedure i.e. whether it is to be called by the main program located within 64k of physical memory or not. – Please, refer Procedures @ chapter 3, Unconditional branch instructions. • PTR: Pointer – The POINTER operator is used to declare the type of a label, variable or memory operand. – The operator PTR prefixed by either BYTE or WORD. – If prefix is byte then the particular label, variable or memory operand is treated as an 8-bit quantity, while if word is the prefix, then it is treated as a 16-bit quantity. – MOV AL,BYTE PTR [SI] 40 Assembler Directives and Operators Example: • INC [BX] ; This instruction will not know whether to increment the byte pointed to by BX or a word pointed to by BX. • INC BYTE PTR [BX] ;increment the byte ;pointed to by BX • This PTR operator can also be used to override the declared type of variable . If we want to access a byte in an array • WORDS DW 437Ah, 0B97h, • MOV AL, BYTE PTR WORDS 41 Assembler Directives and Operators • FAR PTR – This directive indicates 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 two byte segment address. • NEAR PTR – This directive indicates that the label following NEAR PTR is in the same segment and needs only 16-bit i.e. 2-byte offset to address it. • SHORT – The SHORT operator indicates to the assembler that only one byte is required to code the displacement for a jump (i.e. displacement is within -128 to +127 bytes from address of the byte next to the jump opcode). • JMP SHORT LABEL 42 8086 Microprocessor Assemble Directives procname PROC[NEAR/ FAR] … … … Program statements of the procedure RET Last statement of the procedure procname ENDP User defined name of the procedure 43 8086 Microprocessor Assemble Directives Examples: ADD64 PROC NEAR … … … The subroutine/ procedure named ADD64 is declared as NEAR and so the assembler will code the CALL and RET instructions involved in this procedure as near call and return RET ADD64 ENDP CONVERT PROC FAR … … … The subroutine/ procedure named CONVERT is declared as FAR and so the assembler will code the CALL and RET instructions involved in this procedure as far call and return RET CONVERT ENDP 44 8086 Microprocessor Assemble Directives Reserves one memory location for 8-bit signed displacement in jump instructions SHORT Example: JMP SHORT AHEAD The directive will reserve one memory location for 8-bit displacement named AHEAD 45 Assembly Language Example Programs 46 MACRO • Macro is a group of instructions. • The macro assembler generates the code in the program each time where the macro is ‘called’. • Macro can be defined by MACRO & ENDM assembler directives. • It is important to note the macro sequences execute faster than procedures because there no CALL & RET instructions to execute. • The assembler places the macro instructions in the program each time when it is invoked o This procedure is known as macro expansion. 47 MACRO • Comparison of Procedure and Macro 48 Comparison of Procedure and Macro 49 MACRO • Passing Parameters in Macro o In macro, parameters are passed as part of statement which calls Macro. o The macro instruction “PROMPT MES1” passes the MES1 as 50 a parameter and macro accepts that as an argument. Timings and Delays • Write an 8086 assembly program to calculate 4 + 5 Solution: using procedure 2 using two procedures and macros: SUM, and SQUARE 51 Timings and Delays • Write an 8086 assembly program to calculate 4 + 5 2 using two procedures and macros: SUM, and SQUARE Solution: using macro 52 Assembly Language Example Programs 53 Timings and Delays • In the real time applications, it is important to keep track with time. • Real Time Applications: Traffic Light Control, Digital Clock, Process Control, Serial Communication etc. For example in traffic light control application, it is necessary to give time delays between two transitions. • These time delays are in few seconds Can be generated with the help of executing group of instructions number of times. 54 Timings and Delays … Cont’d • Timer Delays Using NOP Instruction NOP instruction does nothing but takes 3 clock cycles of processor time to execute. So, by executing NOP instruction in between two instructions we can get delay of 3 clock cycles. • Timer Delay using Counters Counting can create time delays. Since the execution times of the instructions used in counting routine are known, the initial value of the counter, required to get specific time delay can be determined. 55 Timings and Delays … Cont’d • Timer Delay using Counters … Cont’d In this program, the instructions DEC CX and JNZ BACK execute number of times equal to COUNT stored in the CX register. The time taken by this program for execution can be calculated with the help of clock cycles. The column to the right of the comments indicates the number of clock cycles required for the execution of each instruction. Two values are specified for the number of clock cycles for JNZ instruction. o The smaller value is applied when the condition is not met, and 56 o The larger value is applied when it is met. Timings and Delays … Cont’d • Timer Delay using Counters … Cont’d The first instruction MOV CX, COUNT is executed only once and it requires 4 clock cycles. There are COUNT-1 passes through the loop where the condition is met and control is transferred back the first instruction in the loop (DEC CX). The number of clock cycles that elapse while CX register is not zero are (COUNT-1) x ( 2 +16 ). On the last pass through the loop the condition is not met and the loop is terminated. o The number of clock cycles that elapse in this pass are 2 + 4 57 Timings and Delays … Cont’d • Timer Delay using Counters … Cont’d The whole calculation of the time delay 58 Timings and Delays … Cont’d • Timer Delay using Counters … Cont’d Let us consider that we have to generate a delay of 50 ms using an 8086 system that runs at 10 MHz frequency. Using the same program we can calculate the count value as follows. 59 Timings and Delays … Cont’d • Timer Delay using Counters … Cont’d Assignment: what maximum delay can be produced with the above code? 60 Timings and Delays … Cont’d • Timer Delay using Nested Loops In this program, one more external loop is added to execute the internal loop multiple times. The inner loop is nothing but the program we have seen in the last section. 61 Timings and Delays … Cont’d • Timer Delay using Nested Loops … Cont’d 62 Timings and Delays … Cont’d 63 Timings and Delays … Cont’d 64 Assembly Language programming • A program is a set of instructions arranged in the specific sequence to do the specific task. • It tells the microprocessor what it has to do. • The process of writing the set of instructions which tells the microprocessor what to do is called “Programming”. • In other words, we can say that programming is the process of telling the processor exactly how to solve a problem. • To do this, the programmer must “speak” to the processor in a language which processor can understand. 65 Assembly Language programming Steps involved in Programming • Specify the problem • Designing the problem-solution • Coding • Debugging o Once the program is coded, the next step is debugging the code. o Debugging is the process of testing the code to see if it does the given task. o If program is not working properly, debugging process helps in finding and correcting errors. 66 Assembly Language programming Flow Chart • To develop the programming logic programmer has to write down various actions which are to be performed in proper sequence. • The flow chart is a graphical tool that allows the programmer to represent various actions which are to be performed. o The graphical representation is very useful for clear understanding of the programming logic. 67 Assembly Language programming Flow Chart • This figure shows the graphic symbols used in the flow chart Graphical symbols used in flow chart 68 Assembly Language programming Sample Flow Chart 69 Assembly Language Programming Assembly Language programs • Machine Level Language Programs o a program which has simply a sequence of the binary codes for the instructions o this binary form of the program is referred to as machine language because it is the form required by the machine. • However, to write program in machine language, programmer has to memorize the thousands of binary codes for a processor. o This task is difficult and error prone. • To make programming easier, usually programmers write programs in assembly language. o then, they translate the assembly language program to machine language so that it can be loaded into 70 memory and executed. Assembly Language Programming Assembly Language programs • Assembly language uses two, three or four letter words to represent each instruction types. o These words are referred to as mnemonics. • The letters in an assembly language mnemonics are usually initials or a shortened form of English word(s) for operation performed by the instruction. o For Example: Mnemonic for additions is ADD, The mnemonic for logic AND operation is AND, and The mnemonic for instruction copy data from one location to another is MOV. 71 Assembly Language Programming Assembly Language programs • Gives you a better appreciation of how the hardware works than high-level languages • Resources are given by the programmer’s model • These include registers, hardware resources in CPU, and memory used for data storage. • Look through the instructions in category, find the mnemonic for the required operation, then find the correct addressing mode. The Assembler • Converts (assembles) a source file into binary codes (machine codes) executed by the computer. o 1-to-1 correspondence between assembly language statements and the machine code in 72 memory Assembly Language Programming Assembly Language programs 73 Programming with an Assembler • Steps Lets us see the steps involved in developing and executing assembly language programs. 74 Programming with an Assembler Steps • The first step in the development process is to write an assembly language program. • The assembly language program can be written with an ordinary text editor such as word star, edit, emu8086 editor and so on. • The assembly language program text is an input to the assembler. o The assembler translates assembly language statements to their binary equivalents, usually known as object code. o Time required to translate assembly code to object code is called assemble time. • During assembling process assembler checks for syntax errors and displays them before giving object 75 code module. Programming with an Assembler Steps • • • • • The object code module contains information about where the program or muddle to be loaded in memory. If the object module is to be linked with other separately assembled modules then it contains additional linkage information. At link time, separately assembled modules are combined into one single load module, by the linker. The linker also adds any required initialization or finalization code to allow the operating system to start the program running and to return control to the operating system after the program has completed. At load time, the program loader copies the program into the computer’s main memory, and at execution time, program execution begins. 76 Programming with an Assembler Steps • Assembling process o As mentioned earlier, assembler translates a source file that was created using the editor into machine language such as binary or object code. o The assembler reads the source file of our program from the disk where we saved it after editing. • Linking process o A linker is a program used to join together several object file. o When writing large programs, it is usually much more efficient to divide the large program into smaller modules. Each module can be individually written, tested and debugged. When all the modules work, they can be linked together to form a large functioning program. 77 Programming with an Assembler • Linking process … cont’d o The linker produces a link file which contains the binary codes for all the combined modules. • Debugging Process o A debugger is a program which allows us to load our object code program into system memory, execute the program, and debug it. o How does a debugger help in debugging a program? 1. the debugger allows to look the contents of registers and memory locations after our program runs. 2. It allows us to change the contents of register and memory locations and return the program 3. Some debugger allows us to stop execution after each instruction so we can check or alter memory and register contents. 4. Etc… 78 Assembly Language Example Programs • Please compare these programs with lab manual II 79 Assembly Language Example Programs • Please compare these programs with lab manual II 80