Question: In the lecture material we looked at some code to write string of information to a file: INCLUDE asmlib.inc CreateFileA Proto, fileName: PTR BYTE, accessMode:
In the lecture material we looked at some code to write string of information to a file:
INCLUDE asmlib.inc
CreateFileA Proto,
fileName: PTR BYTE,
accessMode: DWORD,
shareMode : DWORD,
securityAttrib : DWORD,
creationDispo : DWORD,
flagsAndAttrib : DWORD,
hTemplateFile : DWORD
CloseHandle PROTO, hObject : DWORD
WriteFile PROTO,
hHandle : DWORD,
lpBuffer : PTR BYTE,
nNumberOfBytesToWrite : DWORD,
pNumberOfBytesWritten : PTR DWORD,
lpOverlapped : PTR DWORD
GetLastError PROTO ; get most recent error return value
GENERICREAD h
GENERICWRITE h
GENERICEXECUTE h
GENERICALL h
CREATENEW
CREATEALWAYS
OPENEXISTING
OPENALWAYS
TRUNCATEEXISTING
FILEATTRIBUTENORMAL h
NULL
data
fname BYTE "test.txt
fOut BYTE "Now is the time for all good men",ahdh
fHandle DWORD
bytesWritten DWORD
code
main PROC
mov eax, ;Clear eax
mov edx, OFFSET fname
INVOKE CreateFileA, edx, GENERICWRITE, NULL, NULL, ;Create the file for writing
OPENALWAYS, FILEATTRIBUTENORMAL, NULL
mov fHandle, eax ;File handle returned in eax
INVOKE WriteFile, fHandle, ADDR fOut, SIZEOF fOut, ADDR bytesWritten, NULL ;write text to file
INVOKE closeHandle, fHandle ; Close file
exit
main ENDP
END main
Using what you have learned from this unit create and a some of your own ingenuity create fWriteChar and fReadChar procedures.
fWriteChar should write a single character to a file. You should pass the character to write to the file in the al register. You should also pass the file name in the edx register.
fReadChar should read a single character from a file and return the character back to main in the al register. You should pass the name of the file to be read from in the edx register.
Both procedures should handle opening and closing the file. For instance the logic for fReadChar would be as follows:
Open the file
Read the character from the file
Close the file
It should be noted that some of the calls to the Windows API will not allow registers. Because of this you will need to create local variables for the task. To help you out with this here is the stubs for the procedures:
fReadChar PROC
local inChar : BYTE, bytesRead: DWORD, fHandle : DWORD
ret
fReadChar ENDP
Where inChar is the character that is read from the file. This will value will be passed back to main in the al register, bytesRead is the number of bytes read from the file, this should be and fHandle will be the handle of the file that gets generated when you call CreateFileA
fWriteChar PROC
local outChar : BYTE, bytesWritten: DWORD, fHandle : DWORD
ret
fWriteChar ENDP
Where outChar is the character to be written to the file. This should have been passed in the al register. bytesWritten will hold the number of bytes actually written to the file, and fHandle will hold the handle to the file generated when you call CreateFileA
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
