Author Topic: writing to a file  (Read 1723 times)

Jbarrera

  • Regular Member
  • *
  • Posts: 19
writing to a file
« on: February 16, 2018, 08:52:25 AM »
Hello everyone,

I am new to assembly code. I am using the Kip Irvine book and his example along with the libraries that are provided from the kip Irvine website.

There are a few things that I am not really understanding as my professor explained them (a bit rushed if I may add)

To create a file to write to the book give the following example:

Code: [Select]
   .data

         filename.   BYTE.    "myFile.txt",0
  .code
  main   PROC

        lea    edx, filename
        call CreateOutputFile


now my professor told me that a value is going to be generated in the EAX register and that I need to save that value.

can someone please explain to me how creating an output file works and how to access it for writing.

I was also told that I need to have 3 parameters in order to write to a file

Code: [Select]
.data
    outfile  dword ?   ; I guess I save the out file descriptor here?
   lineOut   BYTE   "Test print",0

.code

    mov   eax,outfile
    lea    edx,lineOut
    mov  ecx, 10
    call  writetofile

Thank you in advanced

jj2007

  • Member
  • *****
  • Posts: 9599
  • Assembler is fun ;-)
    • MasmBasic
Re: writing to a file
« Reply #1 on: February 16, 2018, 12:12:16 PM »
Your code should look similar to this:
Code: [Select]
.data
outfile dword ?   ; I guess I save the out file descriptor here?
lineOut BYTE "Test print",0
filename BYTE "myFile.txt",0

.code
  lea edx, filename
  call CreateOutputFile
  mov outfile, eax

  mov eax, outfile   ; just for illustration - here, eax already contains the handle
  lea edx, lineOut
  mov ecx, 10
  call WriteToFile

  mov eax, outfile
  call CloseFile

You have a .data section, containing strings, file handles, etc., and a .code section. I have not put the headers - your prof will have told you how they should look like. I am also not sure whether I put the arguments to the right registers - read your documentation.

Btw you need to be more careful when writing programs, for example:

- filename.   BYTE.    "myFile.txt",0   ; no such dots in code please
- the assembler doesn't know writetofile, it only know WriteToFile

And if you ever plan to do more serious stuff in assembler, drop that Irvine stuff and install the Masm32 SDK.

Jbarrera

  • Regular Member
  • *
  • Posts: 19
Re: writing to a file
« Reply #2 on: February 16, 2018, 12:49:21 PM »
Ok, this makes sense. Yes, sorry for the periods, I should probably copy and paste the code since double spacing will include a period automatically.

Just a few more questions:

1. what is the "handle" is it an address, just curious, does it work similar to a pointer?

2. on
Code: [Select]
mov ecx, 10 does the size of the output text need to be "declared"? or is it just the way Irvine wrote the PROC.
               Our professor is having us make a new write PROC that outputs one char at a time and checks for a null after each loop so we don't  have to count the number of chars.

Thank you for the reply

jj2007

  • Member
  • *****
  • Posts: 9599
  • Assembler is fun ;-)
    • MasmBasic
Re: writing to a file
« Reply #3 on: February 16, 2018, 02:50:17 PM »
1. a handle is a handle... it is not a pointer, and only Windows knows what to do with it.
2. yes, the size is needed; no idea how Irvine wrote his wrapper, the original is WriteFile

Note that the Irvine functions do not follow the rules of Windows programming. If you have an occasion to do so, tell your professor that the Masm32 SDK is a much better framework for professional assembler programming.

AW

  • Member
  • *****
  • Posts: 2179
  • Let's Make ASM Great Again!
Re: writing to a file
« Reply #4 on: February 16, 2018, 04:20:25 PM »
Note that the Irvine functions do not follow the rules of Windows programming.

The Irvine libraries functions are called with parameters passed in registers. There are no rules about that. There are rules to call Windows API functions.
The likely purpose is make students think out of the box. In addition to that, teachers use to require students to call their own functions declared without parameters and never use constructs like .if .then, .endif .
The end result is make them hate assembly language for the rest of their lives.  :biggrin:

daydreamer

  • Member
  • ****
  • Posts: 867
  • watch Chebyshev on the backside of the Moon
Re: writing to a file
« Reply #5 on: February 17, 2018, 07:34:55 PM »
I also want to recommend masm32sdk,there are many examples and some tutorials , but also recommend Visual C++ book for windows api calls or any other good MS C++ book
and good assembly book, search for Randall Hyde's downloadable assembly book
http://www.plantation-productions.com/Webster/

Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*