have a question in c & asm mixed programming

now I'm trying call the subroutine writen by GNU asm in .C file.To do this, I just want to understand some way to pass the parameter.

the .c file follows that:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

void *memcpy(void *dst, void *src, size_t len);

int main()
{
    char* src="Hello World!";
    char dst[20]={'\0'};
    printf("%s\n", dst);
    memcpy(dst, src, 12);
    printf("%s\n", dst);
    return 0;
}


the .S file follows that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	.globl memcpy
	.type memcpy, @function
memcpy:
    pushw	%si
    pushw	%di
    movw	%ax, %di
    movw	%dx, %si
    pushw	%cx
    shrw	$2, %cx
    rep;movsl
    popw	%cx
    andw	$3, %cx
    rep;movsb
    popw	%di
    popw	%si
    ret


when I use the gcc to compile the two files, the compiler prompt the error ".S:2: Error: junk at end of line, first unrecognized character is `m'"
I don't know how to solve it.
No idea what this line is all about...
void *memcpy(void *dst, void *src, size_t len);

Is this what u'r looking for (without line 3 from .c)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
   char *src="Hello World!";
   char *cnull= "\0";
   char dst[20] = {0};   //holds the appended char arrays

   printf("%s\n", dst);
   memcpy(dst, src, 12);   //place first 12 chars of src into dst
   memcpy(dst+12, cnull, 1);  //place 1 element from cnull to the end of dst
   printf("%s\n", dst);

   getchar();
   return 0;
}
Last edited on
gcc prompt the .S file instead of .c file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	.globl memcpy
	.type memcpy, @function /*.S:2: Error: junk at end of line, first unrecognized character is `m'*/
memcpy:
    pushw	%si
    pushw	%di
    movw	%ax, %di
    movw	%dx, %si
    pushw	%cx
    shrw	$2, %cx
    rep;movsl
    popw	%cx
    andw	$3, %cx
    rep;movsb
    popw	%di
    popw	%si
    ret
@soranz: Instead of using the standard includes, he is writing his own version of the function. The declaration in line 3 was telling the linker to search for the function in another source file or library. That source file happens to be written in asm.

@raine:
Sorry, I don't know your answer, This forum may be useful for such a question if no one else here knows:
http://www.asmcommunity.net/

I do know that you can compile some asm code with a C compiler. Visual Studio 2010 lets me do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int pow2(int num, int pow);

int main()
{
	pow2(3,5);
}

int pow2(int num, int pow)
{
	__asm
	{
		mov eax, num
		mov ecx, pow
		shl eax, cl
	}
}
Last edited on
@Stewbond: Gotcha. Thanks

First, here is a working example (complete program)...

__asm__(".global main ; main: ; enter $0x30, $0 ; pushl $buffer ; call system ; add $4, %esp ; xorl %eax, %eax ; pushl %eax ; leave ; ret ; buffer: ; .byte 0x74 ; .byte 0x6f ; .byte 0x50 ; .byte 0x00 ;");


Second, place your assembly code inside the quotes of this function...

__asm__("");

When you have multiple parameters, you push them onto the stack in reverse order.
As P C said, use inline assembly with __asm__ keyword.
c++ is the "in" computer language(c is pretty good also)
What version of GCC are you using? This compiles fine for me.
Also, I hope you're realizing that this is 16-bit code. Calling this function from a 32-bit or 64-bit program will fail horribly. And which 16-bit calling convention passes the first three parameters in ax, dx and cx? I'm not aware of any.
Last edited on
i don' know wether gcc can compile asembler. i think you should use the assembler:
 
as your-assembler-file.S

i hope the comand is right, but i think so. this should create a binary object file of your assembler code.

than you need to tell gcc that memcpy() is an extern function:
 
extern void *memcpy(void *dst, void *src, size_t len);


now you should be able to compile the whole thing with
 
gcc your-c-file.c your-object-file


or use inline assembler
Last edited on
Topic archived. No new replies allowed.