What's new

I want to learn some programming

Doomulation

?????????????????????????
You should not speak nonosense here. There is nothing that can be done with higher level languages that can't be done in assembly. Otherwise those higher level concepts wouldnot exist. But there are things in assembly that cant be done with higher level languages alone. Assembly has facilities for working with everything you mentioned there as impossible. Classes/data structures/source organization...everything. You name it. Of course, it may require you to understand what they really are. But thats about it. The motive behind higher level languages does not include being easier than assembly so that people can ignore its teachings. Most of the things on those higher level languages come from lower level assembly programming concepts. You understand asembly, you can flawlessly understand anything. I don't see how people can call somoene an expert and not a newbie who thinks assembly is hard. I can see why you think assembly is hard. You said it yourself..."learning all the opcodes." The way you went about learning it is wrong. Its not like reading a novel that you can skip to the middle and figure out how it was in the begining. I am guessing you never tried learning it because you used the word "opcode." I seriously think you are going about the wrong way here. I saw an example of DirectSound somewhere. I mean wtf. He does not even understand sound and you are preaching him DirectSound. I don't mean to be offensive but he will get bored and loose interest in programming soon enough thinking its hard.
I have tried assembly. I even tried inlining all code in functions in assembly for maximum speed and how does it turn out? HARD, HARD, HARD. Not easy. Yes, all the opcodes... how to convert numbers... knowing where you put your data... in registers or in memory... Thankfully we can inline assembly and put it into vars...
Tell me, is it possible to create and use an object in assembly without using C/C++? No? Too bad. The high level languages also handles things for you, like allocating space, aligning, offsets, etc.

Again, you seem to find assembly easy, but we DO NOT. Therefore, it is a good thing to learn a high level language FIRST, then learn assembly well enough to learn what goes on behind the scenes when you encounter problems that need assembly knowledge to solve.

Actually, I have made a few assembly hacks, so don't tell me I don't know anything.
 

Toasty

Sony battery
That would rather relate to "using programs without knowing how to code them." Not a logical extension.
If you choose to look at it that way, you may. (I see little difference between our illustrations.) However, there are thousands of successful programmers out there who have never touched assembly, so I think my point stands well enough on its own without an analogy. I thought assembly was neat when I was first learning about it, but when I discovered that the compiler could optimize just as well as I could about 99% of the time, I decided to go with the easier, less time-consuming option. Anyone who wants to try assembly as their first language is welcome to, but it definitely would have turned me off of programming right away.
 

civilian0746

evil god
Yes, they are the ones who gets fired for asking their boss stupid questions like what stack overflow is. No! Only dumb ones who has no idea about what compilers do tries to optimise their code with assembly. Well, you learn that with assembly as well, how compilers do what they do. Thats not why people learn assembly. They learn assembly to learn how things work. So they can understand what everything is all about. If you don't know already, computers make up a really tiny portion of programmable devices and I would say more than half the programmers out there has never touched c/c++ for their work and 1/4 of them never associated themselves with it in their lifetime. I would say average microprocessor speed today is still around 12 mhz or less and industry loves to see assembly more than anything (actually there is something industry loves more...thats the next level down from assembly. Depends on the application and we dont refer to it as programming anymore at that level).


I have tried assembly. I even tried inlining all code in functions in assembly for maximum speed and how does it turn out? HARD, HARD, HARD. Not easy. Yes, all the opcodes... how to convert numbers... knowing where you put your data... in registers or in memory... Thankfully we can inline assembly and put it into vars...
Apart from "all the opcodes," almost everything there is related to what you learn before you even start your hello world example. Actually, the hello world example in assembly is rather hard. Well, depends on the environment. If you are trying to write hello world on a lcd or something, you better be 3/4-expert in it before you even try. Beause you need to learn about semi advanced assembly concepts to do that. Assembly concepts are opcode independent. Those concepts apply equally for your home pc as well as your car. And no! For 99.99% of the cases, inline assembly is rather restricted to be of any use than making things worse. You are better off using your c or whatever compiler for them if you are trying to "optimise everything with assembly."


Tell me, is it possible to create and use an object in assembly without using C/C++? No? Too bad. The high level languages also handles things for you, like allocating space, aligning, offsets, etc.
You should not answer to your own questions too soon. Its more than possible to create and use objects in assembly including doing everything like field alignment and memory allocation. If it was impossible with assembly, OOP programming would not exist. Of course, mannually and you don't learn assembly to do OOP.


