Skip to content

Instantly share code, notes, and snippets.

Created Sep 12, 2013
What would you like to do?
Source files for my latest blog post on working with assembly routines and C on Mac OS X -
global _my_pow
section .text
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
imul eax, edi ; eax * base
sub esi, 1 ; exponent - 1
cmp esi, 1 ; Loop if exponent > 1
jg multiply
pop rbp ; restore the base pointer
all: run
run: clean test
test: asm.o test.o
clang -o test asm.o test.o
nasm -f macho64 asm.s
clang -o test.o -c test.c
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