flat assembler
Message board for the users of flat assembler.

Index > Linux > [ncurses] How to call a c function from fasm?

Author
Thread Post new topic Reply to topic
int3h



Joined: 13 Dec 2010
Posts: 8
int3h 18 Mar 2011, 22:41
Hi,
How to convert the below ncurses hello world program to asm ?
Code:
#include <ncurses.h>
#include <stdio.h>

int main()
{      
    initscr();                      /* Start curses mode  */
    printw("Hello World !!!");  /* Print Hello World */
    refresh();                  /* Print it on to the real screen */
    getch();                   /* Wait for user input */
    endwin();                   /* End curses mode */

    return 0;
}
    


I wanted a fasm version. I used gcc -lncurses -S -masm=intel nhello.c to get the assembly put by gcc. But its a bit confusing.

Usually in case of masm, we push parameters to stack and call (or uses invoke) the corresponding function. But how to do something similar to that here?
Thanks. Smile
Post 18 Mar 2011, 22:41
View user's profile Send private message Visit poster's website Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 18 Mar 2011, 22:58
Something like
Code:
format     ELF

public       main
public  stdscr

extrn     exit
extrn   initscr
extrn        wrefresh
extrn       wprintw
extrn        wgetch
extrn endwin

section '.data' writeable
msg        db 'Hello World !!!',0
stdscr      dd ?

section '.text' executable
main:   call    initscr
     ccall   wprintw,[stdscr],msg
        ccall   wrefresh,[stdscr]
   ccall   wgetch,[stdscr]
     call    endwin
      ccall   exit,0
    

?

ccall is available somewhere in the examples directory and in the Windows' include directory, /include/macro/proc32.inc.
Or you might want to do it without macros altogether if you like so.
Post 18 Mar 2011, 22:58
View user's profile Send private message Reply with quote
int3h



Joined: 13 Dec 2010
Posts: 8
int3h 18 Mar 2011, 23:36
Thanks for the quick reply.
I'm getting an error while assembling.
Code:
ccall wprintw, [stdscr], msg
./INCLUDE/MACRO/PROC32.INC [31] ccall [8]:
    call proc
error: undefined symbol 'wprintw'.
    

Shouldn't we specify where the ncurse lib is or something?
Post 18 Mar 2011, 23:36
View user's profile Send private message Visit poster's website Reply with quote
ManOfSteel



Joined: 02 Feb 2005
Posts: 1154
ManOfSteel 19 Mar 2011, 00:11
When you link it.
Post 19 Mar 2011, 00:11
View user's profile Send private message Reply with quote
int3h



Joined: 13 Dec 2010
Posts: 8
int3h 19 Mar 2011, 01:14
I have no luck getting fasm to assemble the program. Could you please help?
wprintw is extrn, but why is fasm keep complaining that it's undefined? Btw I'm a noob Wink
Post 19 Mar 2011, 01:14
View user's profile Send private message Visit poster's website Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
ProphetOfDoom 19 Mar 2011, 03:34
Hi, this works for me (Ubuntu)

Code:
format ELF

public _start

extrn initscr
extrn printw
extrn refresh
extrn getch
extrn endwin
extrn exit

section '.data'

hello db "Hello World !!!",0

section '.text' executable

_start:

call initscr
push dword hello
call printw
add esp, 4
call refresh
call getch
call endwin
push 0
call exit
    


assemble with:

Code:
fasm filename.asm
    


link with:

Code:
ld filename.o -dynamic-linker /lib/ld-linux.so.2 -lc -lcurses
    


run with:

Code:
./a.out
    


I don't know why the -dynamic-linker option is necessary btw... another user on this board told me to use it...
Post 19 Mar 2011, 03:34
View user's profile Send private message Reply with quote
int3h



Joined: 13 Dec 2010
Posts: 8
int3h 19 Mar 2011, 20:52
Thanks.
Post 19 Mar 2011, 20:52
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.