You are on page 1of 17

1 Modular Programming in 8086

UNIT 4. Modular Programming
• Program is composed from several smaller modules. Modules could be developed by separate teams concurrently. The modules are only assembled producing .OBJ modules (Object modules). Each module is produced from a separate Assembly Language program. The .OBJ modules so produced are combined using a LINK program. • • Another concept that is useful is that of a Library of Object Modules. Frequently used procedures could be assembled into object modules and these object modules are placed in a Library file that is linked into the application. Note that only the required object modules are pulled from the Library to be linked to the final application. Ex: Let us say, in Module A , we defined num DB 10 DUP (?) In Module B , we wish to access BUF1, say as in MOV DX, OFFSET num num is not defined in Module B. Thus, when assembling Module B, we will get the assembly error of “Undefined Symbol”. Note that the symbol is actually defined in Module A. But, the two modules are assembled independently! Problem: How to make BUF1, defined in Module A, accessible to Module B? • • Solution: • Declare num as public in Module A (Interpretation: Defined in this module, may be used in other modules.) • Declare num as external in Module B (Interpretation: Used in this module, but defined in some other module.)  num becomes a global symbol.  Its use is resolved by a Linker program.  What happens if num is declared as external in one module; but Linker does not find its definition in any of the modules being linked together? This will be a Link Error, something like: “Globally Unresolved Symbol” Properly used, Public and Extrn facilities allow modules to communicate with each other, which is essential for Modular Programming.

4.1 PUBLIC and EXTRN Directives:
The following example illustrates the use of Public and External directives to achieve communication among independently assembled modules.

PROG1.ASM, defines two symbols BUF1 and BUF2 and declares them as PUBLIC. It also defines a Far Procedure called RDKEY and declares this also as PUBLIC. Such a declaration allows these symbols to be accessed from other modules. Thus, BUF1, BUF2 and RDKEY can be accessed from other modules. File 1: PROG1.ASM .MODEL SMALL .DATA PUBLIC PUBLIC BUF1 DB BUF1 BUF2 10 DUP (?)

Compiled by: L. Krishnananda, Assistant. Professor, REVA ITM, Bangalore

ASM • These can be assembled and Linked by the following command ML PROG1. Similarly. Krishnananda.2 Modular Programming in 8086 BUF2 DW 10 DUP (?) .ASM .OBJ PROG2.DATA EXTRN BUF1:BYTE EXTRN BUF2:WORD . Bangalore .ASM PROG2. • Compiled by: L.  Run LINK utility and specify the two .EXIT END • We now have 2 Files: PROG1. • Note that the type of the EXTRN data items must be declared for proper assembly. 01 INT 21H RDKEY ENDP END .OBJ files. BUF2 and RDKEY. getting the two .ASM and PROG2. Assistant. AX .STARTUP MOV DX. MASM PROG1.OBJ files. LINK PROG1. OFFSET BUF1 MOV CX.CODE EXTRN RDKEY:FAR .CODE .ASM  Now assemble the two programs separately. rather they are expected to be defined in other modules which will be specified during LINK time.OBJ files as inputs. The Library file can be specified at LINK time. function definition here In another program PROG2.ASM. 10 L1: CALL RDKEY STOSB LOOP L1 MOV BUF2.STARTUP PUBLIC RDKEY RDKEY PROC FAR MOV AH.MODEL SMALL . MASM PROG2.OBJ 4. REVA ITM. we make use of the symbols BUF1. Professor. the FAR/NEAR nature of an EXTRN procedure also must be declared for the Assembler to generate correct code.2 LIBRARIES: • Frequently used procedures for a given application domain may be placed in a “Library File” as . We declare them as EXTRN. File 2: PROG2. Such a declaration indicates that the definitions for these symbols are not in this module.

