News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

[32bit]mASM FileIO, operands not the same size?

Started by Dale, November 28, 2009, 05:26:35 PM

Previous topic - Next topic

Dale

Hi, I am new to assembly and loving it, but not to programming .  I am having trouble with the following code:

I am pretty sure it is because I would have to do it the 16bit way? But how would I do it using my instruction set in 32 bit?

.386
.model flat, stdcall
option casemap:none

;my libraries and inc files here

.data
FileName     db "info.ini", 0 ;error instruction not the same size

FileLength  dw 0
FileHandle  dw 0

.code
start:
mov ax, 3ch ;3ch = create
mov ax, cs
mov ds, ax

mov dx, offset FileName ;ERROR: instruction operand must be the same size.
mov cx, 0 ;normal file
int 21h

jc @isSaved


@isSaved:



@Create:

@Exit:
push eax
call ExitProcess

end start


So I am having trouble creating and reading files, I don't want to use any high level functions and I want to use the 80386 instruction set (I don't want to use masm32rt.inc, want to do it the low level way (if it is possible in 32bit mASM))

jj2007

You are assembling in 32-bit mode, but you use 16-bit instructions.

mov dx, offset FileName  ; an offset is 32-bit
mov edx, offset FileName  ; this works

Dale

Ah I see, thank you.  It links but crashes when I run it.  I don't know why it's crashing, but I know that its my create file code.

It crashes on 21h

Unhandled exception at 0x00401013 in test.exe: 0xC0000005: Access violation reading location 0xffffffff.

Access violation reading EOF

dedndave

INT 21h is not supported in 32-bit code
you need to use the windows API functions
to open a file, we use the CreateFile API function and set the flag for "OPEN_EXISTING"
http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx

if you search around the forum (use the forum search tool), you can find many examples

Dale


dedndave

that was quick - lol
i am guessing you made it a 16-bit program   :bg

Dale

Nope, used 32bit.  Err, one last thing.  Using WriteFile, how do I make my buffer multilined?

For example in C++:

writer << "Line1\n";
writer << "Line2\n";


How is this done in assembly using WriteFile?

Heres what I am using

laData      db "", 0


invoke WriteFile, hFile, addr laData, sizeof laData, lpWRT, 0

or

push 0
push lpWRT
push sizeof laData
push offset laData
push hFile

call WriteFile


WriteFile crashes my program :( not sure why.  I mov hFile, eax after I created the file using CreateFile

MichaelW

The forth parameter is supposed to be the address of a dword that receives the number of bytes written. To separate the lines you need to add a CRLF to the end of each line (where "end" means that the CRLF should be the last two characters, immediately preceding the null terminator).
eschew obfuscation

Dale

Quote from: MichaelW on November 28, 2009, 06:32:57 PM
The forth parameter is supposed to be the address of a dword that receives the number of bytes written. To separate the lines you need to add a CRLF to the end of each line (where "end" means that the CRLF should be the last two characters, immediately preceding the null terminator).
Thank you, but could you show me an example?

test db ???

MichaelW

Sorry, I should have explained better. By "CRLF" I meant a carriage return (ASCII character 13), linefeed (ASCII character 10) sequence.  This is effectively what the "\n" in "Line1\n" does.

theTest db "line1",13,10,0
        db "line2",13,10,0
eschew obfuscation

Dale

Quote from: MichaelW on November 28, 2009, 06:47:36 PM
Sorry, I should have explained better. By "CRLF" I meant a carriage return (ASCII character 13), linefeed (ASCII character 10) sequence.  This is effectively what the "\n" in "Line1\n" does.

theTest db "line1",13,10,0
        db "line2",13,10,0



Ah I see, thank you very much

But the problem is it doesn't write the second line

Heres my code:

writedata db "Line1", 13, 10, 0
db "Line2", 13, 10, 0

bytesWRT  dd ?


push 0
push offset bytesWRT
push sizeof writedata
push offset writedata
push hFile
call WriteFile

MichaelW

Assuming that theTest is supposed to be a single string, then the code should have been:

theTest db "line1",13,10
        db "line2",13,10,0


So theTest would be written as a single string that contains two lines.
eschew obfuscation