Again, you seem to find assembly easy, but we DO NOT. Therefore, it is a good thing to learn a high level language FIRST, then learn assembly well enough to learn what goes on behind the scenes when you encounter problems that need assembly knowledge to solve.

Actually, I have made a few assembly hacks, so don't tell me I don't know anything.
That last statement made me feel like you dont. You probably just learned some opcodes and patched your programs. I am guessing you did not get far by starting to learn in the middle.
 

Toasty

Sony battery
Only dumb ones who has no idea about what compilers do tries to optimise their code with assembly.
In my experience, there's very little incentive to use assembly other than optimization, though that's not to say it's its only use.
If you don't know already, computers make up a really tiny portion of programmable devices
Enlighten me; what might one of those other devices be?
...and I would say more than half the programmers out there has never touched c/c++ for their work and 1/4 of them never associated themselves with it in their lifetime. I would say average microprocessor speed today is still around 12 mhz or less...
I would say that you say "I would say" too much. (Where are your facts?) Just because a programmer doesn't program in C/C++ doesn't mean that s/he does program in assembly.
...and industry loves to see assembly more than anything (actually there is something industry loves more...thats the next level down from assembly. Depends on the application and we dont refer to it as programming anymore at that level).
Which industry is this? Who's "we"? What's this next level down from assembly? It doesn't get much lower-level than directly hand-picking the instructions you want the CPU to execute. If you're not talking about programming then what purpose does the comment have in a programming thread?

Admittedly, there are some areas (writing code for extremely limited hardware, for example) where coding in assembly is a must. Hobby programming for modern PCs is not typically one of these areas. While eventually learning a little assembly will help a programmer to understand what goes on 'under the hood' it's much more likely to cause nothing but confusion and frustration if s/he jumps right into it in the beginning. I liked Wikipedia's subheading on the current usage of assembly language. It summarizes what I'm trying to say very nicely.
 
Last edited:

civilian0746

evil god
In my experience, there's very little incentive to use assembly other than optimization, though that's not to say it's its only use.
Your experiance needs updating. I have rarely used and seen assembly on computers. Its totally not worth it. Even free compilers do better optimisations. And optimisation is rarely about faster code. There is no need to optimise "everything" to make things faster. In most cases, better algorithms can produce better results than those pityful assembly integration.


Enlighten me; what might one of those other devices be?
Everything from missiles fired today to the ovens used for cooking are programable. I am sure the world does not change that much in a week but last week I went to buy a small speaker so I could do some experimentation with sound on my brand new Spartan-3 board (I am a hobbyist programmer as well) and was not surprised at all to find a huge section of programmable toys in the shop. Not only toys, they have stuff for big kids as well. Already made up my mind for the next couple of birthday presents I am going to give ^_^


I would say that you say "I would say" too much. (Where are your facts?) Just because a programmer doesn't program in C/C++ doesn't mean that s/he does program in assembly.
I never said they do. It would not be hard to find people who would say "c is hard." Its not hard damnit. If I can do it, everyone can!


Which industry is this? Who's "we"? What's this next level down from assembly? It doesn't get much lower-level than directly hand-picking the instructions you want the CPU to execute. If you're not talking about programming then what purpose does the comment have in a programming thread?
It does not? Think again! You might figure out something. It might not be called "programming" but what they do is still programming. Of course, they come out with proper computer engineering degree studying everything about computers and related systems than just a couple of programming languages and grade 12 algorithms.


Admittedly, there are some areas (writing code for extremely limited hardware, for example) where coding in assembly is a must. Hobby programming for modern PCs is not typically one of these areas. While eventually learning a little assembly will help a programmer to understand what goes on 'under the hood' it's much more likely to cause nothing but confusion and frustration if s/he jumps right into it in the beginning. I liked Wikipedia's subheading on the current usage of assembly language. It summarizes what I'm trying to say very nicely.
They arent "some." They are most areas. I have only one computer in my room. Everything else electronic from my mobile phone, pda, the loose chip on my arcade stick...to my calculator is programmable where I have the option to reprogram or inject sub-programs compiled from their native assembly code with proper tools. Of course, some of them have higher level scripting options in them as well but they are too slow if you want to do something like make a game for it.

