Chapter 3 - 8086 family assembly language programming - introduction from Microprocessors and Interfacing by Douglas Hall Objectives • At the end of this chapter you will learn: 1. Write a task list, flow chart or pseudo code for simple programming problem. 2. Write code, assemble and run a very simple assembly language program. 3. Describe the use of program development tools such as editors, assemblers, linkers, locators, debuggers and emulators. 4. Properly document assembly language programs Outline • • • Program Development Steps • Representing program operations • Finding the right instructions • Writing a program Constructing the machine codes for 8086 instructions • Instruction templates • MOD and R/M Bit patterns in 8086 • Tips for hand coding Writing programs for the use with assemblers Program Development Steps • • The problem that you want to solve Just ask yourself question that “What do I really want this program to do?” Representing program operations • • Formula or sequence of operations used to solve a programming problem is called as the algorithm. There are two ways of representing algorithms: • Flowchart • Structured programming and pseudo code Flowchart • • It uses graphical shapes to represent different types of programming operations. Flow chart symbols Structured programming and pseudo code • Programs should be understandable to other programmers • Two approaches top-down and bottom up approach • • • • • Top-down: divide the program in to major modules and divide theses major modules in to sub modules and so on. Bottom-up: each programmer writes the low level module code and hopes that all pieces will eventually fit together. Use set of three to seven standard structures. Main structures are only three viz. IF-THEN-ELSE, SEQUENCE & WHILE-DO. The others are IF-THEN, CASE & REPEAT-UNTIL. Finding the right instructions • Instructions in 8086 are mainly divided into following categories • Data Transfer Instructions • Arithmetic Instruction • Bit manipulation Instruction • String Instruction • Program execution transfer Instruction • Processor control Instruction Writing a program • You need to do the following to write the program effectively: • • • INITIALIZATION INSTRUCTIONS: used to initialize various parts of the program like segment registers, flags and programmable port devices. STANDARD PROGRAM FORMAT: it’s a tabular format containing ADDRESS, DATA OR CODE, LABELS, MNEM, OPERAND(S) and COMMENTS as the columns. DOCUMENTATION: you should document the program. E.g. each page of the document contains page number and name of the program, heading block containing the abstract about the program, comments should be added wherever necessary. Constructing the machine codes for 8086 instructions • Each instruction in 8086 is associated with the binary code. • You need to locate the codes appropriately. • Most of the time this work will be done by assembler • The things needed to keep in mind is: • Instruction templates and coding formats • MOD and R/M Bit patterns for particular instruction Instruction template • • The Intel literature shows two different formats for coding 8086 instructions. Instruction templates helps you to code the instruction properly. Coding Template for 8086 IN Instruction MOD and R/M Bit patterns in 8086 (contd.) MOD and R/M Bit patterns in 8086 - Use • • • • If the other operand in the instruction is also one of the eight register then put in 11 for MOD bits in the instruction code. If the other operand is memory location, there are 24 ways of specifying how the execution unit should compute the effective address of the operand in the main memory. If the effective address specified in the instruction contains displacement less than 256 along with the reference to the contents of the register then put in 01 as the MOD bits. If the expression for the effective address contains a displacement which is too large to fit in 8 bits then out in 10 in MOD bits. MOV instruction coding format(contd.) • MOV SP, BX: this instruction will copy a word from BX register to SP register. MOV instruction coding format(contd.) • MOV 43H [SI], DH: copy a byte from DH register to memory location. MOV instruction coding format • MOV CX, [437AH]: copy the contents of the two memory locations to the register CX. Tips for Hand Coding 8086 Programs • • • • Check your algorithm very carefully to make sure that it really does what it supposed to do. Initially write down assembly language statements and comments for your program. Recheck each statement to make sure that you have right instructions to implement your algorithm. Finally work out the binary code needed by each instruction and execute you program. Writing programs for use with and assembler • Allows you to refer the data items by names rather than their offsets. • How to write the programs so that it can be used by the assemblers. Program Format • There are assembler level directives available to the programmer so that he can write programs which are compatible with the assembler. Sample formatted program Assembler Directives(contd.) • There are different directives available for assembler each with their unique purpose. • SEGMENT and ENDS Directives • Naming data and Addresses – EQU, DB, DW and DD Directives • The ASSUME Directive • The END Directive Assembler Directives(contd.) • SEGMENT and ENDS Directive: • • The SEGMENT and ENDS directives are used to identify a group of data items or group of instructions that you want to be put together in a particular segment. A logical segment is not usually given starting address when it is declared. • EQU Directive: used to assign names to constants. • DB, DW and DD Directives: used to assign names to variables. Assembler Directives(contd.) • The ASSUME Directive • • • 4 physical segments: code, data, stack and extra segment tells which logical segment to use for each of the physical segment at a given time. The END Directive • Tells the assembler to stop reading • instruction or statements that are written after END will be ignored. Assembly Language Program Development Tools • There are several tools available to support the programmer of assembly language for better experience of programming. They are discussed in brief here: • Editor • Assembler • Linker • Locators • Debuggers • Emulators Editor • • • It is the program which allows you to create a file containing the assembly language statements for your program. Examples are PC Write, Word stars and the editors that comes with assemblers. Creates Source file to be processed by the assembler Assembler • • • • It is the program which is used to transfer the assembly language mnemonics to corresponding binary codes. It works in passes. In the first pass, it determines the displacement of the named data items, the offset of labels etc. and puts this information to in a symbol table. In the second pass, it produces the binary codes for each instructions and inserts the offsets etc. that it calculated in the first pass. It generates two files namely object file (.OBJ) and assembler list file (.LST). Linker • • • It is a program used to join several object files into large object files. While writing large programs it is good to divide them into modules so that each modules can be written, tested, debugged independently and then use linker to combine the modules to form the actual program. It produces two files link file which contains the binary codes of all the combined modules and a link map file which contains the address information about the linked files. Locators • A locator is the program used to assign the specific addresses of where the segments of object code are to be loaded in to main memory. • Examples include EXE2BIN which comes with the IBM PC DOS. • Converts .exe to .bin files which has physical addresses Debugger • • • • • A debugger is the program which allows you to load your object code program in to system memory. It allows you to look at the contents of the registers and memory locations after your program runs. It also allows you to set breakpoints at any points in the program. It simply allows you to find the source of the problem into the program. There are lots of debuggers available like Borland Turbo Debugger, Microsoft’s Code view debugger etc. Emulators • One way to run your program • It is mixture of hardware and software. • Generally used to test the hardware and software of an external system such as microprocessor based instruments. Applied Microsystems ES 1800 16-bit emulator