• Replace an existing .OBJ files are extracted from the Library File and linked to the program.LIB and add the module PROG1 to the library: > LIB MYP1. the 3 modules could be added to the Library file as shown below: >LIB MYP1. REVA ITM.LIB. • LIB Command: The LIB command provides the following facilities: • Create a new Library file • Add .OBJ file with the same name (equivalent to Delete followed by Add) The LIB command is used as follows: • LIB library file name If the named Library file does not exist.(remove) . remove the module PROG2: > LIB MYIO1. The Operation can be: + (add) .OBJ file from a Library file. Assistant. some messages are displayed Library file does not exist.3 Modular Programming in 8086 • Only the required .LIB add the module PROG3: >LIB MYP1. .LIB . add the module PROG2: > LIB MYP1. the system prompts whether to create? Type Y It prompts for operation (operation could be specified on command line also). -+ (replace) Example: Create a Library file called MYP1. Thus.LIB .LIB . some messages are displayed from the utility Library file does not exist. Bangalore . some messages are displayed Operations: +PROG3 > Alternatively. • Delete . Libraries provide a powerful reuse mechanism.OBJ file in the Library with another . Create? Y Operations: + PROG1 List File: MYP1 To the Library file called MYP1. Krishnananda. some messages are displayed Operations: +PROG2 > To the Library file called MYP1.OBJ file to a Library file.LIB Copyright messages etc from the utility Operations: -PROG2 >   Compiled by: L.LIB.LIB . Professor. Create? Y Operations: PROG1 + PROG2 + PROG3 List File: MYP1 > From the Library file called MYP1.

though in practice Assemblers impose implementation-dependent limits on the nesting depth. Macros are faster but consume more space. Each directive follows the name of the procedure. Procedure A can call Procedure B which in turn calls Procedure C. specify the Library files required in response to the prompt Libraries [. A procedure may be in a different code segment (Inter segement). In other words. Krishnananda. REVA ITM. NEAR or FAR can be followed by USES statement.lib as the default extension. This is known as NEAR CALL. In such a case. • It is also possible for a Procedure to call itself (a recursive procedure). PROC directive is followed by the type of procedure: NEAR or FAR. the procedure would have an alternative that would not involve recursion. Bangalore • • • • • • .3 Procedures • Procedure or a subroutine or a function is a key concept for modular programming. Professor. Assistant. we specify only IP as relative distance or indirectly as actual value. After completing Procedure C. In such a case. With ML or with LINK. • Further. Of course. nested procedures calls are possible. Thus a Stack would be the most convenient data structure for storing return addresses Procedure . Compiled by: L.lib]: Library files have . Logically. and it can be “called” as many times as needed. As only one copy is stored. Examples of recursive procedures are described in later sessions. A procedure is a reusable set of instructions that has a name. control returns to Procedure A. USES statement allows specification of registers which are automatically pushed onto the stack and popped from the stack within the procedure. This is known as a FAR CALL In program. before transferring control to the procedure. • • • “CALL” transfers control to the procedure like with a jump.4 Modular Programming in 8086 • List file shows: • Sizes and names of the files in the Library • Public labels available in the Library • Once we create Library files. we need to specify both IP and CS (directly or indirectly). procedure has a “RETURN” instruction which returns control to the instruction following the CALL instruction! In order to implement such a return. • 4. • • Only one copy of the procedure is stored in the memory. the necessary information is stored on a stack. the essential way to conquer complexity. but unlike a jump. to avoid infinite regress. such a nesting of calls can be to any level. • We see that return addresses are known in one order and are used for implementing “return” in exactly the reverse order.Related Instructions/Directives: • A procedure may be in the same code segment as that of the main program (Intrasegment). procedure starts with PROC directive and ends with ENDP directive. we can use them to link the required modules into the application program by specifying the Library files to the Linker. it saves memory. control returns to Procedure B and after completing Procedure B. but has execution time overhead for the “call” and “return” operations.

REVA ITM. the target address is directly specified as new CS:IP. procedure definition MULT PROC NEAR USES BX MOV AX. the next two bytes specify the new IP value and the next two bytes specify the new CS value. • Displacement is specified as 16– bit signed integer. .. CALL MULT . . In this case. Target range is thus -32768 bytes to +32767 bytes and consequently. BX RET MULT ENDP … … … CALL SR1 FAR CALL Instruction: • FAR CALL is like FAR JMP in the sense that it can call a procedure available anywhere in the code space. calling a procedure. Professor. target can be anywhere in the current code segment.5 Modular Programming in 8086 Near CALL Instruction: • Similar to Near Jump except that current IP is saved on the stack before transferring control to the new IP with CS remaining the same. Krishnananda.. Assistant. Both current IP and CS are saved on the stack and then control is transferred to the new CS:IP The instruction is 5 – Byte long.. Bangalore . • This Instruction is 3 – Byte long Main program: | . Transfer control to procedure named as MULT . 1 ADD AX. The format is shown below: OPcode • • • IP Low Byte High Byte Low Byte CS High Byte E8 Example: • SUM1 … SUM1 ENDP … PROC FAR … … … … Compiled by: L. The first byte specifies the opcode.