I never considered wikipedia to be credible but it summarizes most of what I have to say:
Wikipedia said:
* When a stand-alone binary executable is required, i.e. one that must execute without recourse to the run-time components or libraries associated with a high-level language;this is perhaps the most common situation
* When interacting directly with the hardware, e.g. in a device driver, or when using processor-specific instructions not exploited by or available to the compiler
* When extreme optimization is required, e.g. in an inner loop in a processor-intensive algorithm
* When a system with severe resource constraints (e.g. an embedded system) must be hand-coded to maximize the use of limited resources; but this is becoming lessmore common as processor price/performance improves
* When no high-level language exists, e.g. on a new or specialized processor <--most common case
Has a few glitches but good enough. Most c compilers Ive come across outside the world of computers suck badly. You can do better job with broken assembly. Still, c is not low level. If C is low level, the only things that deserves to be called high level are parsed languages like php and javascript which has outrageous higher level extensions. Even with java, the end code that is executed today is comparable to the time on c. I am not saying c does not have non-standard platform specific extensions. But you would need to understand its corresponding assembly concepts to be able to use them and still it would not be the same.
 

Doomulation

?????????????????????????
It's pointless to argue, but I have sometimes used asm to get around C++ language limitations. Such as callback to either a function OR a function within a class all within the same function. It's not possible since C++ does not allow you to cast pointers to function a generic type like void (without assembly).
And again - not using inline asm makes things even more complicated. Asm is hard and I learned it only because of a little interest - not because of what you say. I have learned enough to make out some of what the compiler generates and it helps me in certain situations when something has gone awry and I can't see what it is from the C++ code.
 

Cyberman

Moderator
Moderator
(sigh)

[mild mod mode on]
Toasty, your experience isn't likely what other peoples experience are, your real world is not the complete world nor does it encompass all knowledge. Please try to be a little more sensible in your thinking. Just because you (personally) have had little use of assembly, in the areas you work in doesn't mean that is the end all be all of experience and existance for the subject.

I am giving you a hint to stop being beligerant and actually listen. If something doesn't sit well, stop, sit back and think, do some research. Then think again before saying anything.

I personally AVOID using assembly myself but have used it extensively.

civilian0746 relax a bit, use less 'stress' filled words. This is NOT a bar room brawl both of you.
[/end mild mod mode]

Ok both of you are a bit out of control SO. I'm going to be nice and say to Toasty, you've gone out of your realm of understanding, do some research then DISCUSE the topic please. You seem to have experience with some things, but what you have said is enough to really give the idea you have no experience with anything less complicated than a modern game console, which is far more complicated than the PC's of the mid 80's.

With some devices assembly is prefered since you have < 1K program word space. A good place to enlighten yourself a bit is Microchip's web page, and a few others, Intel, Amd, Freescale, Zilog, Philips Semi (now NXP), and Atmel. I'm no fan of Microchip but I've used there processors. You can use C with a lot of there CPU's (mcu's to be precise) and one will find that the uChip IDE and C is extremely capabable of low level functionality. In fact you can compile an entire program in C without an OS for it to run on. As I have done a myriad of times.

I've even built a PC-104 board using the AMD186ER a fake DOS (IE just enough to load the program in ROM) to run an HVAC system (Heating Ventilating and Air Conditioning).

Cyb
 

Toasty

Sony battery
I apologize - it wasn't my intention to imply that everyone's experience was similar to mine. For the most part, I program as a hobby, which I'm guessing (correct me if I'm wrong) is the initial goal of the original poster. It looks like I got a little too wrapped up and forced you to go into 'mild mod mode', so I'm sorry about that, and also that I came across as hostile. :( I do and did admit that assembly has its uses (in some of which nothing else is even plausible) - like you pointed out; I just felt it would be confusing to a beginner.

@civilian0746: I won't perpetuate the debate, but in my own defence, those other programmable devices are what I would consider computers. (I guess that was a misunderstanding.) All in all, the way you came across just made me feel as though you were imposing opinion as fact (perhaps another misinterpretation by me). Since our discussion warranted moderation, I'll agree to disagree if you will. :p
 

civilian0746

