News:

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

Assembler make me crazy!

Started by nobby_trussin, February 09, 2005, 01:49:40 AM

Previous topic - Next topic

nobby_trussin

Hi,
i hope someone can help. i have been trying to write a program to read one file (512 bytes of 'X' test data) and write it to an output file using the create file (), readfile () and writefile () Windows API functions - sounds simple, no?

The program opens the source file handle fine and reads 512 bytes into a buffer successfully, but then when i try to close the handle (or call any function for that matter) the debugger shows me that the function jumps to the data read from the file to execute it!! Eg it jumps to 0x58585858 - 0x58 being the 'X' Ascii code.

I can see no logical reason for this happening especially as the readfile part works fine. I do not consider myself an amatuer and am doing a degree in this stuff but it has me stumped - really - I can only guess that my data is overwriting an address somewhere, somehow. I must be missing something fundamental somewhere. I am using masm32 assembler and windows 2003 .net for debugging.

Thanks, any input would be most appreciated (code below)

Dan

.data
  errortext     db "Error opening", 0
  filename      db "out.old", 0
  tmpfilename   db "out.txt", 0
  handle        dd 0
  tmphandle     dd 0
  bytesread     dd 0
  buffer        db 0 dup(513)

.code
start:
  mov eax, 0
  push eax
  mov eax, FILE_ATTRIBUTE_READONLY      ;flags and attributes (normal access)
  push eax                     
  mov eax, OPEN_EXISTING                        ;Creation distribution
  push eax
  mov eax, 0                                            ;security descriptor
  push eax
  mov eax, FILE_SHARE_READ                   ;share mode
  push eax
  mov eax, GENERIC_READ                       ;read mode
  push eax
  lea eax, filename                                   ;file
  push eax
  call CreateFile                                       ;open

  cmp eax, 0                                          ;function return 0?
  je opendriveerror                                  ;if so, display error dialog
  mov handle, eax                                   ;else save the handle
 
  mov eax, 0
  push eax
  lea eax, bytesread
  push eax
  mov eax, 512
  push eax
  lea eax, buffer
  push eax
  mov eax, handle
  push eax
  call ReadFile

  mov eax, handle
  push eax
  call CloseHandle                    ;at this point program throws exception error: Unhandled exception at 0x58585858 win.EXE: 0xC0000005: Access violation reading location 0x58585858.

raymond

Quotebuffer        db 0 dup(513)

Try changing the above instruction to:

buffer db 512 dup(0)

I don't know which assembler syntax you are using to declare the buffer variable the way you did but you may have been reserving no space for the read buffer. A jump table for the APIs used by your program is usually appended at the end of your data section. Reading the file may have overwritten that jump table.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Mirno

Just so as you know, you can push immediate values and the contents of memory directly (without moving them to a register first).


  push 0
  push FILE_ATTRIBUTE_READONLY
  push OPEN_EXISTING
  push 0
  push FILE_SHARE_READ
  push GENERIC_READ
  push OFFSET filename   ; You only need lea / push if variable is a LOCAL
  call CreateFile


Mirno

nobby_trussin

Ha! well, what do you know, the damn thing works now. To check, i reassembled/linked using different values in dup() and the .EXE file's size didn't change so yeah it wasn't creating it properly, but with the syntax you gave, all of a sudden the file is 512 bytes bigger :) . I've spent hours puzzling over that too.

Cheers man

Dan