• Welcome to Valhalla Legends Archive.
 

[C++] Inline Return

Started by R.a.B.B.i.T, March 10, 2005, 10:36 AM

Previous topic - Next topic

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?

#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;
}

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).

Adron

Quote from: rabbit on March 10, 2005, 10:36 AM
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?

#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;
}


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 :)


MyndFyre

Quote from: Adron on March 10, 2005, 12:56 PM
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 :)
Heh I noticed that too.  I was wondering how that was supposed to work. :P
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

K


#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;
}

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 ><

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
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

R.a.B.B.i.T

I needed to make it a number somehow :P

Kp

Quote from: Zakath on March 12, 2005, 05:08 PM
The sizeof operator returns the size in bytes of its operand (or the length of the array if the operand is an array).

So given this:int x[10];
cout << sizeof(x) << endl;
, it should print 10? ;)  The length of the array is 10, but that's not its size...
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Mephisto

Quote from: rabbit on March 12, 2005, 07:08 PM
I needed to make it a number somehow :P

Perhaps you could've taken the address of an object of type cout?

R.a.B.B.i.T

I don't know that much C++!

MyndFyre

Quote from: rabbit on March 13, 2005, 08:15 AM
I don't know that much C++!

If you don't know how to use pointers, you shouldn't be using C++ *or* assembly.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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.

Zakath

Quote from: Kp on March 12, 2005, 08:30 PM
Quote from: Zakath on March 12, 2005, 05:08 PM
The sizeof operator returns the size in bytes of its operand (or the length of the array if the operand is an array).

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

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.
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Mephisto

I was just curious though, why are you using inline assembler to create a power evaluation function?  Why not just use plain C++...

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;
}
}


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).