ScoreHero
Home | Forum | Wiki
Inbox [ Login ]Inbox [ Login ]
SearchSearch MemberlistMemberlist
ProfileProfile Log inLog in
The Programming Thread / Project Euler Thread
Goto page Previous  1, 2, 3 ... 24, 25, 26 ... 28, 29, 30  Next
 
Post new topic   Reply to topic    ScoreHero Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Mon Nov 14, 2011 2:40 pm    Post subject: Reply with quote

raynebc wrote:
I haven't done assembly in a while, none of that looks odd to me except for what I'm assuming is a couple redundant mov operations at the end (since eax already has the answer, do you need to move it to a memory variable and back)? Is the type of crash indicative of anything, ie. is it a segment fault?


Yea, the only reason I did that is because that's how it was done when I looked at the disassembler in Visual Studio, so I thought "Better Safe than Sorry". If I can get it working, then yeah, I definitely wouldn't move it twice like that.



That's the error it's giving me. No idea what it's trying to say.
_________________
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
raynebc  





Joined: 16 Jun 2008
Posts: 992

PostPosted: Mon Nov 14, 2011 3:37 pm    Post subject: Reply with quote

It's some kind of NULL pointer dereference. Are you doing any odd stack/memory manipulation outside the bit of assembly code you posted?
Back to top
View user's profile Send private message
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Mon Nov 14, 2011 3:39 pm    Post subject: Reply with quote

raynebc wrote:
It's some kind of NULL pointer dereference. Are you doing any odd stack/memory manipulation outside the bit of assembly code you posted?


Nope. The only thing I'm doing outside of that is just I/O asking for the vectors, and passing those along with the function call.
_________________
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
raynebc  





Joined: 16 Jun 2008
Posts: 992

PostPosted: Mon Nov 14, 2011 7:40 pm    Post subject: Reply with quote

If it only crashes when you return from the function, that's what makes me think it's a corrupted stack. If you can inspect the stack in the debugger, do you notice anything weird going on, such as the wrong address being popped off the stack looking incorrect when it goes to return to the calling function?
Back to top
View user's profile Send private message
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Mon Nov 14, 2011 10:21 pm    Post subject: Reply with quote

raynebc wrote:
If it only crashes when you return from the function, that's what makes me think it's a corrupted stack. If you can inspect the stack in the debugger, do you notice anything weird going on, such as the wrong address being popped off the stack looking incorrect when it goes to return to the calling function?


I'm not exactly sure what I'm looking for. Here is a pastebin of the full code if you can run it and see if you find anything wrong: http://pastebin.com/vPMfuwUw

Like I said, all of the arithmetic works just fine, but as soon as it runs the ret statement, it crashes. Not sure why since my assembly code is pretty much the EXACT same as the assembly code I see when I look at the disassembler when using the regular C++ code.

EDIT: If I step into the Ret statement, it goes to a line saying
Quote:
add dword ptr [eax],eax
throws out an error, and if I hit continue, it goes to
Quote:
return(ptd);
0FF4A86E mov eax,dword ptr [ptd]
and just stops there, and doesn't go any further if I hit continue. If that helps at all >_>

EDIT 2:



This is a picture of the Registers and the call stack when the error is first thrown after stepping into the ret function. Like I said, not sure what I'm looking for on the call stack.

If I keep hitting Continue on the Error dialog, it just keeps popping up, but the ESP keeps getting decremented non-stop.

EDIT 3: Okay, so. When I store the asnwer into EAX (In this case, 6), it apparently gives EAX the ADDRESS of 6 (As in, EAX = 00000006), which I believe is why I'm getting a violation writing access at 0x00000006. Any ides on how to fix it?
_________________
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
raynebc  





Joined: 16 Jun 2008
Posts: 992

PostPosted: Tue Nov 15, 2011 12:37 am    Post subject: Reply with quote

That's the only thing that makes sense (that it's writing to memory instead of the register), but in my limited understanding of assembly, that would have to be something like:
Code:
mov [eax], answer


