Forum Archive

Go Back   3D Realms Forums > General Topics > Programming Forum
Blogs FAQ Community Calendar

Notices

 
 
Thread Tools
Old 08-09-2009, 03:26 PM   #1
AllAmericanJBert

AllAmericanJBert's Avatar
Question Including C functions in Assembly
The title wording sounds wrong, but how do I use C functions in x86 assembly?
__________________
*I taught myself C, C++, C#, VB, HTML, and Javascript. At the moment, I am teaching myself Java, Perl, Objective C, and x86 assembly.*

I'm nuts!
Last edited by AllAmericanJBert; 08-09-2009 at 03:49 PM. Reason: The title wording *just plain* sounds wrong to a programmer
AllAmericanJBert is offline  
Old 08-11-2009, 03:07 AM   #2
Crosma

Crosma's Avatar
Exclamation Re: Including C functions in Assembly
I did not write this. It's in NASM format. It's the same basic idea in other assemblers. It's up to you to make sure all of the function names resolve upon linking.

Basically, you just push the function arguments onto the stack and then it's up to you to remove them afterwards. This stack methodology is what makes recursion possible (because the stack just keeps growing).

To load libraries dynamically and such, your best bet is to call the appropriate C functions.

Code:
; printf1.asm   print an integer from storage and from a register
; Assemble:	nasm -f elf -l printf.lst  printf1.asm
; Link:		gcc -o printf1  printf1.o
; Run:		printf1
; Output:	a=5, eax=7

; Equivalent C code
; /* printf1.c  print an int and an expression */
; #include <stdio.h>
; int main()
; {
;   int a=5;
;   printf("a=%d, eax=%d\n", a, a+2);
;   return 0;
; }

; Declare some external functions
;
        extern	printf		; the C function, to be called

        SECTION .data		; Data section, initialized variables

	a:	dd	5		; int a=5;
fmt:    db "a=%d, eax=%d", 10, 0 ; The printf format, "\n",'0'


        SECTION .text                   ; Code section.

        global main		; the standard gcc entry point
main:				; the program label for the entry point
        push    ebp		; set up stack frame
        mov     ebp,esp

	mov	eax, [a]	; put a from store into register
	add	eax, 2		; a+2
	push	eax		; value of a+2
        push    dword [a]	; value of variable a
        push    dword fmt	; address of ctrl string
        call    printf		; Call C function
        add     esp, 12		; pop stack 3 push times 4 bytes

        mov     esp, ebp	; takedown stack frame
        pop     ebp		; same as "leave" op

	mov	eax,0		;  normal, no error, return value
	ret			; return
__________________
(define love (lambda () (map conquer all)))
Last edited by Crosma; 08-11-2009 at 03:10 AM.
Crosma is offline  
 

Bookmarks

Tags
c functions, x86, x86 assembly


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 06:43 AM.

Page generated in 0.08639908 seconds (100.00% PHP - 0% MySQL) with 16 queries

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.

Website is ©1987-2014 Apogee Software, Ltd.
Ideas and messages posted here become property of Apogee Software Ltd.