![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4
![]() |
[x86] Arrays to functions
How do I call a function with an array as argument?
I have been written this code, but it does'nt work of course... #include <iostream>
void printArray(float m[])
{
std::cout << m[0] << std::endl;
std::cout << m[1] << std::endl;
std::cout << m[2] << std::endl;
}
int main()
{
float m1 = 3, m2 = 2, m3 = 9;
_asm
{
push m3 ; How should I prepare the floats?
push m2
push m1
call printArray
}
system("pause");
return 0;
}/Klarre |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
You create an array somewhere, and then you push the address of the array on the stack, IIRC.
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You may either push the address of the array onto the stack, or you may push the individual elements. In either case, the function needs to be aware of extent (and order). Pushing the address allows the contents of the original array to be modified from inside the function, pushing the individual values does not.
If you wish to deal with floats, your function must understand their construction internally, or use the/a floating-point processor.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 | |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4
![]() |
Quote:
/Klarre |
|
|
|
|
|
|
#5 | |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4
![]() |
Quote:
/Klarre |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You say you can only change the asm part, but you have printArray set up to accept an address. You can't do both, so make a decision.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4
![]() |
OK. It seems like it is not possible to do as I want. Therefor I post the actual problem I am trying to solve.
I am working on a lookAt function to an OpenGL application. The function will work as gluLookAt, with the same arguments. I did the implementation several months ago using C++, and now wants to convert it to assembly code. void Camera::lookAt(Vector3d eye, Vector3d center, Vector3d up)
{
// Creates an move/rotation matrix
Vector3d f(center.mX - eye.mX, center.mY - eye.mY, center.mZ - eye.mZ);
Vector3d s = (f.normalize()).cross(up.normalize());
Vector3d u = (s.normalize()).cross(f);
GLfloat m[] = {s.mX, u.mX, -f.mX, 0,
s.mY, u.mY, -f.mY, 0,
s.mZ, u.mZ, -f.mZ, 0,
0, 0, 0, 1};
glMultMatrixf(m);
glTranslatef(-eye.mX, -eye.mY, -eye.mZ);
}fx dd ? fy dd ? fz dd ? sx dd ? sy dd ? sz dd ? ux dd ? uy dd ? uz dd ? /Klarre |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I'm going to ignore the question, per se. If you are deciding to move from a high-level language implementation to an assembly-code implementation, you have to understand a number of different things. You have to understand how the high-level language maps its abstraction to machine code. You have to understand what capabilities machine code has at its own level. You have to understand how to shift your mental paradigm from one to the other. I can't know where you stand there. It isn't a big help that you SEEM to think that a call like, "void printArray(float m[])," is passing an array to a function. If you don't know what that's passing, and if you should decide to think it's a "pointer", which it also isn't, exactly, well, it just ain't so. You might want to read the small blurb referenced in my sig as "Pointer Basics", with particular attention to "What is NOT a pointer."
I'm not trying to discourage you unduly. There is just little point in following this up, if you aren't prepared. Please post back with additional elucidation.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 297
Rep Power: 4
![]() |
Thansk for your answears DaWei. As you wrote on your website that "An array is not a pointer" I am wondering if this is bad or not correct written code then. It works, and give me correct output though.
#include <iostream>
void printArray(const float* m) // Changed from float m[]
{
std::cout << m[0] << std::endl;
std::cout << m[1] << std::endl;
std::cout << m[2] << std::endl;
}
int main()
{
float m[] = {1.0f, 5.3f, 7.0f};
float* x = m;
_asm
{
push x
call printArray
}
system("pause");
return 0;
} |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
"x" is a pointer. "m" is the address of the first element of the array. "x" consequently points to "m". When you use the address of the array, "float []", the compiler knows the difference (one level of indirection) and generates code accordingly. It's the compiler's desire to make life easier for you that leads to confusion. We all know we can't assign a collection of objects to an array using the "=", but we all write, occassionally, "char myString [] = "abcdefg";" Again, the compiler is spoiling us. The compiler generates the array, element by element, as we'd have to do, if we did it at run-time. You can see why I call such things, "Confustoids."
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|