Vous êtes sur la page 1sur 7

CS – 204 Lab Manual Lab 3b: Introduction to Assembly Language Programming

Contents

1. Assembly Language Functions


2. Assembly Program Template
3. A Sample Program using the Template
4. Practices

1 Assembly Language Functions

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:

Function Number Input Output


1 AH = 1 AL = ASCII code if
character key is pressed
AL = 0 if non-character key
is pressed
2 AH = 2 AL = ASCII code of the
DL = ASCII code of the display character or control
display character or control character
character
9 AH = 9 Display character string
DX = offset address of
character string
“The string must end with
$”
A DL = 0Ah Line Feed (new line)
D DL = 0Dh Carriage Return (start of
current line)
8 DL = 8h Backspace

2 Assembly Program Template

Writing an assembly language program is a complicated task, particularly for a beginner.


We will simplify this task by hiding those details that are irrelevant. We will use the
following template for writing programs. This template consists of three types of
statements: executable instructions, assembler directives, and macros. Executable
instructions generate machine code for the processor to execute at runtime. Assembler
directives provide information to the assembler while translating the program. Macros are
shorthand for a sequence of instructions, directives, or even other macros. We will learn
more about instructions, directives, and macros throughout the semester.

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

The most frequently used models are:

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

where name is the optional name of the segment. Here is an example:

.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.

3 A Sample Program by using the Template

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

int 21h ;execute line feed

;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

4 Practices: Answer the following questions

1. What does the MODEL directive identify?

2. List five different types of memory models.

3. Name the files that could be created by MASM if the program


PROG1.ASM is assembled.

4. What does the offset indicate?

5. Write instructions (not a complete program) to do the following:


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.

17
CS – 204 Lab Manual Lab 3b: Introduction to Assembly Language Programming

Manual 4
Answer the following questions

1. What does the MODEL directive identify?

2. List five different types of memory models.

3. Name the files that could be created by MASM if the program


PROG1.ASM is assembled.

4. What does the offset indicate?

5. Write instructions (not a complete program) to do the following:


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.

1) What does the MODEL directive identify?

.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

2) List five different types of memory models?

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

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

3)Name the files that could be created by MASM if the program


PROG1.ASM is assembled?

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

4) What does the offset indicate?

Offsets value indicates the status of processor. It is particular memory location of string
pointer and index. Register contains the offsets.

5) Write instructions (not a complete program) to do the following:

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

Vous aimerez peut-être aussi