I'm not sure why the code that was created during compilation includes that kind of memory manipulation, it should be returning the value through a register, not performing operations on [eax].
Back to top
View user's profile Send private message
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Tue Nov 15, 2011 1:11 am    Post subject: Reply with quote

raynebc wrote:
That's the only thing that makes sense (that it's writing to memory instead of the register), but in my limited understanding of assembly, that would have to be something like:
Code:
mov [eax], answer


I'm not sure why the code that was created during compilation includes that kind of memory manipulation, it should be returning the value through a register, not performing operations on [eax].


Yea, I'm not exactly sure why it's doing that. It seems like my code is just fine >_> Hopefully someone with more Assembly knowledge drops by and can help. If not, I'll just have to ask my professor and see if he can spot any errors.
_________________
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Wed Nov 16, 2011 3:56 pm    Post subject: Reply with quote

Bump, still having the error. Got my class today, so I'll see if I can get the teacher to help me out if no one here does.
_________________
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Thu Nov 17, 2011 12:26 am    Post subject: Reply with quote

Well, my teacher figured out the problem... I wasn't suppose to translate "return answer;" into assembly (It wasn't bolded >_>)... I was just suppose to put the answer INTO the answer variable and let C++ return it. Makes much more sense and working fine.

I'm a fucking idiot.
_________________
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
raynebc  





Joined: 16 Jun 2008
Posts: 992

PostPosted: Thu Nov 17, 2011 12:43 am    Post subject: Reply with quote

Maybe you could store the result in EAX and do a "return _EAX" in the C portion of the code? But who knows what else will happen in the behind the scenes instructions generated by the compiler.
Back to top
View user's profile Send private message
Sarg338  





Joined: 07 Feb 2008
Posts: 5143

PostPosted: Thu Nov 17, 2011 12:44 am    Post subject: Reply with quote

raynebc wrote:
Maybe you could store the result in EAX and do a "return _EAX" in the C portion of the code? But who knows what else will happen in the behind the scenes instructions generated by the compiler.


Nah, It's all good a working now. All I had to do was just take out the ret statement since he didn't want us to use Assembly to return, and I just completely missed that.

All is well and it's working perfectly.
_________________
Back to top
View user's profile Send private message XBL Gamertag: PRS Sarg
GlassDragon  





Joined: 24 May 2008
Posts: 1122

PostPosted: Sat Nov 19, 2011 5:11 am    Post subject: Reply with quote

In a mergesort, how does the merge function work? I remember working with merge and quicksort in my CS class I took last semester. However I was just coding something into excel that sorted a list of observations and assigned them signed ranks in order to be used in a Wilcoxon statistic to make my stats homework easier. When it came time to code the sorting I just used a selection sort because even an inefficient algorithm like selection can sort thousands of values in less than a second, and I was working with like... 20 values.

However I remember in a mergesort you keep dividing the arrays in half and then once you get to a base case ( a single value I think?), you start merging them... how does the merge function work? Oh and if you can code it in VBA or Java that would make it easier to understand for me because those are the languages I know.

Edit: I just realized there is a built-in sorting function on Excel, which is probably based off of quicksort anyways... now I feel stupid.
_________________
Back to top
View user's profile Send private message
blingdomepiece  





Joined: 03 Aug 2007
Posts: 4358
Location: Ottawa ON Canada

PostPosted: Sat Nov 19, 2011 4:06 pm    Post subject: Reply with quote

GlassDragon wrote:
In a mergesort, how does the merge function work?


The merge function is merging two sorted lists, so it's pretty straightforward. You iterate through both lists, looking at the first value of each, and whichever is smaller you append to the result list. Once one of the lists is empty, you append the remainder of the other list to the result list. If two values are equal you append the one from the left list, since that preserves the order of the elements.
_________________
Expert Pro Keys: 50/63 GS, most recent The Killing Moon
Expert Pro Drums: 53/83 GS, most recent Free Bird / Oh My God / Oye Mi Amor
Expert Pro Bass: 6/83 GS, most recent Everybody Wants to Rule the World
Back to top
View user's profile Wiki User Page Send private message
blackwidowcd  