this instruction uses [ ] form of addressing to directly access a table of CALL addresses. FAR PTR directive indicates a far CALL (the table is assumed to contain double words giving CS. a CALL to SUM1 is assembled as FAR CALL. contents of the register are transferred directly into IP (not relative displacement). This form is called Indirect Memory CALL also. Professor. (the table is assumed to contain words giving IP values) Example: CALL FAR PTR [SI] • • • • • • Fetch the double word. 16-Bit register may be used as the operand for the CALL instruction. (Indirect CALL) Again. • Near RET instruction pops a 16-Bit word from the stack and places it in the IP. after popping return address from the stack. at the offset of [SI] from the current code segment and CALL the far procedure at that address. This is always a Near CALL. calls the procedure at 0080H in the current code segment (Near Call). Now the execution of the instruction: CALL BX .4 MACRO  Macros provide several powerful mechanisms useful for the development of generic programs. Example: Assume BX = 0080H. Return from Procedure: • We use a RET instruction to “return” from the called procedure. the specified number (in this case. Krishnananda. • Far RET instruction pops two 16-Bit words from the stack and places them in IP & CS. • In ALP. This is the scheme followed by compilers. 4. Bangalore . Compiled by: L. at the offset of TABLE [SI] from the current code segment and CALL the near procedure at that address. Indirect CALL Using Index: • Like the corresponding JMP instruction. • The control returns to the instruction following the CALL instruction in the calling program. IP values) Otherwise a near CALL is indicated. REVA ITM. RET is written within the procedure. as with JMP. Corresponding to the two varieties of CALL instructions (near & far). using indexed addressing. effectively removing some (in this case 3) words from the stack! These words could have been pushed onto stack before calling. two forms of RET instructions (near & far) exist. before the ENDP directive and the Assembler will automatically select the proper (Near or Far) RET instruction! • Another form: RET immediate value Example: RET 6 In this case.6 Modular Programming in 8086 Now. • • Example: CALL TABLE [SI] Fetch the word. Assistant. CALL with Register Operands: • As with JMP. using indexed addressing. 6) is added to SP.

Another Macro named RETRIEVE is defined here RETRIEVE MACRO POP CX POP BX POP AX ENDM Examples of Macro usage: The following examples illustrate the use of macros. the corresponding code is “inserted” into the source.7 Modular Programming in 8086  MACRO is a group of instructions with a name. Thus the actual Assembler sees the “expanded” source!  We could consider the macro as shorthand for a piece of text. replacing the macro name. Definition of a Macro named SAVE SAVE MACRO PUSH AX PUSH BX PUSH CX ENDM . Bangalore . Execution time overhead is present because of the call and return instructions. We first show the source with macro invocation and then show how the expanded source looks. there is no execution overhead because there are no additional call and return instructions. Macros Vs Procedures: 1) Procedure:    Only one copy exists in memory.1 MACRO Definition: A macro has a name.  When a macro is invoked. “Called” when required. The code is in-place. the associated set of instructions is inserted in place into the source. Krishnananda. Thus multiple copies of the same code exist in the memory leading to greater space requirements. REVA ITM.  No use of stack for operation 4.  Good if few instructions are in the Macro body. MACRO and ENDM. If more lines of code. It should be popped again when control comes back to calling program with RET instruction. better to write a procedure   2) Macro:  When a macro is “invoked”. Return address (IP or CS:IP) is saved on stack before transferring control to the subroutine thro’ CALL instruction. The body of the macro is defined between a pair of directives. Thus memory consumed is less.  However. Compiled by: L. Professor. Assistant. Examples of Macro Definitions: . This “macro expansion” is done by a Macro Preprocessor and it happens before assembly.