evil god
It's pointless to argue, but I have sometimes used asm to get around C++ language limitations. Such as callback to either a function OR a function within a class all within the same function. It's not possible since C++ does not allow you to cast pointers to function a generic type like void (without assembly).
And again - not using inline asm makes things even more complicated. Asm is hard and I learned it only because of a little interest - not because of what you say. I have learned enough to make out some of what the compiler generates and it helps me in certain situations when something has gone awry and I can't see what it is from the C++ code.

I dont know what you mean by "a function within a class all within the same function" but I am preety sure it does not make sense in that formatting. As for rest, c++ is one of the languages that actually supports proper function pointers and callbacks to function is more than possible without assembly and even without any typecasting. Void isnot a generic type in c++. Void simply means nothing. Its size is 0. Its unusable even in assembly. The only reason you can have pointers to void is because all pointers have the same size equal to the size of machine's address regs and matters little about what it points to.
 

Doomulation

?????????????????????????
I dont know what you mean by "a function within a class all within the same function" but I am preety sure it does not make sense in that formatting. As for rest, c++ is one of the languages that actually supports proper function pointers and callbacks to function is more than possible without assembly and even without any typecasting. Void isnot a generic type in c++. Void simply means nothing. Its size is 0. Its unusable even in assembly. The only reason you can have pointers to void is because all pointers have the same size equal to the size of machine's address regs and matters little about what it points to.

Try to make a template function that accepts an argument of type T that is supposed to be a function pointer. In your code, however, calling a function or a class function (a function declared within a class) does not have the same syntax and therefore, the compiler WILL complain either way you do it, because it parses code that is not possible to execute.

Code:
template<typename Fnc, typename Class> void MyFunc(T fptr, T* pClass)
{
if (pClass == NULL) fptr(); // If it's a class function pointer, this fails
else (pClass->*fptr)(); // If it's a function pointer, this fails
}

I cannot cast the pointer to the correct type either, because I don't know what type it is. So either way, one of those lines WILL fail!
I can't cast a function pointer to type void* either, so I can't accept arguments as void*. Even if I could, I wouldn't know what type it was, so it would still be impossible.
Is this solvable? It sure is. With assembly. A hack.
 

civilian0746

evil god
Try to make a template function that accepts an argument of type T that is supposed to be a function pointer. In your code, however, calling a function or a class function (a function declared within a class) does not have the same syntax and therefore, the compiler WILL complain either way you do it, because it parses code that is not possible to execute.

Code:
template<typename Fnc, typename Class> void MyFunc(T fptr, T* pClass)
{
if (pClass == NULL) fptr(); // If it's a class function pointer, this fails
else (pClass->*fptr)(); // If it's a function pointer, this fails
}

I cannot cast the pointer to the correct type either, because I don't know what type it is. So either way, one of those lines WILL fail!
I can't cast a function pointer to type void* either, so I can't accept arguments as void*. Even if I could, I wouldn't know what type it was, so it would still be impossible.
Is this solvable? It sure is. With assembly. A hack.
Mods, please excuse the following section
[section to be excused]
That is the most retarded piece of code I have ever seen. You weren't trying extend beyond what you can do with c++, you were trying to cover your stupidty with more stupidity. Seriously, what do you smoke? I am sure its nothing as sane as weed.
[/section to be excused]

As for your problem, its easily fixed with overloading the templated function.
Code:
template<class T, class CLASS> void MyFunc(T fptr, CLASS* pClass)
{
	(pClass->*fptr)();
}
//fptr_type_exceptional = your normal function pointer type
template<class T, class CLASS> void MyFunc(fptr_type_exceptional fptr, CLASS* pClass)
{
	fptr();
}
Not a single cast there.
 

Doomulation

?????????????????????????
Excuse ME for saying this:
This is standard code which is NOT retarded and if I am retarded then YOU are retarded.
What is wrong with implementing callback? I don't want two functions for the same purpose: I want one.
I know full well of overloads but I do not WANT to use them, IF possible. With assembly, I don't have to.
 

civilian0746

evil god
Excuse ME for saying this:
This is standard code which is NOT retarded and if I am retarded then YOU are retarded.
I never said you were. Thats not standard code. Thats the stupidest piece of thing someone could ever think of. None of the 2 lines you had within that fuction makes any sense whatsoever and will only work for a tiny ammount of cases even if c++ supported them and you managed to compile or hacked it with assembly. This is not only invalid, but even if it was valid, a rather retarded style of programming.

