News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

writing to a file

Started by Jbarrera, February 16, 2018, 08:52:25 AM

Previous topic - Next topic

Jbarrera

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:


   .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


.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

Your code should look similar to this:.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

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

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.

aw27

Quote from: jj2007 on February 16, 2018, 02:50:17 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

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/

my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding