linux-assembly.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Using C Libraries From Assembly
@ 2005-08-29 19:13 Landon Blake
  2005-08-29 19:32 ` Tim Hoolihan
  2005-08-30  9:17 ` Stephen Pelc
  0 siblings, 2 replies; 5+ messages in thread
From: Landon Blake @ 2005-08-29 19:13 UTC (permalink / raw)
  To: linux-assembly

Linux Assembly Programmers,

I had posted an earlier message about adding graphics to an assembly
language program. After some research the past couple of days it seems
that most available, open source graphics libraries provide a C API. I
would like to add graphics capabilities to the simple programming
language I want to write in assembly.

So, my next question is this:

Can you call C functions from a program written in assembly language?
I've googled this subject, but was only able to find one paragraph that
didn't seem to help much. Most information seems to deal with going the
other direction, that is, calling assembly from C functions. However,
I'm guessing that it is possible, since both languages end up as machine
code at some point. Are there any resources that explain how to do this?

Thanks for all the help.

Landon

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Using C Libraries From Assembly
  2005-08-29 19:13 Using C Libraries From Assembly Landon Blake
@ 2005-08-29 19:32 ` Tim Hoolihan
  2005-08-29 20:37   ` Stephen Ray
  2005-08-30  9:17 ` Stephen Pelc
  1 sibling, 1 reply; 5+ messages in thread
From: Tim Hoolihan @ 2005-08-29 19:32 UTC (permalink / raw)
  To: Landon Blake; +Cc: linux-assembly

Try googling on "printf assembly".  I found a few good examples there.  
I'm not sure what assembler or syntax style you're using, so it's 
probably best to dig through the search results yourself. 

-Tim

Landon Blake wrote:

>Linux Assembly Programmers,
>
>I had posted an earlier message about adding graphics to an assembly
>language program. After some research the past couple of days it seems
>that most available, open source graphics libraries provide a C API. I
>would like to add graphics capabilities to the simple programming
>language I want to write in assembly.
>
>So, my next question is this:
>
>Can you call C functions from a program written in assembly language?
>I've googled this subject, but was only able to find one paragraph that
>didn't seem to help much. Most information seems to deal with going the
>other direction, that is, calling assembly from C functions. However,
>I'm guessing that it is possible, since both languages end up as machine
>code at some point. Are there any resources that explain how to do this?
>
>Thanks for all the help.
>
>Landon
>-
>To unsubscribe from this list: send the line "unsubscribe linux-assembly" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
>  
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Using C Libraries From Assembly
  2005-08-29 19:32 ` Tim Hoolihan
@ 2005-08-29 20:37   ` Stephen Ray
  0 siblings, 0 replies; 5+ messages in thread
From: Stephen Ray @ 2005-08-29 20:37 UTC (permalink / raw)
  To: Tim Hoolihan; +Cc: Landon Blake, linux-assembly

If you can find a copy of Jeff Duntemann's "Assembly Language 
Step-by-Step, 2nd Ed.", the second section deals with using assembly and 
C together.  A lot of this stuff is covered in G. Adam Stanislav's 
tutorial on using assembly and FreeBSD, available at 
http://www.int80h.org/bsdasm/
Pay careful attention to the bit on the calling convention, what 
registers you have to save, setting up a stack frame, etc.  That's the 
bulk of what you'll need to know.  The trickiest part seems to be 
getting your assembler to produce an object file in the correct format. 
  After you have an object file, you can link it all together with gcc.

A short example exerpted from Duntemann follows:

; Build using these commands:
;    nasm -f elf eatlinux.asm
;    gcc eatlinux.o -o eatlinux

[SECTION .txt]

extern puts
global main          ;required so linker can find entry point

main:
     push ebp         ;Set up stack frame for debugger
     mov ebp, esp
     push ebx         ; Program must preserve ebp, ebx, esi, & edi
     push esi
     push edi
     ;;; everything before this is boilerplate; use it for all ordinary 
apps!

     push dword eatmsg ; push a 32-bit pointer to the message on the stack
     call puts         ; call the clib function for displaying strings
     add esp, 4        ; clean stack by adjusting esp back 4 bytes

     ;;; everything after this is boilerplate; use it for all ordinary apps!
     pop edi          ; restore saved registers
     pop esi
     pop ebx
     mov esp, ebp     ; destroy stack frame before returning
     pop ebp
     ret

[SECTION .data]      ; section containing initialized data

eatmsg:  db "Eat at Joe's!",10,0

[SECTION .bss]       ; section containing uninitialized data

If I recall correctly, underscores may have to be placed before function 
names when coding for Windows.

A few points to remember (from Duntemann):
  - Functions have to preserve the contents of EBX, ESP, EBP, ESI and 
EDI.  You usually do this by not using them, or saving them on the stack 
and restoring them when done.
  - C functions return their values in EAX if they're 32-bit, or EAX and 
EDX, with the low bits in EAX and the high bits in EDX.  Anything else 
is returned via a pointer in EAX
  - you pass parameters to a c function by pushing them in reverse order 
(right to left) onto the stack.
  - C functions do not clean their parameters off the stack.  If you 
call a C function and pass parameters onto the stack, you are 
responsible for cleaning up afterwards.

If you want to call C code, or have C code call your function, just 
follow these rules.  With C code calling your functions, you will  have 
to follow these rules scrupulously, and may have to experiment with 
underscores preceding your function.  Once you get it figured out, 
though, it's not that hard.

Stephen


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Using C Libraries From Assembly
  2005-08-29 19:13 Using C Libraries From Assembly Landon Blake
  2005-08-29 19:32 ` Tim Hoolihan