When the macro is invoked. AX POP AX ENDM . DA2 ADD AX.1. Example: where a Macro named COPY is defined with two parameters called A and B. REVA ITM. invoke macro using the name . Bangalore . Krishnananda. the expanded source looks as shown below: PUSH AX PUSH BX PUSH CX MOV CX. the dummy parameters are replaced by the corresponding actual parameters. Thus. Thus the first actual parameter is associated with the first dummy parameter. y PUSH AX MOV AX. The association between the dummy and actual parameters is positional. DA1 MOV BX. .ASM file with Macros Macro Preprocessor . BX ADD AX. What gets assembled is the expanded source. These are dummy parameters. Assistant. x MOV b. Example: COPY MACRO x. the second actual parameter with the second dummy one and so on. CX MOV DA2. we provide the actual parameters.1 MACROS with Parameters: A macro can be defined with parameters. macro name does not appear in the expanded source code. Professor. the actual Assembler “does not see” the macros. During the macro expansion. macro named COPY with 2 parameters Compiled by: L.EXE file Assemble and Link 4. DA1 MOV BX. invoke macro  When the Macro Preprocessor expands the macros in the above source.ASM file with Macro Expanded . AX POP CX Expanded MACRO at POP BX the place of invocation POP AX Note how the macro name is replaced by the associated set of instructions. Body of the macro gets substituted at the place where the macro name appears in the program. In other words.8 Modular Programming in 8086 Program with macro invocations: SAVE MOV CX. BX RETRIEVE . DA2 Expanded MACRO at the place of invocation ADD AX.

AL POP DX ENDM • . READ VAR1 gets expanded as: PUSH DX ??0000: MOV AH. try again MOV num. the label Next will be replaced automatically. Thus during the macro expansion. AX POP AX Local Variables in a Macro: • Assume that a macro definition includes a label Next as in the example: READ MACRO num PUSH DX Next: MOV AH. AL POP DX ENDM Now. 0FFH INT 21H JZ Next . No key. This eliminates the problem of multiple definitions in the expanded source. 0FFH INT 21H JZ Next MOV num. with a unique label of the form ?? xxxx . the Assembler sees the label Next at two different places and this results in the “Multiple Definition” error! SOLUTION: Define Next as a local variable in the macro. 06 MOV DL. Professor. VAR2 The expanded code is: PUSH AX MOV AX. REVA ITM. the parameter A is replaced by VAR1 and the parameter B is replaced by VAR2. With the use of local variable as illustrated above. No key. READ MACRO num LOCAL Next PUSH DX Next: MOV AH.. Krishnananda. In other words.. as in READ VAR1 READ VAR2 assembly error !!! The problem is that the label Next appears in the expansion of READ VAR1 as well as in the expansion of READ VAR2. Assistant. COPY VAR1. try again If READ macro is invoked more than once. in each invocation of READ. 0FFH INT 21H • • • • Compiled by: L. Bangalore . where xxxx is a unique number generated by Assembler. VAR2 MOV VAR1. 06 MOV DL. 06 MOV DL.9 Modular Programming in 8086 The macro is invoked in the following code with actual parameters as VAR1 and VAR2.

MAC Summary: • • • Modular programming techniques simplify the software development process MACROS have very powerful features. generated automatically by the Assembler. No key. when displaying a number on the screen. 0FFH INT 21H JZ ??0001 MOV VAR2. for ex. Bangalore . we must produce a sequence of ASCII characters representing the number to be displayed. Macros make it easy to develop Generic Programs which can be easily adapted for specific applications. try again Note how each invocation of the READ macro gets expanded with a new and unique label. REVA ITM. if we write READ VAR2 it gets expanded as: PUSH DX ??0001: MOV AH. Ex: Suppose the Macros are placed in D:\MYAPP\MYMAC.10 Modular Programming in 8086 JZ ??0000 MOV VAR1. Professor. 06 MOV DL.. note that LOCAL directive must immediately follow the MACRO directive. Further. AL POP DX . Further. Thus the given number must be converted to a string of equivalent ASCII characters.. in place of the local variable Next. (a single semicolon). Assistant. No key. Expertise with Modular programming techniques and Macros is essential for developing industry-strength large scale software in Assembly Language. and not as usual by . we write INCLUDE D:\MYAPP\MYMAC. Another feature to note is that Comments in Macros are preceded by . Used properly.MAC • In the source file. they can reduce effort required to develop large and complex programs. AL POP DX Subsequently. (two semicolons) . try again . Krishnananda.. Compiled by: L. • Code Conversions:    •   Algorithm Look –Up Table ASCII to Binary Binary to ASCII BCD to 7-Segment Code Data Conversion may be based on 1) Converting from Binary to ASCII: In many contexts. File of Macros: • We can place all the required Macros in a file of its own and then include the file into the source.

