Lab#2: Basic Assembly Language Format SSUET/QR/114
LAB#2
Objective
To understand the basic concept and functionality of Assembly Language.
Theory
ASSEMBLY LANGUAGE
Assembly language is a machine specific programming language with a one-to-one
correspondence between its statements and the computer’s native machine language. There are
many different types of assembly language, each specific to a processor or processor family.
IBM-PC assembly language refers to instruction recognized by a number of different
microprocessors in the Intel family: 8086, 8088, 80186, 80286, 80386, 80486, and Pentium.
USES:
Assembly language is used most often when either communicating with the operating
system or directly accessing computer hardware.
Secondly, assembly language is used to optimize certain critical areas of application
programs to speed up their runtime execution.
ASSEMBLER
An assembler is a program that converts source code programs from the assembly
language into machine language. The assembler can optionally generate a source- listing file with
line numbers, memory addresses, source code statements and a cross-reference listing of symbols
and variables used in a program.
The most popular assemblers for the Intel family are MASM (Microsoft Assembler), TASM
(Turbo Assembler).
LINKER
A companion program that combines individual files created by an assembler into a
single executable file
ASSEMBLY PROGRAM SYNTAX
Assembly language program consists of statements.
A statement is either an instruction to be executed when the program runs or a directive for
the assembler.
A program normally consists of three parts or segments.
CE-207: Microprocessor and Microcontroller 9
Lab#2: Basic Assembly Language Format SSUET/QR/114
DATA SEGMENT
Variables are declared in the data segment.
Each variable is assigned space in memory and may be initialized.
Exp:
A DW 3501H
It sets memory for a variable called A, and initialize it to 3501H.
DW - Define word (16 bits = 2 memory locations)
A DW (?) ; un- initialized variable
CODE SEGMENT
Program instructions are placed in the code segment. Instructions are actually organized
into units called procedures. Every procedure starts with a line.
Exp:
Main Proc;
Main is the name of procedure and PROC is the directive identify the start of the procedure
Main Endp;
Main is again the name of the procedure and Endp is the direcitive ; identifies the end
of the procedure
STACK SEGMENT
The stack segment is used for temporary storage of addresses and data. If no stack segment
is declared, an error message is generated, so there must be a stack segment even if the
program doesn’t utilize the stack.
These segments begin with the directives .stack, .code, and .data
PROGRAM SYNTAX
TITLE first program syntax
.Model Small ;Specifies the memory model used
.Stack 100H ;allocate 100H memory locations for stack
.Data ;start of the data segment
; Data definitions here
A DB ?
……..
.Code ;start of the code segment
Main Proc ;start of the first procedure
; instructions here
……
Main Endp ;end of the first procedure
; Other procedures here
End Main ;end of the complete assembly program
CE-207: Microprocessor and Microcontroller 10
Lab#2: Basic Assembly Language Format SSUET/QR/114
BASIC DIRECTIVES
Following are the description of commonly used directives;
The .MODEL directive specifies the memory model for an assembler module that uses the
simplified segment directives. The .MODEL directive must precede .CODE, .DATA, and
.STACK. Note that near code is branched to (jumped to) by loading the IP register only, while far
code is branched to by loading both CS and IP. Similarly, near data is accessed with just an offset,
while far data must be accessed with a full segment:offset address. In short, far means that full 32-
bit segment:offset addresses are used, while near means that 16-bit offsets can be used. The format
of the .MODEL directive is:
.MODEL memorymodel [[, langtype]] [[, stackoption]]
The memorymodel can be TINY, SMALL, COMPACT, MEDIUM, LARGE, HUGE, or
FLAT. The langtype can be C, BASIC, FORTRAN, PASCAL, SYSCALL, or STDCALL. The
stackoption can be NEARSTACK or FARSTACK.
TINY One segment. Thus both program code and data together must fit within
the same 64 Kb segment. Both code and data are near.
SMALL Program code must fit within a single 64 Kb segment, and data must fit
within a separate 64 Kb segment. Both code and data are near.
MEDIUM More than one code-segment. One data-segment. Thus code may be
greater than 64K.
COMPACT One code-segment. More than one data-segment. Thus data may be
greater than 64K.
LARGE More than one code-segment. More than one data-segment. No array
larger than 64K. Thus both code and data may be greater than 64K.
HUGE More than one code-segment. More than one data-segment. Arrays may
be larger than 64K. Thus both code and data may be greater than 64K.
FLAT No segmentation, all code and data can reach any location up to 4 Gb.
All program models but TINY result in the creation of exe-format programs. The TINY model
creates com-format programs.
CE-207: Microprocessor and Microcontroller 11
Lab#2: Basic Assembly Language Format SSUET/QR/114
.STACK: Defines the size of stack used in program
.DATA: Defines the data segments for data used in the program. Mark the beginning of the data
segment
.CODE: Identifies the code segment which contains all the statements. Also .code marks the
beginning of the code segment.
PROC: Beginning of the procedure
ENDP: End of the procedure
END: End of assembly language program
BASIC MOV INSTRUCTION:
We already defined in the Lab#1
RESTRICTIONS:
Move between memory to memory is not allowed.
A number directly inside a segment register is not allowed.
Segment to segment registers, move is not allowed.
The INTerrupt Instruction
Pentium processor has two memory architectures: real and protected. In real mode a Pentium
works like fast 8086 processor. Real mode uses 16 bit addresses. The Real mode is also called as
16-bit mode, because all 20 bit physical address is constructed by 16 bit address. MS-DOS
Operating system was the first operating system to implement Real-Address mode on IBM
personal computer.
The INT instruction is the instruction which does the most work in any assembler program. INT
instruction calls a DOS interrupt service routine (like a function) to perform a special task.
INT InterruptNumber
Where Interrupt Number ranges from 00H to 0FFH (i.e., from 0 to 255).
MS-DOS Operating system provides many common services through INT 21h. INT 21h MS-DOS
services are procedures that provide input-output, file handling, and memory management. They
are also called “MS-DOS function calls.”
CE-207: Microprocessor and Microcontroller 12
Lab#2: Basic Assembly Language Format SSUET/QR/114
The execution of an INT instruction causes an Interrupt Service Routine (ISR) associated with the
InterruptNumber to be executed. Many of the ISRs have multiple sub-functions. To specify which
sub-function is to be executed under a particular InterruptNumber, the AH register is assigned a
sub-function number before the execution of the INT instruction. Example:
MOV AH , 08H
INT 21H
causes sub-function number 08H of Interrupt number 21H to be executed. In addition, some sub-
functions require other values to be passed to the ISR in particular registers. Example: Sub-
function 09H of Interrupt 21H displays a $-terminated string on the screen. The sub-function
requires the offset of that string to be passed in the DX register:
MOV DX , OFFSET STRING
MOV AH , 09H
INT 21H
NOTE: DPMI (DOS PROTECTED MODE INTERFACE) IS AN INTERFACE ALLOWING A DOS PROGRAM
TO RUN IN PROTECTED MODE AND TO ACCESS EXTENDED MEMORY UNDER A MULTITASKING
OPERATING SYSTEM LIKE MICROSOFT WINDOWS 3.0 AND LATER.
DOS FUNCTION CALLS (INT 21H)
DOS function calls preserve the contents of all the registers except the AX register and any other
register or registers in which they explicitly return data.
TERMINATE PROGRAM AND RETURN TO DOS
Every time you want to terminate the program and return to DOS, you have to put the following
codes:
Assembly Language C Language Meaning
mov AX , 4C00H exit(0) Program terminates normally
int 21h
mov AX, 4C01h exit(1) Program terminates with error code 1.
int 21h
CE-207: Microprocessor and Microcontroller 13
Lab#2: Basic Assembly Language Format SSUET/QR/114
CHARACTER INPUT WITH ECHO
To take single input character thru a keyboard, you have to put the following codes:
The Codes The Result
mov AH, 01h The program is waiting for the input. Once a user presses a key, the
int 21h ASCII Code of the input character is returned in the AL register and
the input character is displayed as well.
NOTE: This service makes the program waits for the input. The user just needs to press the intended key
WITHOUT pressing "enter" key.
CHARACTER INPUT WITHOUT ECHO
MOV AH , 08H
INT 21H
The code of the input character is returned in the AL register.
CHARACTER OUTPUT
To display a character, you have to use the DOS function 02h.
The Initial requirement The result
AH = 02h The character stored in DL will be displayed.
DL = Character or ASCII Code
Example:
The following code fragment will display a string 'Hey'.
.code
Main proc
mov DL, 'H'
mov AH, 2
int 21h
mov DL, 'e'
mov AH, 2
int 21h
mov AH, 2
mov DL, 'y'
CE-207: Microprocessor and Microcontroller 14
Lab#2: Basic Assembly Language Format SSUET/QR/114
int 21h
mov ah,4ch
int 21h
main endp
end main
ASSEMBLY AND EXECUTION PROCESS:
Before you can run the program, though, you have to convert the source code into an executable
(able to be run or executed) form. This requires two additional steps, assembling and linking.
However, Microsoft has merged MASM and LINK program in one called ML. Source directory
must contain assembly source program and LINK.EXE. The assembly process will be simply:
Source directory> ML source.asm
The ML command will generate .exe file, which may be executed.
In our lab, we may used a utility called Edit plus to give a Graphical User Inter (GUI) for
generating .exe file. We need to configure edit plus as detailed below:
1. Start Edit plus by double clicking edit plus icon or by menu driven method.
CE-207: Microprocessor and Microcontroller 15
Lab#2: Basic Assembly Language Format SSUET/QR/114
2. Click in the item "Preference" under the menu "Tool". Then, select the item "Setting &
Syntax" under the group "Categories". And, press the button "Add".
3. Fill in the input line "Description" with Assembler, the input line "File extension" with asm,
the input line "Syntax file:" with Z:\MASM\masm.stx.
4. Press the button "Apply" and also press the button "OK".
5. Create a new file.
CE-207: Microprocessor and Microcontroller 16
Lab#2: Basic Assembly Language Format SSUET/QR/114
6. Copy the source codes in the Example–1 and save it in files prog1.asm.
7. Assemble the file prog1.asm by clicking the item "Assemble EXE format" under the menu
"Tool". Look at the messages in the screen. If there are some errors in your source code, fix
them all.
8. Link the file by clicking the item "Link EXE format" under the menu "Tool". Look at the
messages in the screen. If there is an error, contact your instructor.
9. Go to the MS-DOS prompt by clicking the item "DOS" under the menu "Tool".
10. Run the program, by typing in the command line: prog1.exe
11. Observe in your directory, by typing: dir prog1.*
What files do you have as the result of the above processes?
12. Close the DOS window and go back to the editor. Now, try to trace the execution of the
program step by step by clicking the item "Debug EXE program" under the menu "Tool".
CE-207: Microprocessor and Microcontroller 17