Joined: 17 Nov 2007
Posts: 1717
Location: Bucketheadland

PostPosted: Sat Nov 19, 2011 10:11 pm    Post subject: Reply with quote

I'm trying to make a hangman program and am frustrated. Anyone have any idea why visual studio gives me this crap?:
Code:
error LNK2001: unresolved external symbol "char * letters" (?letters@@3PADA)


I've looked it up and no one can give a good explanation. Doesn't help that microsoft is retarded at explaining errors.

Apparently the issue is not including the correct library file or something. I seriously doubt it because I didn't have the error before and haven't changed the #includes.

The only thing I really changed before it worked was calling the binarySearch function. Commenting the implementation makes no difference.

Code:

   
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

#include "randword.h"
#include "myfuncts.h"

#define MAX 26

void drawHangman(int incorrectGuesses);
void bubbleSort(char letters[MAX], int used);
int binarySearch(char letters[MAX], int used, char letterToFind);

string playChoice = "";

char guess = '0';
char lettersGuessed[26];
char numLettersGuessed = 0;
string word = "";
string maskedWord = "";
int incorrectGuesses = 0;
bool validInput = false;
bool isPlaying = true;
int answer = 0;
int letterIndex = 0;
int strLen = 0;
char letters[];
int used = 0;

int numErrors = 0;

//ifstream inFile;
//ofstream outFile;

int main()
{
   while(isPlaying)
   {
      while(numErrors < 3)
      {
         cout << "Would you like to play Hangman? ";
         cin >> playChoice;
         answer = promptYN(playChoice);

         if(answer == PLAY)
            break;
         else if(answer == ERROR)
         {
            numErrors++;
            if(numErrors == 3)
            {
               system("pause");
               return 0;
            }
         }
         else
         {
            cout << "Goodbye" << endl << endl;
            system("pause");
            return 0;
         }

      }

      cout << "Let's PLAY\n\n";

      getWords("hangman.dat");
      word = getNextWord();
      maskedWord = word;

      for(int i = 0; i < (int)word.length(); i++)
      {
         word[i] = toupper(word[i]);
         maskedWord[i] = '_';
         letters[i] = toupper(word[i]);
      }

      cout << "Word to Guess: " << word << endl << endl;

      //inFile.close();

      cout << " -------| \n";
      cout << " |      | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "      ----- \n\n";


      cout << endl << endl;

      for(int j = 0; j < (int)maskedWord.length(); j++)
      {
         cout << maskedWord[j] << " ";
      }
      cout << endl << endl;

      while(incorrectGuesses != 5)
      {
         cout << "Enter a letter to guess: ";
         cin >> guess;
         guess = toupper(guess);

         if(guess >= 65 && guess <= 90)
         {
            letterIndex = binarySearch(lettersGuessed, used, guess);

            if(letterIndex == 1)
            {
               cout << "You already guessed " << guess << "! Enter a new letter, please." << endl;
               break;
            }
            else
            {
               lettersGuessed[numLettersGuessed] = guess;
               numLettersGuessed++;
               bubbleSort(lettersGuessed, numLettersGuessed);
               cout << "You entered: " << guess << endl << endl;

            }


            cout << "Letters Guessed: ";
            for(int j = 0; j < sizeof(lettersGuessed); j++)
               cout << lettersGuessed[j];
            cout << endl << endl;

            validInput = true;
         }
         else
         {
            cout << "Invalid input!\n\n";
            validInput = false;
         }


         if(validInput)
         {

            letterIndex = binarySearch(letters, used, guess);

            if(letterIndex == -1)
            {
               cout << guess << " is NOT in the word to guess.\n\n";
               incorrectGuesses++;

            }
            else
            {
               cout << guess << " is in the word to guess.\n\n";

               //while(letterIndex != string::npos)
               //{

               
               for(int i = 0; i < (int)maskedWord.length(); i++)
               {
                  if(word[i] == guess)
                  {
                     maskedWord[letterIndex] = guess;
                     letterIndex = word.find(guess);
                  }
               }
               

               //maskedWord.replace(letterIndex, 1, guess);

               //}


            }
            drawHangman(incorrectGuesses);

         }

      }
   }
   system("pause");
   return 0;
   
}

