Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Calling fwrite?

anessenanessen Member Posts: 2
Hey all, I'm a bit new to Assembly language.

What I'm trying to do is to write an integer value to a COM port on Windows. The way I think I can do this is by using the fwrite function supplied with C, and having a pointer to an integer that I use with that function. I got this working in C just fine, now I need to get it to work in Assembly. (I don't know if this is the best way to do this)

So far I have this (in Visual Studio 2008):
int myint = 12000;
int *myintptr = &myint;
//...blah blah, portptr is declared and set up too (i know this works)
__asm {
mov eax, DWORD PTR(portptr)
push eax
push 1
push 4
mov ecx, DWORD PTR(myintptr)
push ecx
call DWORD PTR(fwrite) ;it breaks on this line
add esp, 10h

That general method for calling functions works fine for fgetc, fputc, fopen etc... but not for fwrite unless I've done something wrong there.

Any help is greatly appreciated!


  • Josh CodeJosh Code Member Posts: 675
    One obvious problem is that no FILE* parameter is being passed.

    The prototype for fwrite is:

    size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

    The parameter types used are fine except for the last one which is an int * instead of a FILE *. While it assembles fine even though it'll cause runtime problems because the assembler doesn't check data types as extensively as a c or c++ compiler.

    How do you know it isn't working? When you run it, does it give a segmentation fault or memory access violation message?

    This shouldn't require any inline assembly. See [link=][/link]

  • anessenanessen Member Posts: 2
    OK well portptr is a FILE * and it has been opened up successfully at this point. So what's going on the stack is this:

    portptr (initialised file pointer)
    myintptr (initialised integer pointer)

    This should be in the right order for the function call.

    What happens when it breaks during debugging is this:
    Unhandled exception at 0x7c90100b in Transmitter.exe: 0xC0000005: Access violation reading location 0xcccccd00.[/code]
Sign In or Register to comment.