@ 2005-08-30  9:17 ` Stephen Pelc
  2005-08-30 14:23   ` Richard Cooper
  1 sibling, 1 reply; 5+ messages in thread
From: Stephen Pelc @ 2005-08-30  9:17 UTC (permalink / raw)
  To: linux-assembly

From:           	"Landon Blake" <lblake@ksninc.com>

> Can you call C functions from a program written in assembly
> language? I've googled this subject, but was only able to find
> one paragraph that didn't seem to help much. Most information
> seems to deal with going the other direction, that is, calling
> assembly from C functions. However, I'm guessing that it is
> possible, since both languages end up as machine code at some
> point. Are there any resources that explain how to do this?

We've been going through the same exercise porting one of our 
products. I've kept all my notes in one place so that I can 
publish them as "The Compiler Writer's Guide to Linux". The 
current set of notes includes a complete program that calls 
printf from the libc shared library.

The notes are at
  http://www.mpeforth.com/arena/cwgtLinux.txt

At this stage I can't say that everything is 100%, but so far so 
good. The next task is to set up signal handlers from assembly. 
All feedback, criticism and help is welcome. The final book will 
be available as a free PDF from our website. Printed copies with 
an accompaying CD will also be available.

Stephen

--
Stephen Pelc, stephen@mpeltd.demon.co.uk
MicroProcessor Engineering Ltd - More Real, Less Time
133 Hill Lane, Southampton SO15 5AF, England
tel: +44 23 80 631441, fax: +44 23 80 339691
web: http://www.mpeltd.demon.co.uk - free VFX Forth downloads

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Using C Libraries From Assembly
  2005-08-30  9:17 ` Stephen Pelc
@ 2005-08-30 14:23   ` Richard Cooper
  0 siblings, 0 replies; 5+ messages in thread
From: Richard Cooper @ 2005-08-30 14:23 UTC (permalink / raw)
  To: Stephen Pelc; +Cc: linux-assembly

> The next task is to set up signal handlers from assembly.

That's not too difficult.  From your guide, it looks like you've got it  
mostly figured out already.

   %idefine sys_signal 48
   %idefine SIG_SEGV 11

   mov eax, sys_signal
   mov ebx, SIG_SEGV
   mov ecx, signal_handler
   int 0x80

   signal_handler
     printf "We crashed!!!\n";
   ret

Well, actually, you wouldn't want to return with SIG_SEGV, but...as long  
as you don't re-register the handler, I imagine returning would terminate  
your program.

One thing you have to keep in mind that I didn't see in your guide is that  
you have to re-register the signal hander every time the signal gets  
called.  Also, if your program recieves a signal that it doesn't have a  
handler set up for it will be terminated "kill -9" style.

The real trick is figuring out the register contents at the time of the  
crash.

   pushfd; pop dword [r.flags]; cld
   mov eax, [esp + 13 * 4]; mov [r.eax], eax
   mov eax, [esp + 10 * 4]; mov [r.ebx], eax
   mov eax, [esp + 12 * 4]; mov [r.ecx], eax
   mov eax, [esp + 11 * 4]; mov [r.edx], eax
   mov eax, [esp +  7 * 4]; mov [r.esi], eax
   mov eax, [esp +  6 * 4]; mov [r.edi], eax
   mov eax, [esp +  8 * 4]; mov [r.ebp], eax
   mov eax, [esp +  9 * 4]; mov [r.esp], eax
   mov eax, [esp + 16 * 4]; mov [r.eip], eax

Since I'm using pushfd, I guess I didn't find the flags saved anywhere.

Took me a while to figure that out.  There's another set of registers on  
the stack in front of that set, but they aren't the right ones, and where  
they come from, I have no idea.  I also have no idea if that works on any  
kernel other than my own.

I'll have to have a look at that third argument to the signal handler some  
time, if it points to where I'm getting the registers from now, then I'll  
go ahead and use it.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-08-30 14:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-08-29 19:13 Using C Libraries From Assembly Landon Blake
2005-08-29 19:32 ` Tim Hoolihan
2005-08-29 20:37   ` Stephen Ray
2005-08-30  9:17 ` Stephen Pelc
2005-08-30 14:23   ` Richard Cooper

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).