Introduction 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.
3.1 Program development steps:
The first step in writing the program is to think very carefully about the problem that you want to solve with the program. 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: 1. FLOWCHARTS It uses graphical shapes to represent different types of programming operations. Some of the symbols of the flowchart are shown below:
2. Structured programming and pseudo code
Programs should be understandable to other programmers Two approaches top-down and bottom up approach o Top-down: divide the program in to major modules and divide theses major modules in to sub modules and so on. o 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. Example: IF temperature less than70 degree THEN Turn on heater ELSE Turn off heater
Finding the right instructions
After the structure of the program is worked out we need to find the right instructions for the right job. Instructions in 8086 are mainly divided into following categories: DATA TRASFER: General purpose, IO, special and flag instructions. Examples, PUSH, POP, XCHG, IN, LEA, LDS and LAHF etc. are the instructions used for the transfer of data. ARITHMATIC: Addition, subtractions, multiplications and division instructions. Examples, ADD, ADDC, SUB, DEC, NEG, CMP, MUL, DIV, CBW and CWD etc. are instructions supporting arithmetic operations. BIT MANIPULATION: It contains logical instructions, shift instructions and rotate instructions. Examples, NOT, AND, SHR and ROR etc. STRING INSTRUCTIONS PROGRAM EXECUTION TRANSFER: Conditional transfer, iteration control, interrupt, high level language interface. Examples are CALL, RET, JC, JNC, INT and LEAVE etc. PROCESSOR CONTROL: flag set/clear, external hardware sync, no operation instructions. Examples are STC, CLC, HLT, WAIT and NOP etc. 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. 3.2 Constructing the machine codes for 8086 instructions In this you will learn how to construct binary codes for 8086 instructions. Instruction templates The intel literature shows two different formats for coding 8086 instructions. Instruction templates helps you to code the instruction properly. MOD and R/M bits pattern in 8086
Using the above table:
1. 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. 2. 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. 3. 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. 4. 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. Example: MOV instruction coding format MOV instruction coding Examples: MOV SP, BX: this instruction will copy a word from BX register to SP register. MOV CL, [BX]: this instruction will copy the byte to CL from the memory location whose effective address is stored in BX. MOV 43H [SI], DH: copy a byte from DH register to memory location. 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. 3.3 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. SEGMENT and ENDS Directives 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. Naming data and Addresses – EQU, DB, DW and DD Directives Program work with constants, variables and addresses. All of them can be given names so that in the program you can identify them through names. Labels are used to give names to addresses. EQU Directive: used to assign names to constants. DB, DW and DD Directives: used to assign names to variables. Types of Numbers Used in Data Statements BINARY: numbers containing 0 or 1. DECIMAL HEXADECIMAL BCD(Binary Coded Decimal) ASCII Accessing Named Data with the Program Instructions The assembler automatically calculates the address and displacement of the data item written in the instruction. Different types of addressing techniques are employed in the assembler so that programmer need not worry about the address of the data item in the memory. The ASSUME Directive At any given times the 8086 works directly with only 4 physical segments: code, data, stack and extra segment. The ASSUME Directive 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. Any instruction or statements that are written after END will be ignored. 3.4 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 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. 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. 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.