[Solved]Use C to call an AS function?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
leyley
Member
Member
Posts: 35
Joined: Thu Jun 28, 2012 1:54 am

[Solved]Use C to call an AS function?

Post by leyley »

How to call an as function at a c program?
It works

Code: Select all

.type s2_main,@function
.type __putc,@function
__MAGEIC: .4byte 0x0000CDEF	/* Magic number; */
.code32
.text
_start:
	call s2_main
	jmp .
__putc:	
    popw %cx	/* char; */
    popw %bx	/* y: */
    popw %ax	/* x; */
    mov $0x0F,%ch
    imull $0x0050,%eax
    addl %ebx,%eax
    movw %cx,0xB8000(%eax)
    ret
It doesn't work!

Code: Select all

void __putc(unsigned short x,unsigned short y,unsigned char c);
void s2_main(){
	__putc(1,0,'*');
}
The error message is

Code: Select all

stage2_c.o: In function `s2_main':
stage2.c:(.text+0x12b): undefined reference to `__putc'
This error occurs when calling ld to link the as object and c object.
Is there any solution for this problem?
Thanks!
Last edited by leyley on Wed Aug 29, 2012 7:48 pm, edited 8 times in total.

Code: Select all

#rm -rf /
User avatar
leyley
Member
Member
Posts: 35
Joined: Thu Jun 28, 2012 1:54 am

Re: C call an AS function?

Post by leyley »

The makefile of it:

Code: Select all

# --------------------------------------------------------------------
# Makefile for boot;
# --------------------------------------------------------------------
CC = gcc
LD = ld
OBJCOPY = objcopy
RM = rm
# --------------------------------------------------------------------
# Stage1;
stage1.bin: stage1.elf
	@$(OBJCOPY) -R .pdr -R .comment -R .note -S -O binary stage1.elf stage1.bin
stage1.elf: stage1.o
	@$(LD)	stage1.o -o stage1.elf -nostdlib -Ttext 0x7c00
stage1.o: stage1.s
	@$(CC) -c stage1.s -o stage1.o
# --------------------------------------------------------------------
# Stage2;
stage2.bin: stage2.elf
	@$(OBJCOPY) -R .pdr -R .comment -R .note -S -O binary stage2.elf stage2.bin
stage2.elf: stage2_s.o stage2_c.o
	@$(LD) stage2_s.o stage2_c.o -o stage2.elf -nostdlib -Ttext 0x10000
stage2_s.o: stage2.s
	@$(CC) -c stage2.s -o stage2_s.o
stage2_c.o: stage2.c
	@$(CC) -c stage2.c -o stage2_c.o
# --------------------------------------------------------------------
# stages;
stages: stage1.bin stage2.bin
# --------------------------------------------------------------------
# Clean;
clean:
	@$(RM) stage1.bin stage1.elf stage1.o stage2.bin stage2.elf stage2_s.o stage2_c.o
Thanks!

Code: Select all

#rm -rf /
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Use C to call an AS function?

Post by iansjack »

"_putc" is not visible to the linker. Try using the ".global" directive.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Use C to call an AS function?

Post by AJ »

Hi,

Also, something in your code and error output does not match: The linker complains about the symbol s2_putc, whereas you attempt to call __putc according to your code snippet. Is this actually the version of the code that produces the error?

Cheers,
Adam
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Use C to call an AS function?

Post by qw »

In 32-bits mode, function arguments smaller than a doubleword are still stored as a doubleword, so "popw" will not work. Also, the value pushed last (and in this case, popped first) is the return address, not an argument. You should read up on calling conventions.
User avatar
leyley
Member
Member
Posts: 35
Joined: Thu Jun 28, 2012 1:54 am

Re: Use C to call an AS function?

Post by leyley »

AJ wrote:Hi,

Also, something in your code and error output does not match: The linker complains about the symbol s2_putc, whereas you attempt to call __putc according to your code snippet. Is this actually the version of the code that produces the error?

Cheers,
Adam
:? I must be copied the older error message because I have changed the name to s2_putc for test... ...The new error message is as same as older one except function name.

Code: Select all

#rm -rf /
User avatar
leyley
Member
Member
Posts: 35
Joined: Thu Jun 28, 2012 1:54 am

Re: Use C to call an AS function?

Post by leyley »

Hobbes wrote:In 32-bits mode, function arguments smaller than a doubleword are still stored as a doubleword, so "popw" will not work. Also, the value pushed last (and in this case, popped first) is the return address, not an argument. You should read up on calling conventions.
Thanks, I will try it.
Another thing is that The apple gcc can not compile it because it can not recognize such symbol like .2byte(I compile those code with Linux before).
Unknown pseudo-op: .2byte
Do it needs something like 'int', 'double'?

Code: Select all

#rm -rf /
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Use C to call an AS function?

Post by iansjack »

It depends upon how you are trying to use the symbol. Give us some example code.
User avatar
leyley
Member
Member
Posts: 35
Joined: Thu Jun 28, 2012 1:54 am

Re: Use C to call an AS function?

Post by leyley »

iansjack wrote:"_putc" is not visible to the linker. Try using the ".global" directive.
Thank you! It works now!

Code: Select all

#rm -rf /
Post Reply