void drawHangman(int incorrectGuesses)
{
   switch(incorrectGuesses)
   {
   case 0:
      cout << " -------| \n";
      cout << " |      | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "      ----- \n\n";

      cout << endl << endl;
      break;
   case 1:
      cout << " -------| \n";
      cout << " |      | \n";
      cout << " O      | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "      ----- \n\n";

      cout << endl << endl;
      break;

   case 2:
      cout << " -------| \n";
      cout << " |      | \n";
      cout << " O      | \n";
      cout << " |      | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "      ----- \n\n";

      cout << endl << endl;
      break;

   case 3:
      cout << " -------| \n";
      cout << " |      | \n";
      cout << " O      | \n";
      cout << "-|-     | \n";
      cout << "        | \n";
      cout << "        | \n";
      cout << "      ----- \n\n";

      cout << endl << endl;
      break;

   case 4:
      cout << " -------| \n";
      cout << " |      | \n";
      cout << " O      | \n";
      cout << "-|-     | \n";
      cout << "/       | \n";
      cout << "        | \n";
      cout << "      ----- \n\n";

      cout << endl << endl;
      break;

   case 5:
      cout << " -------| \n";
      cout << " |      | \n";
      cout << " O      | \n";
      cout << "-|-     | \n";
      cout << "/ \\     | \n";
      cout << "        | \n";

      cout << endl << endl;
      cout << "Sorry, you lose - the word was: " << word << endl << endl;
      
   }

   if(incorrectGuesses < 5)
   {
      for(int j = 0; j < (int)maskedWord.length(); j++)
      {
         cout << maskedWord[j] << " ";
      }
      cout << endl << endl;
   }

   if(maskedWord == word)
   {
      cout << "You Win!\n\n";
      isPlaying = false;
      system("pause");
      
      
   }
}

void bubbleSort(char letters[MAX], int used)
{
   bool sorted = false;
   char tmpLetter;
   while(!sorted)
   {
      sorted = true;
      for(int i = 0; i <used> letters[i+1])
         {
            tmpLetter = letters[i];
            letters[i] = letters[i+1];
            letters[i+1] = tmpLetter;
            
            sorted = false;
         }
      }
   }
}

int binarySearch(char letters[MAX], int used, char letterToFind)
{
   int first = 0, last = used-1, mid = 0;
   bool found = false;
   char letterToCheck;

   letterToFind = toupper(letterToFind);

   while(!found && first <= last)
   {
      mid = (first+last)/2;
      letterToCheck = letters[mid];
      letterToCheck = toupper(letterToCheck);

      if(letterToFind == letterToCheck)
         found = true;
      else if(letterToFind < letterToCheck)
         first = mid+1;
      else
         last = mid-1;
   }
   if(!found)
      mid = -1;

   return(mid);
}

_________________
Back to top
View user's profile Send private message Visit poster's website XBL Gamertag: DisembodiedLoaf
raynebc  





Joined: 16 Jun 2008
Posts: 992

PostPosted: Sun Nov 20, 2011 12:54 am    Post subject: Reply with quote

My guess is the declaration of that array:
Code:
char letters[];


I'm not really familiar with C++ syntax, but since your lettersGuessed[] array defines the size, this one probably should too. Since you did define a MAX macro, make sure to use it in the array definitions (ie. lettersGuessed[MAX] and letters[MAX]).
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    ScoreHero Forum Index -> General Chat All times are GMT
Goto page Previous  1, 2, 3 ... 24, 25, 26 ... 28, 29, 30  Next
Page 25 of 30

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Copyright © 2006-2025 ScoreHero, LLC
Terms of Use | Privacy Policy


Powered by phpBB