Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
19 Programs
with
Assembly
Language
LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. Explain three steps used for writing programs in assembly
language;
2. Determine the output for programs whereby the steps are already
stated; and
3. Differentiate between file.exe and .com program.
INTRODUCTION
Before continuing with this topic, you should know the basic instructions used in
the assembly language. In this topic, we will learn how to write complete codes
or programs in assembly language.
For you to have a better understanding of the functions and concepts used here,
you can make comparisons with high-level language instructions such C or C++
language. Ensure that you have downloaded the Arrowsoft Assembler software
from the LMS for you to write and execute programs in assembly language.
TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE 159
ACTIVITY 19.1
19.1.1 Edit
Editing is process of writing programs in a computer. Assembly language
programs can be written with any word processing application such as
WinEdit, Microsoft Word and NotePad. This program needs to be stored
in a form that is understood by the assembler. The writing in this program
is in a text form because the assemblers can only understand text.
The name of the program could be anything but it should have .asm
extension such as test.asm and problem.asm.
19.1.2 Assemble
Assemble is a process to convert programs in assembly language into
machine language. The usual Assembler used for the 8088 is masm.
However, only the assembly language emulator is used. This emulator is
known as Arrowsoft Assembler. The Emulator is compatible with the
Microsoft Assembler 3.0 produced by Microsoft.
You can easily get the Arrowsoft Assembler from any web sites or OUM
LMS. The difference between Microsoft Assembler 3.0 and the Arrowsoft
Assembler is assembler commands. If the Microsfot Assembler 3.0 uses the
masm command, the Arrowsoft Assembler uses asm.
160 TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE
To know more about it, you are advisable to visit this web site.
C:\assembly>asm Testing1.asm
Arrowsoft Assembler
Public Domain v1.00d(64K Model)
Copyright (c) 1986 Arrowsoft Systems Inc
Warning Severe
Errors Errors
0 0
In the above file execution, the model used is 64K. When an assembler assembles
a program in assembly language thus the output is an OBJ file. The size used to
run the program is 64K 49690 byte, which produces 14310 bytes.
As the assembler did not find any problems with the program, thus the message
Warning Errors and Severe Errors are set to 0. If this file has no problem
in terms of variable name, label name, registers and others, severe errors that still
exist due to the register used are not compatible in order to receive the data
assigning.
This OBJ program could convert to executable file by using link commands such
as:
tlink Testing1.obj
Testing1.exe
19.1.3 Execute
Finally, execute is a process to run the particular program to see whether the
program is correct or not.
TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE 161
The first program explains the way to write an assembly language program in
order to display the letter N on the monitor screen.
Page ,132
comment*
This program is to view how variables and registers
used change
Method:
Use debug to view the value changes.
The output of this program - Value is N is because
04eh is an N in ASCII
*
data segment
message db Value is,04eh,$
data ends
code segment
assume cs:code, ds: data
start:
mov ax,data
mov ds,ax
mov ah,9
mov dx,offset message
int 21h
mov ah,4ch
int 21h
code ends
end start
ACTIVITY 19.2
C:\assembly>tlink example1.obj
Once this step is completed, there will be an EXE file
named example1.exe.
Section 1:
Data segment
message db Value is , 04eh, $
data ends
code segment
assume cs:code, ds: data
start:
mov ax,data
mov ds,ax
mov ah,9
mov dx,offset message
int 21h
mov ah,4ch
int 21h
code ends
end start
Explanation:
(a) Code segment shows a program in assembly language will be executed.
(b) assume cs:code, ds:data shows CS register is used as the program
code whereas DS register is used for saving data.
(c) start: is a label used to show the beginning of an assembly language
program.
(d) mov ax,data is an instruction to assign data value into AX register.
(e) mov ds,ax is an instruction that causes data value to be stored into the DS
register. This is important because the AX register can be used for any
other operations.
(f) mov ah,9 is a dos function that enables string to be displayed on the
monitor.
(g) mov dx, offset message is to assign message into the DX register.
(h) int 21h is an important interrupt for input and output service.
(i) mov ah,4ch is a DOS instruction to end the program execution and
return control to the DOS.
(j) int 21h is an important interrupt for input and output service.
164 TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE
Second Example:
page ,132
comment *
This program is to view the output MY NAME IS
YUSOF
*
data segment
message db MY NAME IS ,059h,055h,053h,04fh,046h,$
data ends code segment
assume cd:code, ds:data
start:
mov ax,data
mov ds,ax
mov ah,9
mov dx,offset message
int 21h
mov ah,4ch
int 21h
code ends
end start
ACTIVITY 19.3
Perform the following steps to execute the program in the previous
example
(a) asm example2.asm
(b) tlink example2.obj
(c) example.exe
What is the output of this program?
In the second example, the difference is only at the data segment because in this
section there are five hexadecimal values that represent ASCII.
TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE 165
The ASCII values that represent these five characters are given below:
059h Capital Y
055h Capital U
053h Capital S
04fh Capital O
046h Capital F
Third Example
page , 132
title Welcome
data segment
message db Welcome...$
data ends
code segment
assume cs:code,ds:data
main proc far
start:
push ds
xor ax,ax
push ax
mov ax,data
mov ah,9
mov dx,offset message
int 21h
ret
main endp
code ends
end start
166 TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE
Section 1:
Section 2:
data segment
message db Welcome ....$
data ends
The name of this subprogram is data. It has string data that can be displayed on
the screen. String ends with $ that shows the execution of string ends.
Section 3:
code segment
assume cs:code,ds:data
main proc far
start:
main endp
code ends
end start
TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE 167
This segment will produce instruction for computer. It starts with ASSUME
instruction which will inform the symbol assembly for data and program codes.
Far procedure produces a type of file that can be obtained once the assembly
program is compiled, linked and ready to be executed.
These are the execution steps when this program runs as exe file:
(c) To enable DS register to refer to the data segment, the program must
ensure that the return path before the information is stored in a DS register.
(e) DS register cannot be offset directly, thus another way that can be used is
by using AX.
In your opinion, can the exe file for assembly program be used together with
other exe files? (Not an assembly language file).
The main difference between the .EXE and .COM file is the PROC FAR or
PROC NEAR statement. FAR usually for . EXE files whereas .COM files is
usually uses NEAR.
.EXE files are usually larger than .COM files. One more difference is .com files
does not require data segment declaration whereas .EXE files does.
page ,132
title com_file
code segment
assume cs:code, ds:code
org 100h
Main proc Near
startcom:
mov ah,9
mov dx,offset message
int 21h
ret
message db Welcome 2$
main endp code ends
end startcom
code segment
assume cs:code, ds:code
org 100h
main proc Near
The code segment is almost the same as procom1.asm above, the only
difference lies in the DS:CODE because in order to develop a COM file, the main
condition is that it cannot declare different stack segments.
Moreover, the programmer does not need to determine the value for the
registers because this task will be performed by linker.
The statement org 100h will determine where the code will start in a segment.
Whereas the main proc near statement differentiates the COM from EXE
files.
TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE 169
ACTIVITY 19.4
To produce COM file from EXE file:
1. asm procom1.asm
2. tlink procom1.obj
3. exe2bin procom1.exe procom1.com
Compare your output with the below output:
C:\macro>tlink procom 1.obj
Turbo Link Version 7.00 Copyright(c) 1987,1994
Borland International
Warning:No stack
C:\macro>procom 1
Welcome 2
(Use your own paper to answer the following question.)
page ,132
comment*
This program shows how the variable and
register are being used and changed
The output of this program - Value is N because
04eh is an N in ASCII.
Display for 5 times.
data segment
message db Value is , 04eh,$
data ends
code segment
assume cs:code,ds:data
start:
mov cx,5
looping
mov ax,data
mov ds,ax
mov ah,9
mov dx,offset message
int 21h
loop looping
mov ah,4ch
int 21h
code ends
end start
The program is changed from the previous one. The only addition required is
MOV CX,5 and then the loop label is entered.
mov cx,5
looping:
mov ax,data
mov ds,ax
mov ah,9
mov dx,offset message
int 21h
loop looping
mov ah,4ch
int 21h
TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE 171
In the above section program, it is clearly stated that the AX register will be
assigned to the segment data. Later the value in AX register will assigned to the
DS register.
The program continues by displaying string on the screen and the loop will be
executed five times because the CX register will be reduced by 1 for each loop.
Looping will stop when cx=0 and finally instruction mov ah,4ch is used to
terminate the program.
ACTIVITY 19.6
By doing all the given exercises, you will know how the execution of an
assembly language program is performed.
However, the examples of writing programs given are very basic.
Assemble Execute
Edit