Valhalla Legends Forums Archive | C/C++ Programming | [C++] Inline Return

AuthorMessageTime
R.a.B.B.i.T
I've got some code (simply powers) which I'm inlining in assembly because I have nothing else to do right now.  I can't, however, get the result to be returned or printed to the console.  Can anyone help?

[code]#include <iostream.h>

void power2(int num, int power, unsigned int outl)
{
__asm
{
mov eax, num
mov ecx, power
mov ebx, outl
shl eax, cl
out eax, outl
}
}

int main()
{
power2(5, 3, (unsigned int) sizeof(cout));
return 0;
}[/code]
March 10, 2005, 4:36 PM
Arta
Out is for device IO. You need to call printf (or equivalent) from your assembly code, or just return the value and printf it in main (that would be better).
March 10, 2005, 5:14 PM
Adron
[quote author=rabbit link=topic=10877.msg103200#msg103200 date=1110472598]
I've got some code (simply powers) which I'm inlining in assembly because I have nothing else to do right now.  I can't, however, get the result to be returned or printed to the console.  Can anyone help?

[code]#include <iostream.h>

void power2(int num, int power, unsigned int outl)
{
__asm
{
mov eax, num
mov ecx, power
mov ebx, outl
shl eax, cl
out eax, outl
}
}

int main()
{
power2(5, 3, (unsigned int) sizeof(cout));
return 0;
}[/code]
[/quote]

I like the novel idea of getting the size of cout and using that as the output port to send the data to. Must've required some creative thinking :)

March 10, 2005, 6:56 PM
Myndfyr
[quote author=Adron link=topic=10877.msg103208#msg103208 date=1110480998]
I like the novel idea of getting the size of cout and using that as the output port to send the data to. Must've required some creative thinking :)
[/quote]
Heh I noticed that too.  I was wondering how that was supposed to work. :P
March 11, 2005, 1:48 AM
K
[code]
#include <cstdio>

unsigned int power2(int num, int power)
{
__asm
{
mov eax, num
mov ecx, power
shl eax, cl
}
// return value is in eax already
}

int main()
{
std::printf("%d\r\n", power2(x, y));
return 0;
}
[/code]
March 11, 2005, 2:21 AM
R.a.B.B.i.T
Thanks, K, that work's after some modification :)

I used sizeof(cout) because I knew out couldn't take an ostream as a parameter, but it didn't work ><
March 12, 2005, 2:48 PM
Zakath
The sizeof operator returns the size in bytes of its operand (or the length of the array if the operand is an array). Why would that be a valid device handle? It would just be the number of bytes an instance of class ostream occupies.

Think about what you're doing. :P
March 12, 2005, 11:08 PM
R.a.B.B.i.T
I needed to make it a number somehow :P
March 13, 2005, 1:08 AM
Kp
[quote author=Zakath link=topic=10877.msg103543#msg103543 date=1110668881]
The sizeof operator returns the size in bytes of its operand (or the length of the array if the operand is an array).[/quote]

So given this:[code]int x[10];
cout << sizeof(x) << endl;[/code], it should print 10? ;)  The length of the array is 10, but that's not its size...
March 13, 2005, 2:30 AM
Mephisto
[quote author=rabbit link=topic=10877.msg103571#msg103571 date=1110676123]
I needed to make it a number somehow :P
[/quote]

Perhaps you could've taken the address of an object of type cout?
March 13, 2005, 5:32 AM
R.a.B.B.i.T
I don't know that much C++!
March 13, 2005, 2:15 PM
Myndfyr
[quote author=rabbit link=topic=10877.msg103611#msg103611 date=1110723358]
I don't know that much C++!
[/quote]

If you don't know how to use pointers, you shouldn't be using C++ *or* assembly.
March 13, 2005, 8:27 PM
R.a.B.B.i.T
I know pointers, just not too well.  I'm still learning, remember.  And ACTUALLY, I learned Assembly first, during my electronics course.
March 13, 2005, 11:05 PM
Zakath
[quote author=Kp link=topic=10877.msg103581#msg103581 date=1110681027]
[quote author=Zakath link=topic=10877.msg103543#msg103543 date=1110668881]
The sizeof operator returns the size in bytes of its operand (or the length of the array if the operand is an array).[/quote]

So given this:[code]int x[10];
cout << sizeof(x) << endl;[/code], it should print 10? ;)  The length of the array is 10, but that's not its size...
[/quote]

My mistake. I was going from memory, and it appears that my memory has a leak in it.

It returns the total number of bytes occupied by the array, not the length of the array.
March 14, 2005, 1:40 AM
Mephisto
I was just curious though, why are you using inline assembler to create a power evaluation function?  Why not just use plain C++...

[code]unsigned long EvaluatePower(unsigned long base, unsigned long exponent)
{
if(exponent == 0)
return 1;
else if(exponent == 1)
return base;
else {
unsigned long value = 0;
unsigned long tracker = base;

for(unsigned int i = 1; i < exponent; i++) {
value = base * tracker;
tracker = value;
}

return value;
}
}[/code]

Note: Does not support negative exponents.  If you want this supported you'll have to change the values to type signed and come up with some thinking as to how to process a negative exponent (e.g. taking the recipricol of the evaluated power).
March 14, 2005, 2:56 PM
Adron
[quote author=Mephisto link=topic=10877.msg103769#msg103769 date=1110812212]
I was just curious though, why are you using inline assembler to create a power evaluation function?  Why not just use plain C++...

[code]unsigned long EvaluatePower(unsigned long base, unsigned long exponent)
{
if(exponent == 0)
return 1;
else if(exponent == 1)
return base;
else {
unsigned long value = 0;
unsigned long tracker = base;

for(unsigned int i = 1; i < exponent; i++) {
value = base * tracker;
tracker = value;
}

return value;
}
}[/code]

Note: Does not support negative exponents.  If you want this supported you'll have to change the values to type signed and come up with some thinking as to how to process a negative exponent (e.g. taking the recipricol of the evaluated power).
[/quote]

Well, that loop will be much slower than his power2 function for calculating numbers times powers of two. It would be much easier to just define it:

[code]
unsigned int power2(int num, int power)
{
return num << power;
}
[/code]

That is, if you at all bother to make it a subroutine :)

For a generic exponentiation function, you'd probably want to use floating point math and the built-in functions, since you'll run out of number range quickly when exponentiating ints.
March 14, 2005, 11:11 PM
R.a.B.B.i.T
I could switch over to unsigned double if I need to, but I don't.

The only reason I was doing this in assembly in the first place was to prove that I could prove to my friend that it's possible to write assembly code in MSVC++.  The only part I didn't quite understand was the return, but that's because the assembly I learned with output to some LED's, not a console.

On top of that, I like assembly :)
March 14, 2005, 11:19 PM
Mephisto
[quote author=Adron link=topic=10877.msg103843#msg103843 date=1110841887]
[quote author=Mephisto link=topic=10877.msg103769#msg103769 date=1110812212]
I was just curious though, why are you using inline assembler to create a power evaluation function?  Why not just use plain C++...

[code]unsigned long EvaluatePower(unsigned long base, unsigned long exponent)
{
if(exponent == 0)
return 1;
else if(exponent == 1)
return base;
else {
unsigned long value = 0;
unsigned long tracker = base;

for(unsigned int i = 1; i < exponent; i++) {
value = base * tracker;
tracker = value;
}

return value;
}
}[/code]

Note: Does not support negative exponents.  If you want this supported you'll have to change the values to type signed and come up with some thinking as to how to process a negative exponent (e.g. taking the recipricol of the evaluated power).
[/quote]

Well, that loop will be much slower than his power2 function for calculating numbers times powers of two. It would be much easier to just define it:

[code]
unsigned int power2(int num, int power)
{
return num << power;
}
[/code]

That is, if you at all bother to make it a subroutine :)

For a generic exponentiation function, you'd probably want to use floating point math and the built-in functions, since you'll run out of number range quickly when exponentiating ints.
[/quote]

Off hand, I don't think base << exponent would calculate the power.  Not sure what you meant by if you bother making it a subroutine.  :p
March 14, 2005, 11:43 PM
Adron
[quote author=Mephisto link=topic=10877.msg103850#msg103850 date=1110843834]
[quote author=Adron link=topic=10877.msg103843#msg103843 date=1110841887]
Well, that loop will be much slower than his power2 function for calculating numbers times powers of two. It would be much easier to just define it:

[code]
unsigned int power2(int num, int power)
{
return num << power;
}
[/code]

That is, if you at all bother to make it a subroutine :)

For a generic exponentiation function, you'd probably want to use floating point math and the built-in functions, since you'll run out of number range quickly when exponentiating ints.
[/quote]

Off hand, I don't think base << exponent would calculate the power.  Not sure what you meant by if you bother making it a subroutine.  :p
[/quote]

The routine this thread is about doesn't calculate powers though. His power2 routine calculates num * 2**power. If he were to use your routine to calculate num * 2**power, he'd be doing

[code]
#define power2(num, power)  ((num)*EvaluatePower(2, power))
[/code]

At least to me it's rather obvious that that's much slower than just shifting the number, and most people don't make a subroutine for shifting a number since there's a shifting operator available :)



March 15, 2005, 12:10 AM
Mephisto
[quote author=Adron link=topic=10877.msg103857#msg103857 date=1110845436]
[quote author=Mephisto link=topic=10877.msg103850#msg103850 date=1110843834]
[quote author=Adron link=topic=10877.msg103843#msg103843 date=1110841887]
Well, that loop will be much slower than his power2 function for calculating numbers times powers of two. It would be much easier to just define it:

[code]
unsigned int power2(int num, int power)
{
return num << power;
}
[/code]

That is, if you at all bother to make it a subroutine :)

For a generic exponentiation function, you'd probably want to use floating point math and the built-in functions, since you'll run out of number range quickly when exponentiating ints.
[/quote]

Off hand, I don't think base << exponent would calculate the power.  Not sure what you meant by if you bother making it a subroutine.  :p
[/quote]

The routine this thread is about doesn't calculate powers though. His power2 routine calculates num * 2**power. If he were to use your routine to calculate num * 2**power, he'd be doing

[code]
#define power2(num, power)  ((num)*EvaluatePower(2, power))
[/code]

At least to me it's rather obvious that that's much slower than just shifting the number, and most people don't make a subroutine for shifting a number since there's a shifting operator available :)




[/quote]

Oh, I didn't even realize what his function was doing.  I just assumed by the name of it he was trying to calculate power expressions generically.  My bad.
March 15, 2005, 1:48 AM

Search