Created
September 12, 2013 12:44
-
-
Save x3ro/6536703 to your computer and use it in GitHub Desktop.
Source files for my latest blog post on working with assembly routines and C on Mac OS X - http://x3ro.de/2013/09/12/link-assembly-routine-with-c-on-osx.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global _my_pow | |
section .text | |
_my_pow: | |
push rbp ; create stack frame | |
mov rbp, rsp | |
cmp edi, 0 ; Check if base is negative | |
mov eax, 0 ; and return 0 if so | |
jl end | |
mov eax, edi ; grab the "base" argument | |
mov edx, esi ; grab the "exponent" argument | |
multiply: | |
imul eax, edi ; eax * base | |
sub esi, 1 ; exponent - 1 | |
cmp esi, 1 ; Loop if exponent > 1 | |
jg multiply | |
end: | |
pop rbp ; restore the base pointer | |
ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: run | |
run: clean test | |
./test | |
test: asm.o test.o | |
clang -o test asm.o test.o | |
asm64.o: | |
nasm -f macho64 asm.s | |
test.o: | |
clang -o test.o -c test.c | |
clean: | |
rm *.o test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
extern int my_pow(int base, int exp); | |
int main(int argc, char const *argv[]) { | |
int base, exp, result; | |
base = 2; | |
exp = 8; | |
result = my_pow(base,exp); | |
printf("Result: %d\n", result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment