C calls assembly methods
- 2020-04-02 01:54:17
- OfStack
Part c is very simple, with a random file name, such as main.c:
#include <stdio.h>
#include <stdlib.h>
void decToBin(long dec,char *b); //Declare external assembly functions
int main()
{
long dec=254;
char *bin=(char*)malloc(sizeof(char)*64);
decToBin(dec,bin);
printf("%sn",bin);
free(bin);
return 0;
}
I use a MAC 64-bit system, so the 64bit register is r, such as: rax, RBX, etc.
C calls the following code, save the name of any name, such as decbin.s or decbin.s.
.global _decToBin # Underlining is required
_decToBin:
pushq %rbp
movq %rsp,%rbp
movq %rdi,-8(%rbp) # First parameter
movq %rsi,-16(%rbp) # Second parameter
movq -8(%rbp),%rax
movq -16(%rbp),%rbx
movq $63,%rcx
A:
rclq $1,%rax
jnc B
movb $49,(%rbx)
jmp C
B:
movb $48,(%rbx)
C:
addq $1,%rbx
loop A
popq %rbp
ret
There are many AT&T grammars on the web, but I won't cover them.
The clang compiler is actually used on the MAC, which in turn involves the LLVM project, which is the framework for building the compiler (compiler), and Apple is the main funder of the LLVM program.
Cc links to the clang, Makefle file:
main:main.o decBin.o
main.o:main.c
decBin.o:decBin.s
cc -c $<
clean:
-rm main.o decBin.o main