Create a gist now

Instantly share code, notes, and snippets.

@x3ro /Makefile
Created Sep 12, 2013

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
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
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
#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