Assistant.CODE . Save 9 • Divide 57 by 10. call procedure Binary to ASCII and Display Mov ah. 37H and 39H as output given 0243H as input • a) Binary to ASCII Algorithm: Example: Binary number: 0000 0010 0100 0011 = 579 D • Divide 579 by 10 . the most convenient technique is to Save & Retrieve the digits using Stack) • While retrieving.11 Modular Programming in 8086 • Example: Binary number: 0100 0011 = 43H = 67 D To display this on the screen. Krishnananda. Remainder = 9 . Test value CALL B2A . Quotient = 0 . or save it…) • Thus the algorithm is: While the number is not equal to 0: Divide the number by 10. we need to produce 35H. ASCII code for character ‘7’ is 37H.MODEL TINY . Output: Equivalent ASCII displayed on screen . ASCII code for character ‘5’ is 35H.STARTUP MOV AX. add 30H to convert the digit to ASCII code and then display it (or print it. we need Three ASCII characters. REVA ITM. Set number <. Input : 16-Bit Binary Number in AX . Remainder = 7 . ‘6’ and ‘7’. Remainder = 5 . • Retrieve remainders in the order of 5. 7. Bangalore . Save 5 • Quotient = 0  Conversion Complete. we need to convert this binary number in to Two ASCII characters. 4ch . (As the order of retrieval is the reverse of the order of producing these digits. 7. and 5. This algorithm is implemented in the following program:  Binary to ASCII Program: . terminate main program Compiled by: L. ‘5’. Another Example: Binary number: 0000 0010 0100 0011 = 0243H = 579 D To display this on the screen. ‘7’ and ‘9’. Save 7 • Divide 5 by 10. and ASCII code for character ‘9’ is 39H So. Quotient = 57 . 2A5H . and 9.quotient While stack not empty Pop a digit from the stack Add 30H to covert it to ASCII and display it Return. • Remainders saved in the order of 9. we need to produce 36H and 37H as output given 43H as input. Push the remainder digit on the stack. Quotient = 5 . ASCII code for character ‘6’ is 36H and ASCII code for character ‘7’ is 37H. So. Professor.

continue division . REVA ITM. their count maintained in CX is incremented. 06H INT 21H LOOP back2 . Thus the POPs occur in a loop controlled by CX. The number to be converted (and subsequently. Dividend in DX. Example: AX = 0027H (39 Decimal) • Compiled by: L. Conversion completed ? (Quotient. So. 30H . No.12 Modular Programming in 8086 Int 21h B2A PROC PUSH PUSH PUSH MOV MOV MOV DIV PUSH INC OR JNZ NEAR DX CX BX CX. Save remainder digit on the stack . the quotient) is in AX. simpler method exists. Retrieve remainder in DL . b) Another Method for Binary to ASCII Conversion: When the input number is less than 100. Clean up & Return. an alternative. So set DX = 0 . Count of remainder digits is in CX back2: POP ADD DX DL. Divide by 10 . Initialized to 0 . Convert to ASCII . AX is destroyed POP POP POP RET B2A ENDP END Notes:  DIV instruction requires the 32-bit dividend in the register pair DX and AX. Count of ASCII digits. 10 DX. AX. Conversion is complete as quotient in AX = 0 . DX is cleared to 0 to set up the correct dividend. i. In the same loop. Increment digit count . • AAM (ASCII Adjust AX After Multiplication) instruction converts value in AX in to 2-Digit • Unpacked BCD and leaves it in AX. 0 BX. 0 BX DX CX AX. As the digits are pushed on to the Stack. Console Display Function . AX back1 back1: . 30H is added to produce the output in ASCII. Repeat for all digits MOV AH. Assistant. DOS Service. Professor.e AX = 0 ?) . Divisor is 10 . Bangalore BX CX DX  . display digit . Krishnananda. as we POP each digit.

Display second digit . 06H . Assistant. This is Packed ASCII representation.EXIT B2A PROC NEAR PUSH DX MOV AH.CODE . Yes. 3030H PUSH AX . Bangalore . The following program is developed based on this idea. AH . Display first digit POP AX . else. • Works only when the number is less than 100 as the maximum unpacked BCD that we can have in the AX register is 0909H only. add 3030H to AX to get 3339H . Now.STARTUP MOV AL. Krishnananda. Display Function INT 21H . AH MOV AH. AX = Unpacked BCD . Now. We need to modify the previous program to incorporate this check for a leading 0. AL . Second Digit MOV AH. REVA ITM. 0 AAM ADD AX. Output: Equivalent ASCII displayed on screen .13 Modular Programming in 8086 Execute AAM . display the digit. display 20H (blank). AX = 0309H . Retrieve value MOV DL. Now. First Digit MOV AH. Assumed <100 . AX = Number . Input : Binary Number in AL. • . AX is destroyed POP DX RET B2A ENDP END Refinements: • Suppose the input is: AL = 7H. • Separate the two bytes (unpack) to get the two ASCII characters representing the given number • (33H and 39H). Convert to ASCII MOV DL. Clean up & Return. the display should be 7. unpack and display . 06H . This is Unpacked BCD. We need to check if the first digit is 0. Test value . Old Code for displaying first digit: MOV DL. 2AH . Binary to ASCII and Display CALL B2A . 06H . Display Function •   Compiled by: L. instead of 07. Professor. First Digit .MODEL TINY . If so. Display Function INT 21H . What is displayed is 07 • Can we replace leading 0 with a blank so that the display looks better? Thus.

unpack and display ADD AH. AH AH. the program will be as shown below: . 20H CMP AH. 10H B2A1: MOV DL . Professor. Compiled by: L. No. REVA ITM. Now. Retrieve value . Display Function . 2AH CALL B2A Mov ah. 10H back1: MOV MOV INT POP MOV MOV INT DL.14 Modular Programming in 8086 INT  ADD AH. First Digit . AX is destroyed POP DX RET B2A ENDP END 2) ASCII to Binary Algorithm: In many contexts. AX = Unpacked BCD . Assistant. First Digit itself if not 0 . AX = Number . 06H INT 21H  . 4ch Int 21h B2A PROC NEAR PUSH DX MOV AH. Output: Equivalent ASCII displayed on screen . Display Function . Display blank (ASCII Code is 20H) . 20H CMP AH. Display Function . 06H 21H . Or Blank (if 0) .STARTUP MOV AL. we have already added 20H. This string of ASCII characters must be converted to the equivalent number for further processing. 20H JZ B2A1 ADD AH. Clean up & Return. YES. 0 AAM ADD AX. display a blank (ASCII Code is 20H) . AL AH. Assumed <100 . Convert to ASCII . Display first digit Revised Code for displaying first digit: Incorporating this change. for example. Add 10H more to get the correct ASCII Code for the digit . Add 10H more to get the correct ASCII Code for the digit . 20h is the ASCII code for “blank” . Bangalore . Binary to ASCII and Display .CODE . AH MOV AH . Second Digit . when reading a number from the key board. 20H JZ back1 ADD AH. Display first digit .MODEL TINY . 3030H PUSH AX . Input : Binary Number in AL. 06H 21H AX DL. So. First Digit = 0? . Test value . we get a sequence of ASCII characters representing the number. Display second digit . Krishnananda. First Digit = 0? . Display first digit 21H .

