2 - Macros - Arithmetic - FIle Handling

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

NAME DATE REMARKS

Onin John Paul P. Binuya 11-02-23


COURSE/YEAR/SECTION INSTRUCTOR

BSCpE 3 Hilda Wycoco Santos

Experiment 12
Using Macros

I. Objective

After finishing this activity, you


should be able to III. Procedures
1. create and use macros to remove
repetitive encoding of 1. Run EDIT.COM from the DOS
instructions. prompt. Type the following lines of
code.
II. Introduction .model tiny
.tiny
A macro is a name that defines a set msg1 db ‘Enter a character: $’
msg2 db 0ah,0dh, ‘You entered $’
of instructions that are substituted msg3 db 0ah,0dh,‘End of program. $’
wherever the macro name appears to the .code
program. A macro is declared before a org 100h
segment. begin:
mov ah,09h
lea dx,msg1
The syntax of a macro declaration is int 21h
shown below what a statement actually mov ah,01h
does. int 21h
mov bl,al
macro name macro(dummy_argument) mov ah,09h
statements lea dx,msg2
statements int 21h
… mov ah,02h
endm mov dl,bl
int 21h
mov ah,09h
The macro name is a unique identifier lea dx,msg3
that is used in place of the actual program int 21h
statement. You can give your macro ret
definition a descriptive name that tells. end begin
The (dummy_argument) is a placeholder
for additional that tell data needed by the Save the above program as
macro. EXER16.ASM.
2. Run and assemble the program. What IV. Observation
is the output of the program?
After running the above program, it After assembling and running the
prompts the user to enter a Exer16 and Exer17, I noticed that the
character and display the input in
output is still the same, even though we
the new line together with the msg2
and ends the program. implemented macros on Exer17.
With the use of macros, the
readability of the program improves and
can also be more efficient, since we can
3. Run EDIT.COM from the DOS always call that macro every time we
prompt. Then try typing the following need that function, conserving coding
lines of code.
time and running time of the assembly
.model tiny program.
.data In single-stepping through each
msg1 db ‘Enter a character: $’
msg2 db Oah,Odh, ‘You entered $’
instruction, the character entered by the
msg3 db Oah,Odh, “End of program.$’ user is stored in the AL register and the
.code same character is stored in the BL register
write macro msg
mov ah,09h for output purposes. Before displaying
lea dx,msg the output, the DL register, which
int 21h
endm
indicates the character to be displayed on
org 100h the screen, is set to the value of BL.
begin:
write msgl
mov ah,01h V. Conclusion
int 21h
mov bl,al
write msg2
Upon accomplishing this activity, I
mov ah,02h was able to apply macros in an assembly
mov dl,bl language program. With this, repetitive
int 21h
write msg3 encoding can be eliminated, which will
ret reduce the time of our coding and make
end begin
our program more efficient in terms of
1. Save the program as EXER17.ASM execution.
Assemble the above examples (Steps Additionally, it can also make our
1 and 3) and single-step through each code easier to understand and manipulate
instruction using DEBUG.EXE. because any changes applied in the
function can be made in the macros,
which reduces the tendency of errors.
VI. Programming Problem

1. Write a program that instructs the


user to type in a string and accepts ten
characters (without echoing the input)
and displays asterisks (‘*’) in place of
the entered characters. Use macros.

Save the program as PROB10.ASM

2. Write macro definitions for


• Clearing the screen
• Positioning the cursor
• Displaying a single character
NAME DATE REMARKS

Onin John Paul P. Binuya 11-03-23


COURSE/YEAR/SECTION INSTRUCTOR

BSCpE 3 Hilda Wycoco Santos

Experiment 13
Arithmetic Instructions (Part I)

I. Objective The sum 6C is NOT the correct


ASCII or binary value.
After performing this activity, you The AAA instruction is used to adjust
the result of ASCII addition in AX
should be able to
register to the correct decimal result. For
1. perform addition and subtraction example, assume that AX contains hex
of binary and ASCII data in 0038 and BX contains hex 0034.
assembly language.
add al,bl
aaa
II. Introduction
The result in AX is 0102.
Addition and Subtraction
To restore the ASCII representation
The ADD and SUB instructions (for displaying the result of addition),
process binary data. Their general format insert 3s in the leftmost hex digits as
is as follows: follows:

add operand1,operand2 or ax,3030H ;result is now 3132H


sub operand1,operand2

Where: III. Procedures


