Floating point - absolute value - inline assembly - edited new code?

It is completely working..! You are just forgetting to put values to argv. You can run the program with something like this.

Up vote 2 down vote favorite share g+ share fb share tw.

I wrote a function named absD that I want to return the absolute value of its argument.. I am using GCC inline assembly with cygwin.. I don't see why its not working. I m loading into memory. Then into st(0) where I am using fabs - absolute value.

Do I have to allocate memory? I am trying to learn assembly with C here so please be nice. Please give me good help.

Thank you heres the code and then the error: #include #include #define PRECISION 3 double absD (double n) { asm( "fldl %nIn\n" "fabs\n" "fstpl %nOut\n" : nOut "=m" (n) : nIn "m" (n) ); return n; } int main (int argc, char **argv) { double n = 0.0; printf("Absolute value\n"); if (argc > 1) n = atof(argv1); printf("abs(%. *f) = %. *f\n", PRECISION, n, PRECISION, absD(n)); return 0; } here is the output: ~ $ gc a3 gcc -Wall -g a3.

C -o a3 ~ $ . /a3 Absolute value abs(0.000) = 0.000 ~ $ Not outputing its absolute value... Thank you.. c assembly link|improve this question edited Feb 15 '10 at 6:31 asked Feb 15 '10 at 5:05icelated612215 78% accept rate.

Why not just include math. H and use fabs( )? That will typically be at least as fast as what you have here.

If you want to go faster still, use the GCC-specific intrinsic __builtin_fabs( ). – Stephen Canon Feb 16 '10 at 1:22 I cant use Math. H – icelated Feb 16 '10 at 1:54 The absolute value of zero is zero.

What's the problem? – Stephen Canon Feb 16 '10 at 3:56.

It is completely working..! You are just forgetting to put values to argv. You can run the program with something like this: . /a3 -1.3 and it should return 1.3 you were done before posting it here...

Fld (%eax) means "load a float from the value at address %eax". Obviously, the contents of %eax are a double, and not a pointer to a float, which is why you segfault. Since the input is already on the stack (thus it has an address), there's no need to jump through hoops moving things around.

Double absD(double input) { double output; asm( "fldl %input\n" "fabs\n" "fstpl %output\n" : output "=m" (output) : input "m" (input) ); return output; } Also, your printf format is wrong: %f means float, but you're giving it a double; you want to use %g.

2 You're wrong about printf - because it's a varargs function, it's impossible to pass it a float (they're always promoted to double when part of the variable-argument-list). %f expects a double. – caf Feb 15 '10 at 5:48 ephemient - this doesn't work.. its ouputing 0.0 - just not its absolute value... – icelated Feb 15 '10 at 6:00.

(not sure why this was not flagged with a warning). Good luck.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions