0% found this document useful (0 votes)
8 views19 pages

Coal Lab 13

The document contains a series of assembly language programs that demonstrate file handling operations using the Irvine32 library. It includes tasks for creating a file, reading a file, copying file contents, reversing file content, and adding line numbers to file content. Each task is accompanied by code and explanations detailing the functionality and flow of the programs.

Uploaded by

bscs22f22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Coal Lab 13

The document contains a series of assembly language programs that demonstrate file handling operations using the Irvine32 library. It includes tasks for creating a file, reading a file, copying file contents, reversing file content, and adding line numbers to file content. Each task is accompanied by code and explanations detailing the functionality and flow of the programs.

Uploaded by

bscs22f22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

MEHWISH SANA

NUM-BSCS-2022-22
COAL LAB REPORT
Task1P1:

; Creating a File (CreateFile.asm)

; Inputs text from the user, writes the text to an output file.

include \irvine\Irvine32.inc

include \irvine\macros.inc

INCLUDELIB \irvine\Irvine32.lib

BUFFER_SIZE = 501

.data

buffer BYTE BUFFER_SIZE DUP(?)

filename BYTE "output.txt",0

fileHandle HANDLE ?

stringLength DWORD ?

bytesWritten DWORD ?

str1 BYTE "Cannot create file",0dh,0ah,0

str2 BYTE "Bytes written to file [output.txt]: ",0

str3 BYTE "Enter up to 500 characters and press "

BYTE "[Enter]: ",0dh,0ah,0

.code

main PROC

; Create a new text file.

mov edx,OFFSET filename


call CreateOutputFile

mov fileHandle,eax

; Check for errors.

cmp eax, INVALID_HANDLE_VALUE ; error found?

jne file_ok ; no: skip

mov edx,OFFSET str1 ; display error

call WriteString

jmp quit

file_ok:

; Ask the user to input a string.

mov edx,OFFSET str3 ; "Enter up to ...."

call WriteString

mov ecx,BUFFER_SIZE ; Input a string

mov edx,OFFSET buffer

call ReadString

mov stringLength,eax ; counts chars entered

; Write the buffer to the output file.

mov eax,fileHandle

mov edx,OFFSET buffer

mov ecx,stringLength

call WriteToFile

mov bytesWritten,eax ; save return value

call CloseFile

; Display the return value.

mov edx,OFFSET str2 ; "Bytes written"


call WriteString

mov eax,bytesWritten

call WriteDec

call Crlf

quit:

exit

main ENDP

END main

EXPLANATION:

is designed to create a text file and write user-inputted text into it. It begins by
including necessary Irvine library files for input/output operations. The program
allocates a buffer for user input and specifies the filename as "output.txt". After
attempting to create the file, it checks for errors; if successful, it prompts the user
to enter up to 500 characters. The input is then written to the file, and the number
of bytes written is displayed before the program exits. Overall, this code
demonstrates basic file handling and user interaction in assembly language.

TASK1P2:
; Reading a File (ReadFile.asm)

; Opens, reads, and displays a text file using


; procedures from Irvine32.lib.

include \irvine\Irvine32.inc
include \irvine\macros.inc
INCLUDELIB \irvine\Irvine32.lib
BUFFER_SIZE = 50

.data
buffer BYTE BUFFER_SIZE DUP(?)
filename BYTE 80 DUP(0)
fileHandle HANDLE ?

.code
main PROC

; Let user input a filename.


mWrite "Enter an input filename: "
mov edx,OFFSET filename
mov ecx,SIZEOF filename
call ReadString

; Open the file for input.


mov edx,OFFSET filename
call OpenInputFile
mov fileHandle,eax

; Check for errors.


cmp eax,INVALID_HANDLE_VALUE ; error opening file?
jne file_ok ; no: skip
mWrite <"Cannot open file",0dh,0ah>
jmp quit ; and quit
file_ok:

; Read the file into a buffer.


mov edx,OFFSET buffer
mov ecx,BUFFER_SIZE
call ReadFromFile
jnc check_buffer_size ; error reading?
mWrite "Error reading file. " ; yes: show error message
call WriteWindowsMsg
jmp close_file

check_buffer_size:
cmp eax,BUFFER_SIZE ; buffer large enough?
jb buf_size_ok ; yes
mWrite <"Error: Buffer too small for the file",0dh,0ah>
jmp quit ; and quit

buf_size_ok:
mov buffer[eax],0 ; insert null terminator
mWrite "File size: "
call WriteDec ; display file size
call Crlf

; Display the buffer.


mWrite <"Buffer:",0dh,0ah,0dh,0ah>
mov edx,OFFSET buffer ; display the buffer
call WriteString
call Crlf

close_file:
mov eax,fileHandle
call CloseFile
quit:
exit
main ENDP

END main

EXPLANATION:
is designed to open, read, and display the contents of a text file using procedures
from the Irvine32 library. The program begins by prompting the user to input a
filename. After receiving the filename, it attempts to open the specified file for
reading. If the file cannot be opened, an error message is displayed, and the
program exits. Upon successfully opening the file, it reads up to 50 bytes into a
buffer. The program checks if the buffer is large enough to hold the file's contents; if
not, it displays an error message. Finally, if the read operation is successful, it
terminates the buffer with a null character, displays the size of the file, and prints
its contents before closing the file and exiting. This code illustrates basic file
handling and string manipulation in assembly language.
TASK 1 p2 changes
; CODE
Reading a File (ReadFile.asm)

; Opens, reads, and displays a text file using


; procedures from Irvine32.lib.

include \irvine\Irvine32.inc
include \irvine\macros.inc
INCLUDELIB \irvine\Irvine32.lib

BUFFER_SIZE = 90

.data
buffer BYTE BUFFER_SIZE DUP(?)
filename BYTE 80 DUP(0)
fileHandle HANDLE ?
.code
main PROC

; Let user input a filename.


mWrite "Enter an input filename: "
mov edx,OFFSET filename
mov ecx,SIZEOF filename
call ReadString

; Open the file for input.


mov edx,OFFSET filename
call OpenInputFile
mov fileHandle,eax

; Check for errors.


cmp eax,INVALID_HANDLE_VALUE ; error opening file?
jne file_ok ; no: skip
mWrite <"Cannot open file",0dh,0ah>
jmp quit ; and quit
file_ok:

; Read the file into a buffer.


mov edx,OFFSET buffer
mov ecx,BUFFER_SIZE
call ReadFromFile
jnc check_buffer_size ; error reading?
mWrite "Error reading file. " ; yes: show error message
call WriteWindowsMsg
jmp close_file

check_buffer_size:
cmp eax,BUFFER_SIZE ; buffer large enough?
jb buf_size_ok ; yes
mWrite <"Error: Buffer too small for the file",0dh,0ah>
jmp quit ; and quit

buf_size_ok:
mov buffer[eax],0 ; insert null terminator
mWrite "File size: "
call WriteDec ; display file size
call Crlf

; Display the buffer.


mWrite <"Buffer:",0dh,0ah,0dh,0ah>
mov edx,OFFSET buffer ; display the buffer
call WriteString
call Crlf

close_file:
mov eax,fileHandle
call CloseFile

quit:
exit
main ENDP

END main
Task 2:

CODE:

include \irvine\Irvine32.inc

include \irvine\macros.inc

INCLUDELIB \irvine\Irvine32.lib

INCLUDELIB \irvine\Kernel32.lib

BUFFER_SIZE = 501

.data

buffer BYTE BUFFER_SIZE DUP(?)

sourceFile BYTE 256 DUP(?)

destFile BYTE 256 DUP(?)

fileHandle HANDLE ?

bytesRead DWORD ?

bytesWritten DWORD ?

prompt1 BYTE "Enter the source file name: ",0