operand1 maybe a register or a
memory location 1. Run EDIT.COM from the DOS
operand2 may be a register, a prompt. Then type the following
memory location, or an immediate program code.
value.
.model tiny
ASCII Addition .code
org 100h
begin:
Consider the effect of adding the mov ah,28H
ASCII numbers 8 (hex 38) and 4 (hex 34). mov al,0AH
add ah,al
hex 38 mov bh,28H
+34 mov bl,0AH
hex 6C sub bh,bl
ret
end begin IV. Observation
Save the above program as
EXER18.ASM In step 1, the AX register becomes
320A, the Auxiliary flag is set, and the
2. Assemble the program. Then run the Parity flag shows that the output of the
program using DEBUG.EXE
arithmetic operation is odd.
3. Try single-stepping through the Meanwhile, the BX register becomes
instructions and observe the result, 1E0A, and the Auxiliary flag is still set,
including the flags. but the Parity flag is now ‘PE’ indicating
that the result is even.
4. Run EDIT.COM from the DOS On the other hand, after single-
prompt. Type the following lines of stepping through the code in step 4, when
code:
I trace the instructions until the ‘add al,
.model tiny bl’, it normally adds 38h and 34h,
.data resulting in FF6C, and the parity even
byte1 db 38h
flag is set.
byte2 db 34h
.code As we continue to the ‘AAA’
org 100h instructions, the FF6C (which is stored in
begin:
mov al,byte1 the AX register) changes into 0002, and
mov bl,byte2 the auxiliary carry flag is also now set,
add al,bl
aside from the parity even flag.
aaa
or ax,3030h Moving on, when we restore the
mov bx,ax ASCII representation of the 0002, by
mov ah,02h
int 21h adding 3030h into AX, the result
mov dl,bl becomes 3032. The auxiliary carry flag is
int 21h
not set at this point, and the parity even
ret
end begin flag changes into a parity odd flag.

Save the program as EXER19.ASM V. Conclusion

5. Assemble and run the above program. After finishing this activity, I can
What is the output?
perform a simple arithmetic operation,
A2
like addition and subtraction of binary in
The 2 represents the one’s digit in
the result stored in BX, which is the an assembly language program.
3032 I also learned that the result of the
operation is not the correct ASCII value,
so we need the AAA or AAS instruction
6. Open the program using to adjust the result of the addition or
DEBUG.EXE. Try stepping through subtraction, respectively.
each instruction and observe the
Aside from this, we need to restore its
result of each trace operation. How is
the result displayed by the program? ASCII representation by adding 3030h in
the AX register (which holds the result of
our arithmetic operation).

VI. Programming Problem