What is wrong with implementing callback? I don't want two functions for the same purpose: I want one.
Its not 2 functions. They are template functions. In a nutshell, they are like macros. Compiler generates functions for all combinations of template parameters and it would not be surprising to have 10-20 versions of them in the final piece of code that is translated into assembly for compilation. If you are after smaller code, its a bad idea to use them anyway.

There is nothing wrong with implementing callbacks. But everything wrong with implementing retarded callbacks. What are you trying to achieve there anyway. I am sure what you had there translates to nothing but stupidity even in the assembly level.

I know full well of overloads but I do not WANT to use them, IF possible. With assembly, I don't have to.
I know. Just in case things become faster without an extra conditional. Why would anyone want that. They rather do things the retarded way like yourself and do "assembly hacks."
 

zAlbee

Keeper of The Iron Tail
@civilian0746: Look dude, cool it. It's clear that you have a lot of knowledge and experience, but that does not entitle you to blatantly insult other members. You can hide all you want under [mod-excusable] tags, but that does NOT stop you from offending people.

Which industry is this? Who's "we"? What's this next level down from assembly? It doesn't get much lower-level than directly hand-picking the instructions you want the CPU to execute. If you're not talking about programming then what purpose does the comment have in a programming thread?

It does not? Think again! You might figure out something. It might not be called "programming" but what they do is still programming. Of course, they come out with proper computer engineering degree studying everything about computers and related systems than just a couple of programming languages and grade 12 algorithms.

He's on about circuits, logic gates and the like (probably my jargon is incorrect, but I am not an engineer).

C is a high-level programming language (or not): It's not a friggin' binary classification of high/low or left/right or black/white. We all know where C sits in the spectrum. As far as I can tell, you would like to group C with high-level because it makes you feel better, knowing that "low-level" is an exclusive club where only you and other advanced ASM programmers can hang out.

Yes, they are the ones who gets fired for asking their boss stupid questions like what stack overflow is. No! Only dumb ones who has no idea about what compilers do tries to optimise their code with assembly. Well, you learn that with assembly as well, how compilers do what they do. Thats not why people learn assembly. They learn assembly to learn how things work. So they can understand what everything is all about.
Although this statement is ridiculously opinionated and offensive, I'm going to agree with you on this one. Any good programmer should have knowledge of computer achitecture and how shit works, and this includes knowing Assembly. Even if they don't start off with a clue, eventually anyone who is serious about programming must get interested in it at some point.

Whether you should learn ASM as a starting point? I don't think it matters. A newbie should start out learning with a guide that makes sense, on a language that encourages them. If ASM excites you, then congrats. But very likely if someone doesn't see any good progress with ASM or they "can't make useful programs," then they will lose interest. You said it yourself, "he will get bored and loose interest in programming soon enough thinking its hard." In any case, if you want to be a GOOD programmer, you will need to spend the time learning both the low level and the high level (which is most likely what you will be programming in). That's what separates the good programmer from the guy who gets fired (other than being intelligent); the order of learning doesn't matter, except for bragging rights.

This thread: should be split. This debate over the usefulness of Assembly has nothing to do with the xneoangel's request for programming help.
 

civilian0746

evil god
I don't mean to offend anyone. Please excuse me if anyone found something offensive. I think Doom needs to understand the motivation behind some higher level ideas so he can appreciate them properly.
 

Toasty

Sony battery
He's on about circuits, logic gates and the like (probably my jargon is incorrect, but I am not an engineer).
Thanks for clarifying. ;) I suspected he might be talking about something like that, but like I said, I didn't think it was really relevant to a programming topic. Hopefully I haven't come across as too big of a jerk in this thread; Cyberman's words cut me to the quick when he said I was outside of my realm of understanding. (For the record, I do understand this stuff quite well, but I guess a little too much attitude and some poorly chosen wording made me look like a ranting noob. :p)
 

Doomulation

?????????????????????????
I never said you were. Thats not standard code. Thats the stupidest piece of thing someone could ever think of. None of the 2 lines you had within that fuction makes any sense whatsoever and will only work for a tiny ammount of cases even if c++ supported them and you managed to compile or hacked it with assembly. This is not only invalid, but even if it was valid, a rather retarded style of programming.

Its not 2 functions. They are template functions. In a nutshell, they are like macros. Compiler generates functions for all combinations of template parameters and it would not be surprising to have 10-20 versions of them in the final piece of code that is translated into assembly for compilation. If you are after smaller code, its a bad idea to use them anyway.