prompt2 BYTE "Enter the destination file name: ",0

successMsg BYTE "File has been copied successfully.",0


.code

main PROC

mov edx,OFFSET prompt1

call WriteString

mov edx,OFFSET sourceFile

mov ecx,SIZEOF sourceFile

call ReadString

mov edx,OFFSET sourceFile

call OpenInputFile

mov fileHandle,eax

cmp eax,INVALID_HANDLE_VALUE

jne file_ok

jmp quit

file_ok:

mov eax,fileHandle

mov edx,OFFSET buffer

mov ecx,BUFFER_SIZE

call ReadFromFile

mov bytesRead,eax

call CloseFile
mov edx,OFFSET prompt2

call WriteString

mov edx,OFFSET destFile

mov ecx,SIZEOF destFile

call ReadString

mov edx,OFFSET destFile

call CreateOutputFile

mov fileHandle,eax

; Check for errors.

cmp eax,INVALID_HANDLE_VALUE

jne write_ok

jmp quit

write_ok:

mov eax,fileHandle

mov edx,OFFSET buffer

mov ecx,bytesRead

call WriteToFile

mov bytesWritten,eax

call CloseFile

mov edx,OFFSET successMsg

call WriteString

call Crlf
quit:

exit

main ENDP

END main

EXPLANATION:
is designed to copy the contents of a source file to a destination file using the
Irvine32 library. The program begins by prompting the user to enter the name of
the source file. After attempting to open the specified file, it checks for errors; if the
file cannot be opened, the program exits. Upon successful opening, it reads the
contents of the source file into a buffer and then prompts the user for a destination
filename. The program attempts to create the destination file and checks for errors
again. If successful, it writes the contents from the buffer to the new file and
displays a success message indicating that the file has been copied successfully.
This code effectively demonstrates basic file operations, including reading from and
writing to files in assembly language.

Task3:

CODE:

include \irvine\Irvine32.inc

include \irvine\macros.inc

INCLUDELIB \irvine\Irvine32.lib

INCLUDELIB \irvine\Kernel32.lib

BUFFER_SIZE = 501

.data

buffer BYTE BUFFER_SIZE DUP(?)


sourceFilename BYTE 256 DUP(?)

destFilename BYTE 256 DUP(?)

fileHandle HANDLE ?

bytesRead DWORD ?

bytesWritten DWORD ?

str1 BYTE "Enter the source file name: ", 0

str2 BYTE "Enter the destination file name: ", 0

str3 BYTE "Cannot open file", 0dh, 0ah, 0

str4 BYTE "Reversed content saved to ", 0

.code

main PROC

mov edx, OFFSET str1

call WriteString

mov edx, OFFSET sourceFilename

mov ecx, SIZEOF sourceFilename

call ReadString

mov edx, OFFSET sourceFilename

call OpenInputFile

mov fileHandle, eax

cmp eax, INVALID_HANDLE_VALUE

je file_error

mov eax, fileHandle

mov edx, OFFSET buffer


mov ecx, BUFFER_SIZE

call ReadFromFile

mov bytesRead, eax

call CloseFile

mov ecx, bytesRead

mov esi, OFFSET buffer

add esi, ecx

dec esi

mov edi, OFFSET buffer

shr ecx, 1

jz skip_reverse

L1:

mov al, [esi]

mov bl, [edi]

mov [esi], bl

mov [edi], al

inc edi

dec esi

loop L1

skip_reverse:

mov edx, OFFSET str2

call WriteString

mov edx, OFFSET destFilename

mov ecx, SIZEOF destFilename

call ReadString
mov edx, OFFSET destFilename

call CreateOutputFile

mov fileHandle, eax

cmp eax, INVALID_HANDLE_VALUE

je file_error

mov eax, fileHandle

mov edx, OFFSET buffer

mov ecx, bytesRead

call WriteToFile

mov bytesWritten, eax

call CloseFile

mov edx, OFFSET str4

