Page 1 of 1

[Solved]Use C to call an AS function?

Posted: Wed Aug 29, 2012 12:39 am
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!

Re: C call an AS function?

Posted: Wed Aug 29, 2012 12:41 am
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!

Re: Use C to call an AS function?

Posted: Wed Aug 29, 2012 2:02 am
by iansjack
"_putc" is not visible to the linker. Try using the ".global" directive.

Re: Use C to call an AS function?

Posted: Wed Aug 29, 2012 3:03 am
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

Re: Use C to call an AS function?

Posted: Wed Aug 29, 2012 4:33 am
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.

Re: Use C to call an AS function?

Posted: Wed Aug 29, 2012 9:45 am
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.

Re: Use C to call an AS function?

Posted: Wed Aug 29, 2012 9:48 am
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'?

Re: Use C to call an AS function?

Posted: Wed Aug 29, 2012 10:21 am
by iansjack
It depends upon how you are trying to use the symbol. Give us some example code.

Re: Use C to call an AS function?

Posted: Wed Aug 29, 2012 7:45 pm
by leyley
iansjack wrote:"_putc" is not visible to the linker. Try using the ".global" directive.
Thank you! It works now!