The AAS (ASCII Adjust for


Subtraction adjusts the result of
subtraction. Write a program that
subtracts two-byte values (38h and 34h)
and then displays their result.
Save the program as PROB11.ASM
NAME DATE REMARKS

COURSE/YEAR/SECTION INSTRUCTOR

Experiment 14
Arithmetic Instructions (Part II)

I. Objective For example, suppose the AL register


contains hex 35 (5) and CL contains hex
After performing this activity, you 39 (9). The following multiplies the
contents of AL by CL and converts the
should be able to
result to ASCII format:
1. perform multiplication and
division of binary and ASCII data and al,0fh ;convert 35 to 5
in assembly language. and cl,0fh ;convert 39 to 9
mul cl ;multiply 5 by 9 (2Dh)
aam ;convert 2Dh to 0405
II. Introduction or ax,3030h ;convert to ASCII (3435)

For multiplication, the MUL The result of the MUL operation is


instruction handles unsigned data and the hex 2D or decimal 45. AAM divides this
IMUL (Integer Multiplication) result by 10, placing the quotient in AH
instruction handles signed data. and the remainder in AL (AX-0405). The
OR instruction then converts the bytes
For byte multiplication, the into its correct ASCII value (04=34h and
multiplicand is in the AL register and the 05=35h).
multiplier is a byte in memory or a
register. Division

The general format is For division, the DIV instruction


handles unsigned data and the IDIV
mul memory/register (Integer Division) instruction handles
imul memory/register signed data.

ASCII Multiplication To divide a word by a byte value, the


dividend is placed in the AX register and
The AAM (Adjust ASCII for the divisor (byte) is in memory or a
Multiplication) instruction is used to register. The remainder is in the AH and
adjust the result of multiplication in the the quotient in AL.
AX register. However, the leftmost hex
digit must first be cleared of 3s by The general format is
ANDing the operands by hex 0F.
div memory/register
idiv memory/register
3. Run EDIT.COM. Then type the
ASCII Division following lines of code:
The AAD (Adjust ASCII for Division
instruction provides adjustment of the .model tiny
dividend before division is performed. .data
Like AAM, the leftmost 3s must first be dividend dw2000h
cleared. divisor db 80h
.code
Assuming that AX contains the org100h
ASCII value 3238 (hex 1C) and the begin:
divisor 37 (hex 7), is in CL Then mov ax,dividend
div divisor
and cl,0fh ;convert 37 to 5 mov ax,dividend
and ax,0f0fh ;convert 323 to 0208 idiv divisor
aad ;convert to hex IC (28) ret
div cl ;divide by 7 end begin

The result of the DIV operation is Save the program as EXER21.ASM


04h.
4. Assemble the program. Open the
III. Procedures program using DEBUG.EXE and try
single-stepping through the
instructions. Observe each trace
1. Run EDIT.COM from the DOS operation.
prompt. Then type the following lines
of code
IV. Observation
.model tiny
.tiny
multiplier db 02h
multiplicand db 80H
.code
org 100h
begin:
mov al,multiplicand
mul multiplier
mov al,multiplicand
imul multiplier V. Conclusion
ret
end begin

Save the above program as


EXER20.ASM.

2. Assemble the program. Open the


program using DEBUG.EXE and VI. Programming Problem
then try stepping through the
instructions and observe the result,
including the flags.
Write a program to multiply the display the product. Save the program as
ASCII number 38 (hex 08) by hex 04 and PROB12 ASM.
NAME DATE REMARKS

COURSE/YEAR/SECTION INSTRUCTOR

Experiment 15
Procedures

I. Objective …
ret
procl endp
After performing this activity, you end begin
should be able to
The RET instruction is used to return
1. create and use procedures in your
program control to the main program
assembly language programs.
once the procedure is finished.

II. Introduction
III. Procedures

A procedure is a sequence of
1. Run EDIT.COM from the DOS
instructions that perform a single task. It
prompt. Then type the following lines
begins with the PROC directive and ends
of code.
with ENDP.
.mode tiny
The general format is .data
msgl db ‘Inside the main program$’
procedure_name PROC NEAR msg2 db ‘Inside the procedure$’
… .code
procedure_name ENDP org 100h
begin
A procedure is used by calling the mov ah,09h
lea dx,msgl
procedure name. Normally we use the int 21h
CALL instruction to enter a procedure call procl
(we can also use the JMP instruction). exit
ret
For example, to call the procedure procl proc near
mov ah,09h
PROC1, lea dx,
int 21h
.model tiny ret
.code procl endp
org 100h end begin
begin:
call procl Save the above program as

ret
EXER22.ASM
proc1 proc near
2. Assemble and run the above program. Save as PROB13.ASM and
What is displayed on the screen? PROB14.ASM, respectively.

3. Open the above program using


DEBUG.EXE. Then, single-step
through each instruction using T
(trace) command. Write your
observations.

IV. Observation

V. Conclusion

VI. Programming Problem

Write procedures for the following:

1. Clearing the general-purpose


registers.
2. Entering a string
NAME DATE REMARKS

COURSE/YEAR/SECTION INSTRUCTOR

Experiment 16
File Handling

I. Objective
The value in CX defines the attribute or
the type of file. A file may contain more
After performing this activity, you than one of the following attribute:
should be able to
1. create, delete, and rename DOS 00H Normal File
01H Read-only File
files in assembly language. 02H Hidden File
04H DOS System File
II. Introduction 10H Directory
20H Archive File

This experiment covers a number of


extended DOS services for disk A successful operation creates a file
operations introduced by DOS 2.0 and with the specified attribute and clears the
3.0. Many of these services are much carry flag. If the file already exists the file
simpler than the original DOS services is overwritten and its size is set to zero.
and are generally recommended.
An unsuccessful operation sets the
These operations involve the use of carry flag and returns an error code in AX
an ASCIIZ string to initially identify a (depending on the operation).
drive, path and filename; a file handle,
and special error return codes. Below are a number of common error
codes.
INT 21H Service 3CH: Creating a file 03 File not found
04 Path not found
Load service number 3CH in the AH 05 Access denied
register and the address of the ASCIIZ 11 Invalid format
string in DX. 21 Drive not ready

Code 05 means that the directory is


For example, full or the filename already exists and has
a read-only attribute. Be sure to check the
pathname db ‘C:\UNTITLED TXT’,0
… carry flag first if an error occurs.
mov ah,3ch
mov cx,00h INT21H Service 41H: Deleting a file
lea dx,pathname
int 21h
Use service 41H to delete a file (this
service cannot delete read-only files) 2. Assemble and run the program. What
from within a program. Load the address does the program do? Write your
of the ASCIIZ string in DX. observations.

For example, 3. Open the program using


DEBUG.EXE. Try to single-step
pathname db ‘C:\PROG8.ASM’,0 through each instruction (remember

to use the G command for interrupts).
mov ah,41h
lea dx,pathname Write down your observations.
int 21h
4. Modify EXER23.ASM, replacing
INT 21H Service 56H: Remaining a file service 3CH with service 41H. Save
the program as EXER24.ASM
Use service 56H to rename a file from
within a program. Load the DX register 5. Repeat Step 2 and 3 with EXER24
with the address of the ASCIIZ STRING ASM. Write your observations.
containing the drive, path and name of the
file to be renamed, and load the DI 6. Run EDIT.COM from the DOS
register with the address of the ASCIIZ prompt. Then type the following lines
string containing the new drive, path and of code.
name
.model tiny
oldpathname db ‘C:\PROG8.ASM’,0 .data
newpathname db ‘C:\EXER8.ASM’,0 oldpathname db ‘c:\untitled.txt’,0
… newpathname db ‘c:\myfile.txt’,0
mov ah,56h .code
lea dx,oldpathname org 00h
lea dl,newpathname begin:
int 21h mov ah,56h
lea dx,oldpathname
lea dl,newpathname
III. Procedures int 21h
ret
end begin
1. Run EDIT.COM from the DOS
prompt. Type the following lines of Save the above program as
code. EXER25.ASM.
.model tiny
.data
2. Assemble and run the program (make
org 100h sure that the file C:\UNTITLED.TXT
begin: exists in the root directory). What
mov ah,3ch does the program do? Write your
mov cx,00h observations.
lea dx,pathname
int 21h
end begin

Save the above program as


EXER23.ASM
IV. Observation VI. Programming Problem

Write a program that allows a user to


input the complete pathname (drive, path
and filename) and perform the following
file operations)

1. Create file with the specified


pathname.
2. Delete a file with the specified
pathname.
3. Rename a file
V. Conclusion
Hint: Use a parameter list for fileinput.
To convert the input string to an ASCIIZ
string, use the following lines of code.

namepar label byte


maxlen db 20
actlen db?
filename db 20 dup (‘ ‘)

mov ah,0ah
lea dx,namepar
int 21h
mov bl,actlen
mov filename [bx],0

Save as PROB15.ASM.
PROB16.ASM, and PROB17.ASM,
respectively
NAME DATE REMARKS

COURSE/YEAR/SECTION INSTRUCTOR

Experiment 17
Directories

I. Objective A successful operation creates the


subdirectory TASM in drive C. An error
After performing this activity, you sets the carry flag and returns code 03
(path not found or invalid path) or 05
should be able to
(access denied or directory already exists)
1. perform operations on directories in register AX.
in assembly language.
INT1H Service 3AH: Removing a
II. Introduction directory

This experiment covers a number of Function 3AH deletes a subdirectory,


extended DOS services for disk just like the DOS command RD.
operations introduced by DOS 2.0 and
3.0. Many of these services are much NOTE: You cannot delete the current
simpler than the original DOS services directory of a directory that contains files
are generally recommended.
dirnamne db ‘C:\TASM’,0

These operations involve the use of mov ah,3Ah
an ASCIIZ string to initially identify a lea dx,dirname
drive, path and filename; a file handle; int 21h
and special error return codes.
An error sets the carry flag and
Int21H Service 39H: Creating a returns code 03, 05, or 15 (invalid drive
directory or directory does not exists).

Function 39H creates a subdirectory, INT21H Service 3BH: Changing the


just like the DOS command MD current directory

For example, Function 3BH changes the current


directory to one that you specify, just like
dirname db ‘C:\TASM’,0 the DOS command CD.

mov ah,39h dirname db ‘C:\WINDOWS’,0
lea dx,dirname …
int 21h mov ah, 47h
lea dx, dirname
int 21h
4. Repeat Step 2 for EXER27.ASM.
What does the program do? Write
An error sets the carry flag and
your observations.
returns code 03 in AX.

INT 21H Service 47H: Getting the IV. Observation


current directory

Function 47H determines the current


directory for any drive. Define a space
large enough to contain the longest
possible pathname (up to 64 bytes), and
loads its name in SI. Identify the drive in
DL (0=default, 1=A, 2=B, and so forth). V. Conclusion

currentdir db ?

mov ah,47h
mov dl,0
lea si,currentdir
int 21h
mov bl,actlen
VI. Programming Problem
III. Procedures
Write a program that allows a user to
1. Run EDIT.COM from the DOS input the directory name and perform the
prompt. Then type the following lines following file operations.
of code
• Create a subdirectory with the
.model tiny specified name.
.data
pathname db ‘C:\UNTITLED’,0
• Delete a subdirectory with the
.code specified name
org 100h • Go to the specified director
begin • Retrieve the current working
mov ah,39h
lea dx,pathname
directory.
int 21h
ret Hint: Use a parameter list for file input.
end begin To convert the input string to an ASCII
string, use the following lines of code:
Save the program as EXER26.ASM.

2. Assemble and run the above program. namepar label byte


maxlen db 64 ← FOR LONG NAMES
What happens? Write your actlen db?
observations. dirname db 20 dup (' ')

3. Modify EXER26.ASM and replace mov ah,0ah
lea dx,namepar
service 39H with 3BH. Save the int 21h
program as EXER27.ASM.
mov bl,actlen
mov filename(bx), 0

Save as PROB18.ASM, PROB19.ASM,


and PROB20, ASM, respectively

You might also like