call WriteString

mov edx, OFFSET destFilename

call WriteString

call Crlf

jmp quit

file_error:

mov edx, OFFSET str3

call WriteString

quit:
exit

main ENDP

END main

EXPLANATION:

is designed to read the contents of a specified source file, reverse that content, and
save it to a new destination file. It begins by prompting the user for the source
filename and attempts to open this file for reading. If the file cannot be opened, an
error message is displayed, and the program exits. Upon successfully opening the
file, it reads the contents into a buffer and reverses the string by swapping
characters from both ends towards the center. After reversing, the program
prompts for a destination filename and creates this file. If successful, it writes the
reversed content to the new file and displays a confirmation message indicating
where the content has been saved. This code effectively demonstrates basic file
handling, string manipulation, and user interaction in assembly language.

Task 4:

CODE:

include \irvine\Irvine32.inc

include \irvine\macros.inc

INCLUDELIB \irvine\Irvine32.lib

BUFFER_SIZE = 501

.data

buffer BYTE BUFFER_SIZE DUP(?)

sourceFileName BYTE 256 DUP(?)


destFileName BYTE 256 DUP(?)

fileHandle HANDLE ?

bytesRead DWORD ?

bytesWritten DWORD ?

lineNumber DWORD 1

lineBuffer BYTE BUFFER_SIZE DUP(?)

prompt1 BYTE "Enter the source file name: ", 0

prompt2 BYTE "Enter the destination file name: ", 0

successMsg BYTE "File copied with line numbers successfully.", 0

.code

main PROC

mov edx, OFFSET prompt1

call WriteString

mov edx, OFFSET sourceFileName

mov ecx, SIZEOF sourceFileName

call ReadString

mov edx, OFFSET sourceFileName

call OpenInputFile

mov fileHandle, eax

cmp eax, INVALID_HANDLE_VALUE

je quit
read_loop:

mov eax, fileHandle

mov edx, OFFSET buffer

mov ecx, BUFFER_SIZE

call ReadFromFile

mov bytesRead, eax

cmp eax, 0

je close_source

mov esi, OFFSET buffer

mov edi, OFFSET lineBuffer

process_line:

mov al, [esi]

cmp al, 0

je write_line

cmp al, 0Ah

je add_line_number

movsb

jmp process_line

add_line_number:

mov eax, lineNumber

call WriteDec

mov al, ':'

stosb

mov al, ' '

stosb

inc lineNumber
mov al, 0Ah

stosb

jmp write_line

write_line:

mov edx, OFFSET lineBuffer

mov ecx, edi

sub ecx, OFFSET lineBuffer

mov eax, fileHandle

call WriteToFile

mov bytesWritten, eax

jmp read_loop

close_source:

mov eax, fileHandle

call CloseFile

mov edx, OFFSET prompt2

call WriteString

mov edx, OFFSET destFileName

mov ecx, SIZEOF destFileName

call ReadString

mov edx, OFFSET destFileName

call CreateOutputFile
mov fileHandle, eax

cmp eax, INVALID_HANDLE_VALUE

je quit

mov eax, fileHandle

mov edx, OFFSET lineBuffer

mov ecx, bytesWritten

call WriteToFile

call CloseFile

mov edx, OFFSET successMsg

call WriteString

call Crlf

quit:

exit

main ENDP

END main

EXPLANATION:

is designed to read a source text file, copy its contents to a new destination file,
and prepend line numbers to each line in the output. The program starts by
prompting the user for the source filename and attempts to open this file for
reading. If the file opens successfully, it enters a loop where it reads the file's
content line by line. For each line, it checks for line breaks and adds a line number
before writing the line to a buffer. Once all lines are processed, the program
prompts for a destination filename and creates this file. If successful, it writes the
buffered lines with their corresponding line numbers to the new file and displays a
success message. This code effectively demonstrates file handling, string
manipulation, and basic output formatting in assembly language.

You might also like