Académique Documents
Professionnel Documents
Culture Documents
Contents
INT 21h may be used to invoke a large number of DOS functions; a particular function is
requested by placing a function in the AH register and invoking INT 21h. Here we are
interested in the following functions:
Memory Models
14
CS – 204 Lab Manual Lab 3b: Introduction to Assembly Language Programming
The size of code and data a program can have is determined by specifying a memory
model using the .MODEL directive. The syntax is:
; MODEL memory_model
Model Description
SMALL Code in one segment
Data in one segment
MEDIUM Code in more than one segment
Data in one segment
COMPACT Code in one segment
Data in more than one segment
LARGE Code in more than one segment
Data in more than one segment
No array larger than 64k bytes
HUGE Code in more than one segment
Data in more than one segment
Arrays may be larger than 64k bytes
Data Segment
A program’s data segment contains all variable definitions. We use the directive .DATA
followed by variable and constant declarations. For example,
.DATA
WORD1 DW 2
WORD2 DW 5
MSG DB ‘THIS IS A MESSAGE’
MASK EQU 10010010B
Stack Segment
Stack is a holding area for addresses & data. The purpose of the stack segment
declaration is to set aside a block of memory to store the stack. The declaration syntax is:
.STACK size
.STACK 100H
Code Segment
The code segment contains a program’s instructions. The declaration syntax is:
.CODE name
15
CS – 204 Lab Manual Lab 3b: Introduction to Assembly Language Programming
.CODE
MAIN PROC
;main procedure instructions
MAIN ENDP
;other procedures go here
Putting It Together
Now we can construct the general form of a SMALL model program. With minor
variations, this form may be used in most applications:
.MODEL SMALL
.STACK 100H
.DATA
;data definitions go here
.CODE
MAIN PROC
;instructions go here
MAIN ENDP
;other procedures go here
END MAIN
The last line in the program should be the END directive, followed by name of the main
procedure.
Our first program here will read a character from the keyboard and will display it at the
beginning of the next line.
.model small
.stack 100h
.code
main proc
;input a character
mov ah,1 ;read character function
int 21h ;character in al
mov bl,al ;save it in bl
;go to a new line
mov ah,2 ;display character function
mov dl,0dh ;carriage return
int 21h ;execute carriage return
mov dl,0dh ;line feed
16
CS – 204 Lab Manual Lab 3b: Introduction to Assembly Language Programming
;display character
mov dl,bl ;retrieve character
int 21h ;display it
;return to DOS
mov ah,4Ch ;DOS exit function
int 21h ;exit to DOS
main endp
end main
Console Text
17
CS – 204 Lab Manual Lab 3b: Introduction to Assembly Language Programming
Manual 4
Answer the following questions
.model tells the program size. The size of code and data a program can have is
determined by specifying a memory model using the .MODEL directive. The syntax is:
; MODEL memory_model
.model directives instruct the assembler to generate code for a protected mode program
and it specifies the memory configuration for the assembly language
Small
Medium
Compact
Large
Huge
Model Description
SMALL Code in one segment
Data in one segment
MEDIUM Code in more than one segment
18
CS – 204 Lab Manual Lab 3b: Introduction to Assembly Language Programming
Once a program is written, it can be assembled and linked using the ML.exe
program. Alternatively, it can be assembled only using the ML.exe program and
linked using the LINK32.exe program. The assembler translates the source
assembly (.asm) file into an
equivalent object (.obj) file in machine language. As an option, the assembler can
also produce a listing (.lst) file. This file shows the work of the assembler and
contains the assembly source code, offset addresses, translated machine code,
and a symbol table. The linker combines one or more object (.obj) files produced
by the assembler with one or
more link library (.lib) files to produce the executable program (.exe)
19
CS – 204 Lab Manual Lab 3b: Introduction to Assembly Language Programming
Offsets value indicates the status of processor. It is particular memory location of string
pointer and index. Register contains the offsets.
a) Read a character, and display it at the same position on the same line.
b) Take two hex values, store them in two variables and exchange the
values of two variables.
a) Read a character, and display it at the same position on the same line.
MOV ah,1
int 21h
MOV AH, 2
MOV dl,al
Int 21h
b) Take two hex values, store them in two variables and exchange the
values of two variables.
value1 db 13h
value2 db 14h
XCGH value1,value
Int 21h
20