Lab Session 1
Lab Session 1
Lab Session 1
1
STUDENT INFORMATION
Name: KULSOOM
Semester: 6
Program:BSCS
2
INTRODUCTION
This section is an attempt to provide details on Assembly Language Programming practice. The
problems have primarily been divided into 8 sessions covering simple, 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 s h o u l d t e s t t h e m a t t h e c e n t e r f o r a n y p o s s i b l e e r r o r s .
P l e a s e n o t e , t h a t t h e a s s e m b l y programs may look cumbersome, but give you a lot
of power on machine. They allow you to understand the machine more closely and use it more efficiently.
3
INDEX
Lab Session 01
Object
4
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
5
A program normally consists of three parts or segments.
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
6
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:
.MODELmemorymodel[[,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. Thus both
code and data may be greater than 64K.
HUGE More than one code-segment. More than one data-segment. 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.
7
.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.
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.”
8
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 , 02H
INT 21H
int 21h
CHARACTER OUTPUT
To display a character, you have to use the DOS function 02h.
Sample Code
The following code fragment will display a string 'Hey'.
.model small
.stack 100h
9
.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'
int 21h
mov ah,4ch
int 21h
main endp
end main
10
Lab Tasks
1. Write a program to display your name.
.Model small
.stack 100h
.code
Main proc
Mov dl,'K'
Mov Ah,02h
INT 21h
Mov dl,'U'
Mov Ah,02h
INT 21h
Mov dl,'L'
Mov Ah,02h
INT 21h
Mov dl,'S'
Mov Ah,02h
INT 21h
Mov dl,'O'
Mov Ah,02h
INT 21h
Mov dl,'O'
Mov Ah,02h
INT 21h
11
Mov dl,'M'
Mov Ah,02h
INT 21h
Mov Ah,02h
INT 21h
Mov dl,'N'
Mov Ah,02h
INT 21h
Mov dl,'A'
Mov Ah,02h
INT 21h
Mov dl,'S'
Mov Ah,02h
INT 21h
Mov dl,'I'
Mov Ah,02h
INT 21h
Mov dl,'R'
Mov Ah,02h
INT 21h
Mov Ah,4ch
12
INT 21h
Main Endp
End Main
13