0 . • • 3 characters.bit addition. Read Key with Echo INT 21H .EXIT RDNUM PROC NEAR PUSH BX PUSH CX MOV CX. the following program implements the ASCII to Binary Conversion.CODE . Result assumed not to exceed 16 bits! . and 36H. Result initialized to 0 RDN1: MOV AH. Subtract 30H to get corresponding BCD digit 06H. Result = Result * 10 + Next BCD Digit Result = 01 * 10 + 05 = 0000 0000 0000 1111 Next ASCII digit 36H. Result returned in AX. ASCII characters representing a number are read from key board. REVA ITM. Assistant. ‘1’. • • • • • • • . Subtract 30H to get corresponding BCD digit 01H. Result = Result * 10 + Next BCD Digit Result = 15 * 10 + 06 = 0000 0000 1001 1100 ASCII digits exhausted. ’0’ JB RDN2 CMP AL. Professor. Krishnananda. Bangalore . ASCII to Binary Program . . Result = Result * 10 + Next BCD Digit Result = 0 * 10 + 01 = 0000 0000 0000 0001 Next ASCII digit 35H. which is then stored in memory location TEMP. Multiplier is 10 MOV BX. Final Result = 0000 0000 1001 1100 = 009CH = 156 (decimal) Based on the above ideas. signals the end of the number entry . 10 . 35H. and ‘6’. Converted Binary Value must be: 0000 0000 1001 1100 = 009CH = 156 (decimal) Conversion Procedure: • • • Start with (Binary) Result = 0 First ASCII digit 31H.STARTUP CALL RDNUM MOV TEMP.15 Modular Programming in 8086 Example: Assume that ASCII character sequence ‘156’ is the input. If less than ‘0’ or greater than ‘9’ Number entry is over CMP AL. 1 . with codes as 31H. AX .MODEL SMALL . Subtract 30H to get corresponding BCD digit 05H. So. . ‘5’. 32.’9’ Compiled by: L. conversion is completed. The first non-digit character (any character other than 0 through 9) typed .DATA TEMP DW ? . Program can be modified to accept larger numbers by implementing . Check the character. .

