diff --git a/Makefile b/Makefile index 25b8148038585c950484d4759a865516a4709d53..1a630bd5d5c0c3996943b33dac65c91c0625db44 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,15 @@ -all: display_stack +OPTION=-m32 + +all: display_stack try_mul display_stack : display_stack.o - gcc -o $@ $^ + gcc $(OPTION) -o $@ $^ + +try_mul : try.o try_mul.o + gcc $(OPTION) -o $@ $^ %.o:%.c - gcc -c $< + gcc $(OPTION) -c $< clean: - rm -f *.o *.s display_stack + rm -f *.o *.s display_stack try_mul diff --git a/try.c b/try.c new file mode 100644 index 0000000000000000000000000000000000000000..83ff446df9667ecc438843cdd4dac6f8177ab74f --- /dev/null +++ b/try.c @@ -0,0 +1,29 @@ +#include <stdlib.h> +#include <assert.h> +#include "try.h" + +int try(ctx_t *pctx, func_t *f, int arg) +{ + asm("movl %%esp, %0" + "\n\t" + "movl %%ebp, %1" + : "=r"(pctx->esp), "=r"(pctx->ebp)); + + return f(arg); +} + +int throw(ctx_t * pctx, int r) +{ + assert(pctx != NULL); + + static int r_tmp; + r_tmp = r; + + asm("movl %0 ,%%esp" + "\n\t" + "movl %1 ,%%ebp " + : + : "r"(pctx->esp), "r"(pctx->ebp)); + + return r_tmp; +} \ No newline at end of file diff --git a/try.h b/try.h new file mode 100644 index 0000000000000000000000000000000000000000..c440da3046dee763bc1c156086e487baef429d68 --- /dev/null +++ b/try.h @@ -0,0 +1,16 @@ +#if !defined(TRY) +#define TRY + +typedef int(func_t)(int); + +typedef struct ctx_s +{ + void *esp; + void *ebp; +} ctx_t; + +int try(ctx_t *, func_t *, int); + +int throw(ctx_t *, int); + +#endif // TRY