flat assembler
Message board for the users of flat assembler.

Index > Windows > Trying to use libC functions

Author
Thread Post new topic Reply to topic
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 17 Mar 2005, 18:57
Hi guys!

I was thinking about using C function on ASM with FASM. C is so easy and asm so powerful, so libC+ASM=Great code!
I've been successful on using functions like fopen, fprintf, printf, scanf, etc...
I just import them from msvcrt.dll or crtdll.dll that MSVC++ uses and comes with Windows.

But this little proggie is working. The problem is that it should copy 1536 bytes of itself (copy it totally) to an txt file. But it does it and puts lots of trash data in there. And what should have 1536 bytes goes to 20KB.

Any helps?

Code:

format PE console
include '%fasminc%\win32a.inc'
start:
invoke GetModuleFileName,0,buf,256
invoke fopen,buf,rmode
mov [fp],eax
invoke fopen,dest,wmode
mov [fp2],eax
mov ecx,1536
next:
invoke fgetc,[fp]
invoke fputc,eax,[fp2]
loop next
done:
invoke fclose,[fp]
invoke fclose,[fp2]
ret
;data
dest db 'test.txt',0
buf rb 256
rmode db 'rb',0
wmode db 'wb',0
fp dd 0
fp2 dd 0
;idata
data import
library crt,'msvcrt.dll',\
kernel32,'kernel32.dll'
import kernel32,GetModuleFileName,\
'GetModuleFileNameA'
import crt,fopen,'fopen',\
fclose,'fclose',\
fgetc,'fgetc',\
fputc,'fputc'
end data

    


Thanks a lot!
And sorry for my bad english... although code is self-explanatory... The universal language of ASM... heheh Wink
Post 17 Mar 2005, 18:57
View user's profile Send private message Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY 17 Mar 2005, 19:15
Oh sorry... I've just discovered it right now...
I just read the libC reference and realized that it returns -1 on EAX if error or EOF. Wink
So, here is the code fixed:

Code:
format PE console
include '%fasminc%\win32a.inc'
start:
invoke GetModuleFileName,0,buf,256
invoke fopen,buf,rmode
mov [fp],eax
invoke fopen,dest,wmode
mov [fp2],eax
mov ecx,1536
next:
invoke fgetc,[fp]
cmp eax,-1
jz done
invoke fputc,eax,[fp2]
loop next
done:
invoke fclose,[fp]
invoke fclose,[fp2]
ret
dest db 'test2.txt',0
buf rb 256
rmode db 'rb',0
wmode db 'wb',0
fp dd 0
fp2 dd 0
;idata
data import
library crt,'msvcrt.dll',\
kernel32,'kernel32.dll'
import kernel32,GetModuleFileName,\
'GetModuleFileNameA'
import crt,fopen,'fopen',\
fclose,'fclose',\
fgetc,'fgetc',\
fputc,'fputc'
end data
    


Anyway thanks!
And FASM is just great!
Soon I'll be writing macros that will allow people to use libC just like a normal c program, like:
printf "Hi!"
printf "What is your name?"
scanf "%s",buf
.
.
.
and so on...

Bye!
Post 17 Mar 2005, 19:15
View user's profile Send private message Reply with quote
Nikolay Petrov



Joined: 22 Apr 2004
Posts: 101
Location: Bulgaria
Nikolay Petrov 17 Mar 2005, 20:14
the functions and procedures are not stdcall. That's why you MUST "invoke" with "cinvoke" to return the stack - like wsprintf.
Code:
;save as test.asm and run it...
format PE console
entry start
include '%fasminc%\win32a.inc'

proc start
enter
        invoke GetModuleFileName,0,buf,256
        cinvoke fopen,buf,rmode
        mov [fp],eax
        cinvoke fopen,dest,wmode
        mov [fp2],eax
        mov ecx,1536
next:
        cinvoke fgetc,[fp]
        cmp eax,-1
        jz done
        cinvoke fputc,eax,[fp2]
        loop next
done:
        cinvoke fclose,[fp]
        cinvoke fclose,[fp2]
        return
endp

dest db 'test2.exe',0
buf rb 256
rmode db 'rb',0
wmode db 'wb',0
fp dd ?
fp2 dd ?

data import

library crt,'msvcrt.dll',kernel32,'kernel32.dll'

import kernel32,GetModuleFileName,'GetModuleFileNameA'

import crt, fopen,'fopen',\
            fclose,'fclose',\
            fgetc,'fgetc',\
            fputc,'fputc'
end data    
Post 17 Mar 2005, 20:14
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 23 Mar 2005, 10:30
Keep the calling convention in mind - when you call external code (libc, the API, ...) those routines are allowed to trash eax,ecx,edx and must preserve ebx,esi,edi,ebp - this means you can't depend on the values of eax,ecx,edx after a call to libc (except that eax is returncode, of course).
Post 23 Mar 2005, 10:30
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.