REVA ITM. 30H AX AX.digit. in the calling program. Result in AX RDNUM Notes:  The constant multiplier 10 is held in the register CX. the result is accumulated in the register BX and at the end. BCD to 7-Segment Code Program Compiled by: L. we are assuming a Common – Anode type. XLAT can be used if table has up to 256 byte-entries Value to be converted is used to index in to the table containing conversion values. The result in AX is moved. BX CX BX. AX RDN1 . The 7 segments and the decimal point are controlled by 8 bits. Krishnananda. we will demonstrate BCD to 7-Segment code conversion. Based on the digit to be displayed. a look-up table simplifies data conversion. This part of the code must be changed to implement 32-bit addition if larger results are to be supported. Bangalore . Update Result . Update Result SUB PUSH MOV MUL MOV POP MOV ADD JMP AL. Assistant. AX AX AH. the following FAR Procedure returns the 7-Segment code in the AL register. Based on the above logic. The bit value required to switch on a segment depends on whether the device is of a Common – Anode type or Common – Cathode type. AH is cleared to 0 so that the 16-bit value in AX represents the correct value and thus can be added directly to the accumulating result in BX. Thus the segment will be ON if the corresponding controlling bit is 1 and will be off if the bit is 0. BCD Digit . corresponding to the BCD digit provided as input parameter in the AL register before calling the procedure. The BCD digit is in AL. 0 BX. BX POP CX POP BX RET ENDP END . Clean Up and Return RDN2: MOV AX. with one bit controlling one segment or the decimal point.  In the procedure RDNUM. 1) BCD to 7-Segment Code Conversion: In many applications. AX = Current Digit . Non.16 Modular Programming in 8086 JA RDN2 .  Using Look–Up Tables for Data Conversion: • • • • Often. it is moved in to register AX. we must determine the segments that must be ON and the ones that must be OFF. Result = Result * 10 . we need to display BCD values on a 7-Segment display. Is digit. in to the memory location TEMP. As an example. Here. Professor. Repeat . .

6DH. Output: AL = 7-Segment code. REVA ITM. Input: . 6FH . BT7SEG PROC FAR PUSH BX MOV BX. 06H. So segment override prefix is being specified. AL = BCD Digit Compiled by: L. Here we are using the operand (TABLE). 66H. 7DH. Bangalore . 5BH.17 Modular Programming in 8086 . Krishnananda. Professor. Here the table is in CS. OFFSET TABLE XLAT CS: TABLE POP BX RET TABLE DB 3FH. Assistant. code corresponds to digits 0 to 9 BT7SEG ENDP Notes: • XLAT instruction does not normally contain an operand. XLAT uses DS by default. It is a dummy operand! It is being used here only to specify segment override. 07H. 7FH. 4FH.