From 3ae355cdcc53b6cf41e25da095b54718ac42567f Mon Sep 17 00:00:00 2001 From: Thierno-Souleymane BAH <thiernosouleymane.bah.etu@univ-lille.fr> Date: Fri, 15 Jan 2021 07:18:05 +0100 Subject: [PATCH] feat(try() and throw() functions done) --- Makefile | 13 +++++++++---- try.c | 29 +++++++++++++++++++++++++++++ try.h | 16 ++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 try.c create mode 100644 try.h diff --git a/Makefile b/Makefile index 25b8148..1a630bd 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 0000000..83ff446 --- /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 0000000..c440da3 --- /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 -- GitLab