Re: help with c++ if test

  • From: "Littlefield, Tyler" <tyler@xxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 09 Feb 2011 12:45:01 -0700

Wow, that's sweet. so essentially you just have the pointers to the functions or points to jump to, spaced out. so you would just xlat to the number you want to compare against, then jump to that. for things like ranges, you'd just repeat the pointer, makes sense.

On 2/9/2011 12:38 PM, Sina Bahram wrote:

In assembler you mean?

Wel, it's pretty simple. Take some of your conditionals, and then simply put them into your data segment by spacing out your data an equivalent spacing based on your conditional, so 32 steps away if you are checking for 32, and so on and so forth, then use xlat and a single jump to basically translate, jump, translate jump, and you've got yourself a super efficient, very fast piece of code, which has some extra stuff in the datasegment, but who cares.

Incidentally, this is the first trick I'd use if writing a piece of software I wouldn't want subject to static analysis. Dynamic would still handle it, especially with concepts like taint analysis, but static stands no chance.

Take care,

Sina

*From:*programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] *On Behalf Of *Littlefield, Tyler
*Sent:* Wednesday, February 09, 2011 2:35 PM
*To:* programmingblind@xxxxxxxxxxxxx
*Subject:* Re: help with c++ if test

xlat looks cool. You were mentioning that that's how you would optomize code, which I didn't understand after I read up on it. It's awesome for indexing into tables, but I'm not really sure how that helps in optomization.
On 2/9/2011 12:31 PM, Sina Bahram wrote:

Yes, good luck doing that in c, lol, haha.

Actually, what you really should do is use function pointers dereferenced out of arrays, using your input as a way of selecting where to jump to next.

And, oh my god, that actually is what they do ... still no goto, **smile**.

**sigh**, I miss the xlat instruction. That was my favorite op code.

Take care,

Sina

*From:* programmingblind-bounce@xxxxxxxxxxxxx <mailto:programmingblind-bounce@xxxxxxxxxxxxx> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] *On Behalf Of *Ken Perry
*Sent:* Wednesday, February 09, 2011 2:18 PM
*To:* programmingblind@xxxxxxxxxxxxx <mailto:programmingblind@xxxxxxxxxxxxx>
*Subject:* RE: help with c++ if test

Yes and instead of goto in kernel programming you really should use jumps.

ken

*From:* programmingblind-bounce@xxxxxxxxxxxxx <mailto:programmingblind-bounce@xxxxxxxxxxxxx> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] *On Behalf Of *Sina Bahram
*Sent:* Wednesday, February 09, 2011 2:11 PM
*To:* programmingblind@xxxxxxxxxxxxx <mailto:programmingblind@xxxxxxxxxxxxx>
*Subject:* RE: help with c++ if test

Yes, regardless, goto is bad.

But absolutely you can break out of the inner loop, but then you'll be in the top level loop, in which you can do a conditional.

This is still more efficient, since it is definitionally true that you would do a check in the inner loop before your goto, and if you are doing a check in the inner loop, then that means you are doing a check M*n times, where m and n are the dimensions of the outer and inner loops, respectively.

If you remove that, and simply do the check in the outer loop, then you are only doing a check m times, which is an entire order of complexity better.

Goto is bad. This is the one rule where because I say so and other inane excuses from teachers is acceptable. Goto is just bad. There is no reason, none, what-so-ever, that you need one.

Now, if we get into kernel programming where assembler is being linked in, etc, etc, then we can talk ... but in something as straight forward and earth shatteringly simple as the logic for an adventure game? Goto is absolutely in no way necessary under any possibly conceivable permutations.

Take care,

Sina

*From:* programmingblind-bounce@xxxxxxxxxxxxx <mailto:programmingblind-bounce@xxxxxxxxxxxxx> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] *On Behalf Of *Littlefield, Tyler
*Sent:* Wednesday, February 09, 2011 2:05 PM
*To:* programmingblind@xxxxxxxxxxxxx <mailto:programmingblind@xxxxxxxxxxxxx>
*Subject:* Re: help with c++ if test

Sina:
Break is awesome when you have one single loop, but you can't break out of two loops, can you? It was just the first thing that came to mind because I've seen it used (and used it) like that.

On 2/9/2011 11:50 AM, Sina Bahram wrote:

tyler, this is a horrible example of goto, my friend.

That's what break is for.

Take care,

Sina

*From:* programmingblind-bounce@xxxxxxxxxxxxx <mailto:programmingblind-bounce@xxxxxxxxxxxxx> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] *On Behalf Of *Littlefield, Tyler
*Sent:* Wednesday, February 09, 2011 1:17 PM
*To:* programmingblind@xxxxxxxxxxxxx <mailto:programmingblind@xxxxxxxxxxxxx>
*Subject:* Re: help with c++ if test

I really really highly recommend you avoid goto. This isn't basic, and they're not very useful except for in some odd cases, far and few between. Such as jumping out of two nested loops like so:
int i, j;
for (i = 0; i < 100; i++)
{
for (j = 0; j < 100; j++)
{
if (i+j == 100)
goto botttom;
}
}
bottom:
//do something here
if you want to compare strings, do something like this:
if (input == "north")
{
GoNorth();
}
else if (input == "south")
...
else ...



On 2/9/2011 10:52 AM, Kristoffer Gustafsson wrote:

Hi.

I'm writing if tests in c++ with the goto command.

I want to do text games and I'm using these commands for that.

But it doesn't work, so I must do something wrong.

Can you give me an example how this is done with a string please?

/Kristoffer

-- Thanks, Ty -- Thanks, Ty



--
Thanks,
Ty


--

Thanks,
Ty

Other related posts: