Sei sulla pagina 1di 14

Topic Writing

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

19.1 THE PROCESS OF WRITING PROGRAMS


This process involves three activities: edit, assemble and execute. Each activity
has its own important and role. Thus, you should able to understand all of the
activities because without one of activities, a program is not complete.

ACTIVITY 19.1

Do you think the writing process in assembly language, which involve


activities like edit, assemble and execute is similar to writing programs
in a high level language?

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

Object filename[testing 1. ogj]: Source listing [NUL 1st]:


Cross reference [NUL.crf]:

49690 Bytes free

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

The output is a file named as

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

19.2 EXAMPLES OF PROGRAMS


First Example:

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

Most computer-architecture books such as Assembly Language For The


IBM - PC, International Edition, by Kip R, Maxwell Mac Millan explain
examples of assembly language programs.
162 TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE

ACTIVITY 19.2

Execution steps for the example1.asm are:

C:\assembly> asm example1.asm


If there are any errors, check the codes and repeat the above step. If there
are no errors, then proceed with the following step.

C:\assembly>tlink example1.obj
Once this step is completed, there will be an EXE file
named example1.exe.

To execute the particular codes


C:\assembly> example1

What output will be produced by the above program?

The explanation for the example1.asm program:

Section 1:

Data segment
message db Value is , 04eh, $
data ends

Section 1 is to declare the data value that is processed by this program.


(a) Data segment shows the beginning of subprogram named data.
(b) Message db is to declare the data value that will be processed which is
string. Value is 04eh and each string should end with the $ symbol.
(c) Count 4eh in decimals = 78. Refer to ASCII table, 78 is the letter N.
TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE 163

The purpose of Section 2 is to declare program codes involved.

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

comment*This program output is Welcome*

stack segment stack


db 16 dup(stack)
stack ends

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

The explanation about testing1.asm used in the program in this topic is as


follows.

Section 1:

stack segment stack


db 16 dup(stack)
stack ends

This subprogram is named stack. Instruction 16 DUP is to provide stack


16 8 byte.

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:

push ds xor ax,ax


push ax
mov ax,data mov ds,ax mov ah,9
mov dx, off message int 21h
ret

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:

(a) CS register goes to the beginning of segment code.

(b) DS register shows the required address to return to DOS.

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

(d) Instruction int 21h will return the program to DOS.

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

19.3 COM PROGRAM


Programs shown in the previous pages produce EXE. Besides this, there is
another type of file that known as file .COM.

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.

The following is program file for .com file


168 TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE

page ,132
title com_file

comment* This program displays welcome on the screen.


This program will be executed as com files *

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

The explanation for procom 1.asm for the upper section is :

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>exe2bin procom 1.exe procom 1.com


EXE to BINary converter.Copyright (c)RaMax 1995
Part of FreeDOS.
This file might be using enhanced EXE format
This files length is 231
File was successfully converted

C:\macro>procom 1
Welcome 2
(Use your own paper to answer the following question.)

19.4 PROGRAMS INVOLVING LOOPING


ACTIVITY 19.5
List down a few examples of operating system that uses looping.
170 TOPIC 19 WRITING PROGRAMS WITH ASSEMBLY LANGUAGE

The following is an example of a program that uses looping.

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.

The programmer should be very careful with the CX declaration. If instruction


MOV CX,0 is given in the above program, thus CX automatically becomes
FFFFh.

If this happens, looping will be executed for 65535 times.

ACTIVITY 19.6

Find out the output for the particular program?

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

Potrebbero piacerti anche