SECTION 2
ASSEMBLY LANGUAGE PROGRAMMING
Structure
Page No.
2.0
Introduction
14
2.1
Objectives
14
2.2
Assemblers
15
2.2.1
Turbo Assembler ( Assembler (TASM)
15
2.2.2
MASM
16
2.2.3
Emu 8086
18
2.2.4
The DEBUG Program
20
2.3
Assembly Programming File
22
2.4
Session-wise List of Programs
23
2.5
What Next? What Next?
28
2.6
Summary
28
2.0
INTRODUCTION
This guide is an attempt att empt to familiarize you with some of the important Assemblers available in the Windows environment. You may use any of thes e tools available as per your study center. This practical session also contains several sample programs that you may need to run/debug at your study center. Some minor mistakes have been created purposely. In order order to run the program, you you must correct those errors. errors. You may may also find that assembler directives used by these programs may differ. You need t o look into such details. You must also attempt attempt the unresolved problems problems in order to gain the maximum maximum from this course. Remember, assembly and C Programming helps you greatly in System Software implementation and giving understanding of the machine. We hope you will enjoy these practicals.
2.1
OBJECTIVES
After going through this section, you should be able a ble :
14
·
develop and assemble assembly programs;
·
identify and use proper assembler directives;
·
design simple assembly programs;
·
write programs that interface with a programming language;
·
appreciate the System Software development environment; environment; and a nd
·
appreciate a keyboard keyboard driver.
Assembly Language Language Programming Programming
2.2 ASSEMBLERS ASSEMBLERS Assembler Assembler is the Program that supports an environment for translating assembly language programs to Machine executable executable files, that is, an assembly program program containing statements statements like MOV AL, 10h and directives like MODEL SMALL, which are meaningless to the microprocessor and so are converted to an equivalent machine program. Thus, the assembler program is a translator that does almost a similar type of work as a compiler. But, how is a compiler compiler different than an a n assembler? One of the major differences to note here is that each high level statement of a C program on compilation will produce many machine statements; whereas an assembly instruction is generally translated to single instruction. We would like to reiterate reiterat e here that assembly and machine languages is machine dependent and programs written in assembly for one microprocessor may not run on other microprocessors. An assembler, assembler, generally, generally, converts an assembly program to an executable executable file. There are two two stand standard ard form formss of execu xecutab table le file filess on DOS. DOS. Thes Thesee are: are: .com .com file filess and .exe .exe file files. s. These These files files basicall basically y diffe differr in form format as per per the segmen segments ts of the 8086 Micro Micropro proces cessor sor. (Please refer to Unit 1 of Block 4 of MCS – 012). The steps of t he assembly process are: Step
Result
Assemble the source program using
·
Creates an object file with extension .obj.
an assembler
·
Creates an optional listing file (.lst) and a file for cross reference
Link the Object file or files fi les
·
Creates an executable (.exe) file
·
Creates optional map file (.map) and library file (.lib)
Convert the executable files to com
·
This is an optional step
file which are fast There are many assemblers that support the above tasks in i n different ways; even the options available with them are quite different. Let us discuss the basic options available with the commonly used assemblers/interfaces for running assembly programs. You may use any of the following depending on the availability of tools at your center.
2.2.1
Turbo Assembler (TASM)
Assembling
Turbo Assembler allows a user to assemble multiple files. Each file may be assigned its own options in a single command line. TASM allows you to use * and ? wild cards, as they they exist xist in DOS. DOS. For For exam example ple, to assem assemble ble all all the progr program amss havi having ng file file name names like like progam1.asm, progam1.asm, program2.asm, program3.asm, program3.asm, you may may just give the command: command: TASM program? program? (.asm extension is is the default extension). extension). The turbo assembler allows you to assemble programs in groups. Each group is separated by a + sign. Two of the most common common options for turbo assembler assembler are: 15
Lab Course Course
/L Generates the list file (.lst) /Z displays source line having having errors. A common use of command line may be: TASM /Z program1 Newer and advanced advanced versions of these assemblers assemblers are also available. available. Cross-Reference Cross-Reference Files : On assembling a program, a cross-reference file of the
programs labels, labels, symbols, and variables variables can be created with an extension extension of .xrf. You can use TCREF command to convert this listing to a sorted cross-reference cross-reference file. For more details please refer to Turbo Assembler help. Linking : The command line for linking a TASM program is:
TLINK object_filename, object_filename, executable_filename [,map_filename] [,library_filename] The default extension for object filename is .obj ·
The executable file is created with extension .exe file.
·
Map file have a .map extension. The map file is used to indicate the relative location, size of each segment and any an y errors that the linker has found. The map file can also be displayed on screen. screen. You need to enter con (for console), console), for such display. display.
·
Library file is used for specifying library option.
Converting Converting Object Files to .COM Programs : TLINK command allows you to convert
an object program directly to .COM format: TLINK /T object_filename, com_filename, con Debugging Options : You can use Turbo Debugger by using the /ZI command command line
option on the assembler.
2.2.2
MASM
There are many versions available with the Microsoft MASM. Let us discuss the two latest versions of it. MASM 6.1
This is one of the versions of Assemblers. It accepts commands of the older versions also. The command that can be used for assembling the program is ML command. This command command has the following format: ML [options] filenames_containing.asm [ [options] filenames.asm] [/link options] Please note that in the above command the terms enclosed within [ ] are optional. The options start with a / and some common options are: /AT 16
/c
To directly convert the assembled file to .com program Assemble the file but do not link it (separate assembly)
/Fl
Generate a listing (.lst) file
/Zd
Include line number in debugging information
Assembly Language Language Programming Programming
Some simple examples of usage of ML command may be: ML /c firstprogram.asm This command will only assemble the file. ML /AT/Zd first.asm
second.asm
This program will create .com file after assembling and linking the two files. MASM 5.1
This is an old version of Microsoft assembler. It requires separate steps for assembling, linking, and converting into .com file. The Th e command command line li ne format for this assembler is: MASM [options] source.asm [,objectfilename [,objectfil ename.obj] .obj] [,listfilename.lst] [,crossreffilename] You need not specify the .asm extension in the above format, as it is taken by default, similarly, you need not assign as sign .obj and .lst extensions which are assigned by default. Each file may have its own path and filename, which may be different from the source file path. The following command creates object and cross-reference files with the same name with the suitable extension. extension. MASM filename, The options relating to MASM are: /L
To create a listing (.lst) file
/Z
To display source lines having errors on the screen
/ZI
Include line-number and symbolic information in the object file
For getting further explanation on these options using the help of MASM, please type: MASM /H. Cross-Reference Cross-Reference Files : On assembling a program, a cross-reference file of the
programs labels, labels, symbols, and variables variables can be created with an extension extension of .crf. For more details please refer to Macro Assembler help. Linking : The command line for linking a MASM M ASM 5.1 program is:
LINK object_filename, executable_filename [, map_filename] map_filename] [,library_filen [,librar y_filename] ame] Most of the option and file fi le names as above are tthe he same as that of TASM. You can also link more than one object files to one executable executa ble file by using the command like: LINK prosgram1 + program p rogram2 2 + program3 17
Lab Course Course
Converting MASM 5.1 Object Files to .COM Programs. The EXE2BIN progr am am
available in DOS converts .EXE modules generated by MASM into .COM . COM modules. Assembler Assembler Tables
The important tables of assemblers that are available a vailable in the .lst listings are: a re: Segments and Groups Table: The following details are contained in this table. Name
Length
Alignment
Combine
Segment Class
types
Provides the
Provides the
Provides
names of all the
size, in hex, of
alignment type,
defined
segment names,
segments and
each segment
such such as BYTE BYTE,
combine type,
as coded in the
groups in
WORD,
alphabetical
PARA.
order.
the Lists the
or such as STACK,
Lists the
SEGMENT statements
NONE when no type is coded, etc.
Symbol table: This table contains the following details: Name
Type column
Value column
Attribute column
Lists the names of L NEAR or L FAR
Provides the hex
Lists the attributes
all defined items, in (Specifies a near or
offset from the
of a symbol,
alphabetical order
far label)
beginning of a
including its
N PROC or F
segment for names,
segment and length.
PROC (Specifies a
labels, and
near or far
procedures.
Procedure) BYTE, WORD, DWORD, etc. (Specify a data item) etc.
2.2.3
Emu 8086
This is an emulator Programming that can be used for executing/testing/ emulating 8086 programs. The Program was available at website website http://www.emu8086.com http://www.emu8086.com.
This
program is priced. priced. The following are some of of the salient points of this package: ·
It contains a source editor, assembler, dis-assembler, and software emulator for assembly program with debugger, and step-by-step tutorials.
·
It is helpful for those t hose who just begin to study assembly language. It compiles c ompiles the source code and executes it on emulator step by step. This is a very useful feature.
18
You must see the changed values with each step. The emulator has an easy Visual
inter face, face, which allows watching registers, flags and memory contents during the program program execution. You can also watch stack. ·
Assembly Language Language Programming Programming
Arithmetic & Logical Unit (ALU) shows the last operation executed by ALU, especially arithmetic operations like addition. This will enhance your understanding.
·
Please note, as the 8086 machine code is fully compatible with all the next generations of Intel's microprocessors, including Pentium II and Pentium 4. This 8086 code is very portable, since it runs both on old and on the modern computer systems. Another advantage of 8086 instruction set is that it is smaller, and thus easier to learn.
How to start Emu 8086?
1. Start Emu8086 by selecting the ICON from the t he desktop, or by running Emu8086.exe. Emu8086.exe. The following window appears:
2. For referri referring ng to already already stored stored samples samples you you can select select sample sample button. button. 3. You can can use your own own program programss also stored in suitable suitable .asm files. files. 4. You You can compile compile or emul emulate ate the the Progr Program am. 5. Click Click Single Single Step Step button button and watch watch how how the code is being being execut executed. ed. Using Emulator
If you want to load your code into the emulator to watch the effect of step by step execution on registers, flags, stach etc you must select the emulate option this is a great learning tool; just click 'Emulate' button
. On selecting emulate button you will see
the following window.
19
Lab Course Course
You can press the buttons at the bottom to watch other windows also along with your program. Please notice even memory offset values as shown s hown and effect can be seen over them also. You can use single step button butt on to execute next instruction. Watch the effect of this single step on Instruction Pointer (IP), and other registers. If you double click on register text-boxes it will open 'Extended Viewer' window with value of that register converted to all possible forms. You can change the value of the register directly in this window. Double click on memory list item; it will open 'Extended Viewer' with WORD value loaded from memory at selected location. Lower byte is at lower address that is: LOW BYTE is loaded from selected position and HIGH BYTE from next memory address. You can change the value of the memory word directly in the 'Extended Viewer' window. You can change the values of registers on runtime by typing over the existing values shown. Flags button allows viewing and showing the flags set by the last ALU operations. The ALU button shows the ALU temporary register. The stack button shows the current stack values.
2.2.4
The DEBUG Program Program
The DOS DEBUG program is a useful tool for writing and debugging assembly programs. This This also allows for examining the contents of a file or memory. memory. DEBUG.EXE is available in DOS in a directory named DOS or in Windows 95/98 by selecting the 20
MS-DOS MS-DOS prompt from Start Menu. You may run DEBUG in a window. You can also use cut and paste paste through clipboard.
Assembly Language Language Programming Programming
Starting Debugger : type DEBUG and press
DEBUG DEBUG starts and a prompt, prompt, a hyphen hyphen (-), appears on the screen. DEBUG is now r eady to accept your commands. The following simple options exist for starting DEBUG: 1. To create create a file or examine examine memory, memory, type type just just DEBUG DEBUG 2. To modify modify or debug debug a program program (.COM (.COM or .EXE) .EXE) type DEBUG DEBUG
2.4
SESSION WISE LIST OF PROGRAMS
The total number of sessions allotted for Circuit Design and Assembly Prgramming are 10. Out of these, the first two sessions have already been covered in Section 2: Circuit Design. Thus, in this section we will have only 8 sessions; numbered 3 to 10. 1 0. Write the following program in 8086 assembly language. Sessions 3 and 4: Simple Assembly Programs (2 sessions & 14 programs)
1.
Write a program to add two numbers present in two consecutive memory locations and store the result in next memory location.
23
Lab Course Course
Memory Number1 Number2 Result
2.
Develop program to read a character charac ter from console and echo it.
3.
Develop and execute a program to read 10 chars from console.
4.
Write a program to exchange two memory variables using MOV and XCHG instruction. Can you do it with just XCHG?
5.
Write a program to find the sum of two BCD numbers stored in memory.
6.
Write a program, which will read two t wo decimal numbers, then multiply them together, and finally print out the result (in decimal).
7.
Write a program to convert the ASCII code to its BCD equivalent.
8.
Write a program, which will read in i n two decimal inputs and print out their sum, in decimal.
9.
Write a program, which will read in i n two decimal inputs and print out the smaller of the two, in decimal.
10.
Write a program to calculate the average of three given numbers stored in memory.
11.
Write a program in 8086 assembly language to find the volume of sphere using following formula: V = 4/3p r 3
12.
Write a program to evaluates evaluates 3 * (x^3) + 4x + 5 if flag flag = = 1 or evaluates evaluates 7x + 8 if flag = = 0.Assume x is a 16-bit unsigned integer.
13.
Write a program to convert Centigrade (Celsius) to Fahrenheit Fa hrenheit temperature measuring scales. Using formula: Celsius = (Fahrenheit - 32) * 5 / 9
14.
Write a Program which adds the sales tax in the Price list of items and replace the Price list with a new list.
Sessions 5, 6 and 7: Loop and Comparisons (3 sessions & 21 programs)
24
1.
Write a program to find the factorial fact orial of decimal number given by user.
2.
Write a program to find nCr for a given n and r.
3.
Write a program to arrange given N numbers in descending order.
4.
Write a program, which will read in decimal inputs repeatedly until a zero value is read; read; at this point, it should print out the sum of the numbers numbers read in so far.
5.
Assembly Language Language Programming Programming
Develop and execute an assembly language program to find the LCM of two 16-bit unsigned integers.
6.
Develop and execute an assembly language program to find the HCF of two unsigned 16-bit numbers.
7.
Write a program for finding the largest number number in an a n array of 10 1 0 elements.
8.
Develop and execute a program to sort a given set of 8-bit unsigned integers into ascending order.
9.
Develop and execute an assembly language program to sort a given set of 16-bit unsigned integers into descending order.
10.
Write a Program which adds adds the sales tax in the Price list of items and replace the Price list with calculated values.
11.
Write a program to Convert ASCII number into decimal digit.
12.
Write a Program for performing the following operation. Z=((A-B)/10*C) Z=((A-B)/10*C) ** 2
13.
Write a Program for adding an array of Binary Digits.
14.
Write a Program, which takes the input of 4-digit number, and display the sum of square of digits as given below. Example: Input = 4721 42+72+22+12 = 16+49+4+1 Result = 70. (Display)
15.
Using the method of 'add-and-shift' loop, in which you use the binary digits of one number to control additions of a shifted version of the other number into a running total; this is essentially the same sa me algorithm you use when multiplying numbers by hand in decimal.
25
Lab Course Course
16.
Write Write a Program Program, which which sho should uld a dds dds two 5-byte num numbe bers rs (number (numberss are stored stored in array- NUM1 & NUM2), and stores the sum in another array arra y named RESULT.
17.
Write a program which should convert 4 digits BCD number into its binary equivalent.
18.
Write a program to conduct a binary search on a given sorted array of 16-bit, unsigned integers, and a given 16-bit unsigned key.
19.
Write Write a progra program m to conver convertt a strin string g in upper case to lower lower case or lower lower case to upper case.
20.
Develop cryptographic algorithm where each letter is replaced by a different letter. Given the mapping of characters to encoded characters, it is simple to translate from encoded to decoded data. Write a Program, Program, which which encodes the string into the ASCII value but not corresponding ASCII value; shift 5 place left in ASCII and write the encoding string.
21.
Similarly write another Program to Decoding with respect to above problem.
Session 8: Strings (1 session and 7 programs)
1.
Write a program, which takes two t wo inputs as strings and display the Concatenated string.
2.
Write a program, which converts converts string lower case characters to upper u pper case characters and upper case characters charact ers to lower case characters.
3.
Write a program for reversing a given string.
4. 5.
Write a program, which converts string to its ASCII value and store in array. Write a program to find if two strings are equal length: and if the strings str ings are found to be of equivalent length then are they the same, if not the same then which string is lexicographically greater.
6.
Write a program to determine a given string is a palindrome. If 'Yes' output the message “The given string is a palindrome”. If 'No' output the message “No, it is not a palindrome”.
7.
Write a program to search for a character in a given string and calculate cal culate the number of occurrences of the character in the given string.
Session 9: Procedural call and Interrupts (1 session & 7 programs)
1.
Write a program program that will will compute compute a grade grade for this this class based on grades input into it. Write two different procedures one for computing total marks based of different examinations held and another for computing overall grade of student
26
Procedures-I: Procedures-I: The total marks will be computed as follows: follows: 20%
Midterm
Exam
20%
Final
Project
Assembly Language Language Programming Programming
30% Quizzes 30% Projects Procedure-II: The letter grade will be computed from the overall grade as follows: 93+: A 90+: A87+: B+ 83+: B 80+: B77+: C+ 73+: C 70+: C65+: D 0+: F 2. Write a Drive Drive detection detection program. The The program should determines determines whether whether the drives. a. Exist b. Are removable or fixed fixed c. Are local local, rem remote, ote, or share shared d d. Are a floppy floppy, hard, hard, RAM RAM or or CD-ROM CD-ROM drive drive 3.
Writ Writee a prog program ram, wh which will will produ produce ce 2 ms ms del delay. ay.
4.
Write Write a prog program ram to displ display ay the curren currentt system system time time using using DOS DOS INT INT 21H, 21H, functio function n 4CH.
5.
Write Write a proced procedure ure, which which takes takes one one charact character er from from conso console le at 10-se 10-second cond interval intervals, s, and stores each character in one array of characters.
6.
Write Write a prog program ram, which which will will gene generate rate an inter interrupt rupt when when the there re is a divi divisio sion n by zero. zero.
7.
Write Write a prog program ram to imple implemen mentt characte characterr array, array, whi which ch can store store only only the the charact character er values in the array and a nd using this array try tto o reverse a string.
Session 10: Interfacing assembly with HLL (1 session & 3 programs)
1.
Write the corresponding 8086 assembly language code of the following program given in C++ language. #include
Lab Course Course
{ cout << 'Enter number: '; cin >> n; sum = 0; k = 1; while (K <= sum) { sum += k; ++k; } cout << 'The sum = '; cout << sum; cout << 'n'; }
2.
Write a program in which it call a routine or function made in c of name fact. The fact functional calculates the factorial of a given number, call that function in assembly program and find the factorial of the given number. Store the result in memory.
3.
Write a program, which convert ASCII number into its equivalent binary in assembly code. Call this program in C and display the result on user screen. s creen.
2.5
WHAT NEXT?
You must use the skills acquired through the practical in order to develop efficient functions/subroutines, device drivers, interrupt servicing programs etc. Thus, you can further go on to do a lot of important things using Assembly Programming Language and extracting some useful efficient work using Microcomputers. You must refer to further readings as given in MCS 012 Block 4 in order to do so.
2.6
SUMMARY
This section is an attempt to provide details on Assembly Language Programming practice. The problems have primarily been divided into 8 sessions covering simple, arrays loops, functions, interrupt handling, calling assembly program from C etc. You must attempt all the problems in the specified number of sessions. In order to complete the tasks as above, you must come prepared with paper-based assembly programs and sho should test test them them at the the cente centerr for for any any possi possible ble erro errors. rs. Plea Please se note note, that that the assem assembly bly programs may look cumbersome, cumbersome, but give you a lot of power on machine. They allow you to understand the machine more closely and use it more efficiently.
28
Nov 4, 2014 MICRO PROCESSOR AND MICRO CONTROLLER Lab manual. INTRODUCTION TO MASM Programming of a microprocessor usually many are free or low cost TASM:- Another popular assembler made by as browse through realistic online museums and stores and download high-quality video. MICRO PROCESSORS AND INTERFERENCE LAB – SRIKARSH. PDF Masm Programming Lab Manual - brunpurire sub- Block 1 - C Programming Lab, Salient Features of C, C Programming Using Borland Language Programming, Assemblers, Turbo Assembler (TASM), MASM, Emu. Program should have suitable comments. Observation book will be evaluated for 20 Marks and Manual for 10 Marks. MASM Commands 1-2 2. Laboratory Session-1 3-5 3. Laboratory Session-1 5-6 4. Sample 8086 Programs 7-10 5. Search a Key Element in a set of 16 bit Numbers 12-13 6. 8086 Microprocessor Programming Lab Manual Chapter 1: 8086 Microprocessor Programming Lab Manual. & engineering and free book 8086 microprocessor programming lab manual pdf ee 314 lab #1 introduction to assembly, masm, and codeview department of computer science & engineering 8086 microprocessor (cont.).
IGNOU BCA Lab Manual Part-2,Assembly Language Programming.Uploaded by: Syed Shiyas, AC, IGNOU SC, MIC Trivandrum. Programming Laboratory Manual Students write experiments in practical files and get them signed by the lab teacher This lab manual is designed for three server side programming languages.
-->The Microsoft Macro Assembler (MASM) provides several advantages over inline assembly. MASM contains a macro language that has features such as looping, arithmetic, and text string processing. MASM also gives you greater control over the hardware because it supports the instruction sets of the 386, 486, and Pentium processors. By using MASM, you also can reduce time and memory overhead.
In This Section
ML and ML64 Command-Line Option
Describes the ML.exe and ML64.exe command-line options.
ML Error Messages
Describes ML.exe fatal and nonfatal error messages and warnings.
Directives Reference
Provides links to articles that discuss the use of directives in MASM.
Symbols Reference
Provides links to articles that discuss the use of symbols in MASM.
Operators Reference
Provides links to articles that discuss the use of operators in MASM.
Processor Manufacturer Programming Manuals
Provides links to websites that may contain programming information about processors that are not manufactured, sold, or supported by Microsoft.
Chemistry Lab Manual
MASM for x64 (ml64.exe)
Information about how to create output files for x64.
Related Sections
C++ in Visual Studio
Provides links to different areas of the Visual Studio and Visual C++ documentation.
Nursing Lab Manual
See also
Masm Programming Guide
Compiler Intrinsics
x86Intrinsics
x64 (amd64) Intrinsics