diff --git a/miosix/libsyscalls/memoryprofiling.cpp b/miosix/libsyscalls/memoryprofiling.cpp
index 7bf144ca9f12278681564501fce388f702ed324b..7014a57c9d8426fbd6427279c188f890191b487e 100644
--- a/miosix/libsyscalls/memoryprofiling.cpp
+++ b/miosix/libsyscalls/memoryprofiling.cpp
@@ -107,7 +107,15 @@ unsigned int MemoryProfiling::getAbsoluteFreeStack()
 unsigned int MemoryProfiling::getCurrentFreeStack()
 {
     unsigned int stackOccupiedByCtxsave=sysconf(ctxsaveOnStack);
-    int *stack_ptr asm("sp");
+    #ifdef __ARM_EABI__
+    void *stack_ptr;
+    asm volatile("mov %0, sp" : "=r"(stack_ptr));
+    #else //__ARM_EABI__
+    //NOTE: __builtin_frame_address doesn't add the size of the current function
+    //frame, likely __builtin_stack_address would be better but GCC 9.2.0 does
+    //not have it
+    void *stack_ptr=__builtin_frame_address(0);
+    #endif //__ARM_EABI__
     const unsigned int *walk=getStackBottom();
     unsigned int freeStack=(reinterpret_cast<unsigned int>(stack_ptr)
                           - reinterpret_cast<unsigned int>(walk));
diff --git a/miosix/util/util.cpp b/miosix/util/util.cpp
index ac8b961747146814ae2c965706e026932874213a..1c24afe4a6c2690b1c656c3d106b4ae6f5ecdcbe 100644
--- a/miosix/util/util.cpp
+++ b/miosix/util/util.cpp
@@ -94,7 +94,15 @@ unsigned int MemoryProfiling::getAbsoluteFreeStack()
 
 unsigned int MemoryProfiling::getCurrentFreeStack()
 {
-    int *stack_ptr;
+    #ifdef __ARM_EABI__
+    void *stack_ptr;
+    asm volatile("mov %0, sp" : "=r"(stack_ptr));
+    #else //__ARM_EABI__
+    //NOTE: __builtin_frame_address doesn't add the size of the current function
+    //frame, likely __builtin_stack_address would be better but GCC 9.2.0 does
+    //not have it
+    void *stack_ptr=__builtin_frame_address(0);
+    #endif //__ARM_EABI__
     const unsigned int *walk=Thread::getStackBottom();
     unsigned int freeStack=(reinterpret_cast<unsigned int>(stack_ptr)
                           - reinterpret_cast<unsigned int>(walk));