The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Dale on November 28, 2009, 05:26:35 PM

Title: [32bit]mASM FileIO, operands not the same size?
Post by: Dale on November 28, 2009, 05:26:35 PM
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))
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: jj2007 on November 28, 2009, 05:29:36 PM
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
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: Dale on November 28, 2009, 05:31:51 PM
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
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: dedndave on November 28, 2009, 05:41:10 PM
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
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: Dale on November 28, 2009, 05:44:18 PM
Ah I see, ok tyvm.  It's working now  :bg
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: dedndave on November 28, 2009, 05:53:33 PM
that was quick - lol
i am guessing you made it a 16-bit program   :bg
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: Dale on November 28, 2009, 06:17:34 PM
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
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: 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).
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: Dale on November 28, 2009, 06:40:18 PM
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 ???
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: 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
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: Dale on November 28, 2009, 06:51:16 PM
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
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: MichaelW on November 28, 2009, 06:56:53 PM
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.
Title: Re: [32bit]mASM FileIO, operands not the same size?
Post by: Dale on November 28, 2009, 07:09:51 PM
Ahh thank you it works.