Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Fopen In Assembly.

assembly fopen

  • Please log in to reply
3 replies to this topic

#1 freiza

freiza

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 03 April 2012 - 02:09 PM

How to call fopen in assembly language?
  • 0

#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 03 April 2012 - 03:25 PM

I'm assuming you're using Intel assembly language. Which assembler are you using? NASM, MASM, YASM, GAS...
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1311 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 03 April 2012 - 04:16 PM

If you're writing for Windows, check out the fopen () function:
http://msdn.microsof...b(v=VS.80).aspx

To use that function in NASM, you would want to define that function as an extern and then import it into the program.

Here's an example of doing that with the ExitProcess () function:
extern ExitProcess 

import ExitProcess kernel32.dll
, and here's how you can call it:
section .text use32 
..start: 

push dword 0 
call [ExitProcess]


Some Win32 API functions have different versions - ANSI or wide-character (Unicode) - such as MessageBox (MessageBoxA or MessageBoxW). For those, you need to look up the function and look at what the reference says (usually near the bottom of those MSDN reference pages).


To include a C library function, though, you'll probably need to import the functions from msvcrt.dll, like this:
extern printf 

import printf msvcrt.dll 

...
, or something similar.



Then you can assemble and link it to a PE. Here's how I usually assemble and link my programs:
\nasm\nasm -fobj hello.asm
\alink\alink -oPE hello.obj



Hope this helps :D .
  • 0
Regards,
RR

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 04 April 2012 - 01:29 AM

And here's an example with fopen(). :)

extern fopen

section .text
main:
    push    ebp
    mov     ebp, esp
    sub     esp, 8

    ; Same as fopen(file_name, file_mode)
    mov     DWORD [esp], file_name
    mov     DWORD [esp + 4], file_mode
    call    fopen

    ; Pointer to a FILE * struct will be returned in EAX.

    add     esp, 8
    pop     ebp
    ret

section .data
file_name: db "foo.txt", 0x00
file_mode: db "r", 0x00

Compile using nasm as usual, and link the object file with the standard C library. For *NIX systems it'd look like this:
nasm -f elf -Wall -Werror -o object.o test.asm
gcc -o program object.o

You now have a program called program using fopen().
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: assembly, fopen

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download