x86 Assembly

Moderators: None (Apply to moderate this forum)
Number of threads: 4563
Number of posts: 16029

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Calling fwrite? Posted by anessen on 29 Nov 2009 at 2:51 PM
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!
Report
Re: Calling fwrite? Posted by Josh Code on 29 Nov 2009 at 11:19 PM
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 http://msdn.microsoft.com/en-us/library/ms810467.aspx

Report
Re: Calling fwrite? Posted by anessen on 30 Nov 2009 at 4:48 AM
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)
1
4
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.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.