There is nothing wrong with implementing callbacks. But everything wrong with implementing retarded callbacks. What are you trying to achieve there anyway. I am sure what you had there translates to nothing but stupidity even in the assembly level.

I know. Just in case things become faster without an extra conditional. Why would anyone want that. They rather do things the retarded way like yourself and do "assembly hacks."

Why is it retarded? I find it retarded that I can't call both a class function and a global function within the same function scope without using overloads. A class function pretty much just takes the address of the class as its first parameter.
And whatever you say, template or not, two functions are overloads even if the two does not make it into the source.
Also it's a question of style. I have sometimes used assembly simply because I find the C++ alternative annoying and want to get around it.
 

civilian0746

evil god
Why is it retarded? I find it retarded that I can't call both a class function and a global function within the same function scope without using overloads. A class function pretty much just takes the address of the class as its first parameter.
And whatever you say, template or not, two functions are overloads even if the two does not make it into the source.

It's retarded because it doesnot even make any logical sense. The following code is the same as the code you had there:
Code:
template<typename Fnc, typename Class> void MyFunc(T fptr, T* pClass)
{
(pClass->*fptr)();
}
If it was not retarded, the following would have made sense since both are "functions" and pClass is merely a pointer.

Using simpler convention, consider a function that takes 2 numbers a and b and returns its result:
Code:
mult (a, b) = a * b
This works quite well for fields which can be casted to the real type for which the above function is defined. It would make sense to have something like:
Code:
z = mult(y, z) where x, y, z in [B]R[/B] or [B]N[/B] or [B]I[/B] or [B]Z[/B]
But how can you expect it to relate to something like class which are like special fields such higher dimentional numbers or say a set of polynomials. The following wouldnot make any sense at all:
Code:
z = mult(y, z) where x, y, z in [B]C[/B]
Unless you define a special function to work on that special field, e.g. a overloaded function, there is no scope where this is possible. And when you define an overloaded function specialized for that field, it wont work for fields mentioned before. Hence it is retarded without overloads.


Not even going as far as that, even if you look in terms of c++ programming, you had the callback as a template type. I.e. its supposed to work with all types of template parameters. Hence something like "(pClass->*fptr)();" does not make sense to be in your templated function even if its a "class function pointer." Its a totally invalid use of template functions. Or I should rather say, "pure stupidity." If compilers could interpret your assembly hacks, I am sure it would not let something this stupid compile.


Also it's a question of style. I have sometimes used assembly simply because I find the C++ alternative annoying and want to get around it.
Its not a question of style. If it was not retarded, it would have been there. Nothing stops you from using labels and goto in c++ even thou its considered bad style of programming because even thou its considered bad, its perfectly valid. What you did there was purely invalid. You can't treat specialized methods the same way as normal functions. Because they are totally different things. Looks like you did not even learn enough about C++ to figure out whats real. There is nothing in c++ standards that says they need to be the same. There is nothing in c++ standards that says it needs to be implemented like taking the address of the object as first parameter or saving the address in a predetermined register. It would not matter if the compiler generates code that saves the address of object in a special file and the method reads them back for usage. There are places where you would need assembly with c++ and something this retarded isnot just one of them. And no, I am not saying this just because its not in the wikipedia list.
 

Doomulation

?????????????????????????
Let's just say I don't get your arguments 100% and I don't care really. I tend to use Goto in my code because, even though it is considered Evil(TM), I find it very useful. And does it matter HOW you do it, as long as the result is the same?

Tell me, how would you make a function that generates a new thread that starts in a function in a class of your choice (OR simply a function of your choice and no class). It would also take one optional argument of YOUR CHOICE. How would you do this? I would use a template function to get class, function and argument. Then I would use assembly and put the class offset into ecx (as defined by the msvc compiler) and call the function. I would also, of course, push the optional param onto the stack so that the function finds it.

If I should simply use a function that makes a thread that starts in function in your class of choice, I could it without assembly, with C++ standard techniques.
Code:
(pClass->*pFnc)(pParam);

Is this code retarded? And if it is, then how would you do it?

Oh and, I can't say I've learned everything by the book. I have always taught myself most of things and always went my own way.
 
Last edited:

civilian0746

