Objective: To Understand The Basic Concept and Functionality of Assembly Language
Objective: To Understand The Basic Concept and Functionality of Assembly Language
Objective: To Understand The Basic Concept and Functionality of Assembly Language
LAB#2
Objective
To understand the basic concept and functionality of Assembly Language.
Theory
ASSEMBLY LANGUAGE
USES:
Assembly language is used most often when either communicating with the operating
system or directly accessing computer hardware.
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
DATA 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
BASIC 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:
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.
.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.
RESTRICTIONS:
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.”
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 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.
int 21h
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.
INT 21H
CHARACTER OUTPUT
To display a character, you have to use the DOS function 02h.
Example:
int 21h
mov AH, 2
int 21h
mov AH, 2
int 21h
mov ah,4ch
int 21h
main endp
end main
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:
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.
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".
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".
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".