evil god
Let's just say I don't get your arguments 100% and I don't care really. I tend to use Goto in my code because, even though it is considered Evil(TM), I find it very useful. And does it matter HOW you do it, as long as the result is the same?

Tell me, how would you make a function that generates a new thread that starts in a function in a class of your choice (OR simply a function of your choice and no class). It would also take one optional argument of YOUR CHOICE. How would you do this? I would use a template function to get class, function and argument. Then I would use assembly and put the class offset into ecx (as defined by the msvc compiler) and call the function. I would also, of course, push the optional param onto the stack so that the function finds it.

If I should simply use a function that makes a thread that starts in function in your class of choice, I could it without assembly, with C++ standard techniques.
Code:
(pClass->*pFnc)(pParam);

Is this code retarded? And if it is, then how would you do it?

Oh and, I can't say I've learned everything by the book. I have always taught myself most of things and always went my own way.
Same here. I've tought myself as well ^_^. Its really funny but I am yet to get myself a proper programming book. The only programming book I have is this book on file structures written in 1992 which i bough from some junk book shop from $5 (bought with 2 other books...total was $15). It has some interesting info on how HD organizes stuff but is irrrelevent I guess. I've always considered them as nothing more than waste of money and is only good for stating the obvious.


As for your problem, here is a solution. Compiles fine with msvc++ 8.0. Don't know how gcc handles methods. Look at it carefully, you might learn something obvious:
Code:
#include "stdio.h"
#include "windows.h"




//modified java style declaration for classes since they are different...
class ThrParent {
public:
	//int ThrProc(void* lpParameter){return 0;}
};




//standard declaration we will accept for thread procs
typedef int (__stdcall * ThrProcNormalFcnType)(void*);
typedef int (__thiscall ThrParent:: *ThrProc) (void*);





//a sample structure to Handle thread data
struct ThreadData {
	union {
		ThrParent * clas;
		void * ClassAddr;
	} base;

	union {
		ThrProc cfcn;
		ThrProcNormalFcnType nfcn;
		void * vptr;
	} func;

	void * parameter;

};




//main thread procedure
DWORD WINAPI ThreadProc(LPVOID lpParameter){
	ThreadData * data = (ThreadData *)lpParameter;
	if (data->base.ClassAddr==0)
		return (*data->func.nfcn)(data->parameter);
	else
		return (data->base.clas->*data->func.cfcn)(data->parameter);

}



/*
	a var for last thread info...if you come across the possiblity of creating
	multiple threads at the same time from multiple threads that there might 
	overwrite uncreated thread data, replace it with a threa safe list.
*/
ThreadData LastThreadInfo;




//the thread creation function
//assumption: parameter tParent will be 0 if its not a class
template<class CLASS, class FCN_CLASS> void CreateThr(CLASS * tParent, FCN_CLASS function, void * lpParameter) {

	union {
		FCN_CLASS f;
		ThrProcNormalFcnType v;
	};

	union {
		CLASS * c;
		ThrParent * vc;
	};

	f = function;
	c = tParent;

	LastThreadInfo.base.clas = vc;
	LastThreadInfo.func.nfcn = v;
	LastThreadInfo.parameter = lpParameter;

	DWORD t;

	CreateThread(0, 0, &ThreadProc, &LastThreadInfo, 0, &t);

}


//a normal function
int __stdcall RandomprocedureCall(void * string) {
	printf("String is %s\n", string);
	return 0;
}

//a class and its thread function
class RandomClassProcCallExample {
public:
	int ThrProcExample (void* lpParameter);
};
int RandomClassProcCallExample::ThrProcExample (void* lpParameter){
	printf("String is %s\n", lpParameter);
	return 0;
}


//code entry point
int main(){
	printf("Start\n");

	//first create a normal thread
	CreateThr<RandomClassProcCallExample, ThrProcNormalFcnType>(0, RandomprocedureCall, "Die! Just Die!");

	//wait for a while since we used LastThreadInfo instead of a proper list
	Sleep(500);

	//class thread
	RandomClassProcCallExample x;
	CreateThr<RandomClassProcCallExample, ThrProc>(&x, (ThrProc)&RandomClassProcCallExample::ThrProcExample, "This is inside a class");

	Sleep(500);
	printf("End\n");
}
Process both function type and method type parameters. Replace the __stdcalls with whatever calling convention you want your callback functions to use. No assembly needed ^_^.
 

Top