I Need Assembly help w/reversing strings

Reply

Join Date: Oct 2005
Posts: 4
Reputation: frantonio is an unknown quantity at this point 
Solved Threads: 0
frantonio's Avatar
frantonio frantonio is offline Offline
Newbie Poster

I Need Assembly help w/reversing strings

 
0
  #1
Oct 31st, 2006
I am trying to write a program in Assembly (IA-32 architecture), that reverses the string input from the user.
I've tried to use push and pop, but I can't seem to get any output.
Here is the code. Any Suggestions?

INCLUDE Irvine32.inc

.data

str1 BYTE "Enter a string of characters: ", 0
str2 BYTE "Entered: ", 0
str3 BYTE "Reversed: ", 0

userInput BYTE 80 DUP(0)
userReversed BYTE 80 DUP(0)
dwordVval DWORD ?
strSize = ($ - userInput) - 1

.code
;******** GET INPUT FROM THE USER
main PROC

mov edx, OFFSET str1
call WriteString ; "Enter a string of characters: "

mov edx, OFFSET userInput ; StringEntered
mov ecx, sizeof userInput - 1 ; load ecx with length -1
call ReadString ; get input from the user

;********** PUSH AND POP ROUTINE
mov ecx, strSize
mov esi,0

L1:

movzx eax, userInput[esi] ; get character
push eax ; push on stack
inc esi
Loop L1
mov ecx,strSize ; Pop the name from the stack, in reverse,
mov esi,0 ; and store in the StringReversed array.

L2:

pop eax ; get character
mov userReversed[esi],al ; store in string
inc esi
Loop L2

;******************************* DISPLAY ROUTINE
mov edx, OFFSET str2 ; "Entered: "
call WriteString

mov edx, OFFSET userInput ; User Input
call WriteString
call CrLf

mov edx, OFFSET str3 ; "Reversed: "
call WriteString
mov edx,OFFSET userReversed ; Display the reversed string.
call WriteString
call Crlf

exit
main ENDP
END main
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: I Need Assembly help w/reversing strings

 
0
  #2
Nov 1st, 2006
Here is a solution using inline assembly in c program
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(int argc, char* argv[])
  5. {
  6. char s1[] = "Hello World";
  7. char s2[255] = {0};
  8. int len = 0;
  9. _asm
  10. {
  11. push esi
  12. push edi
  13. lea esi,s1
  14. lea edi,s2
  15. ; get length of string s1
  16. xor eax,eax
  17. mov ecx,0ffh ; max length of string ?
  18. repnz cmpsb ;locate 0 byte, if it exists
  19. jnz l1 ; oops! not a null terminated string
  20. not ecx ; make it an integer
  21. and ecx,0000000ffh ; mask off high dword
  22. lea edi,[len] ; save for output later
  23. mov [edi],cx ;
  24.  
  25. ;
  26. ;
  27. ; reverse the string
  28. lea edi,s2 ; set up registers for moving string
  29. lea esi,s1
  30. add esi,ecx
  31. dec esi
  32. dec esi
  33. xor eax,eax
  34. l2: ; move each byte from ds:esi to es:edi in reverse order
  35. ; would be nice if we could use rep movsb, but we can't
  36. ; so have to do it the lard way.
  37. mov al,[esi]
  38. mov [edi],al
  39. inc edi
  40. dec esi
  41. loopnz l2
  42. mov [edi],0
  43.  
  44.  
  45.  
  46. l1: nop ; not found
  47. pop edi
  48. pop esi
  49.  
  50.  
  51. }
  52. cout << len << endl;
  53. cout << s2 << endl;
  54. return 0;
  55. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 4
Reputation: frantonio is an unknown quantity at this point 
Solved Threads: 0
frantonio's Avatar
frantonio frantonio is offline Offline
Newbie Poster

Re: I Need Assembly help w/reversing strings

 
0
  #3
Nov 3rd, 2006
Does anyone have a solution to the problem with the above program using the assembly language from Kip Irvine's Book?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: I Need Assembly help w/reversing strings

 
0
  #4
Nov 3rd, 2006
Originally Posted by frantonio View Post
Does anyone have a solution to the problem with the above program using the assembly language from Kip Irvine's Book?
Nobody is going to just hand you the code. Use your head for something other than a hat rack and apply the code I posted to your problem. It can't be all that hard to do assuming they are both written with the same assembly language.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 4
Reputation: frantonio is an unknown quantity at this point 
Solved Threads: 0
frantonio's Avatar
frantonio frantonio is offline Offline
Newbie Poster

Re: I Need Assembly help w/reversing strings

 
0
  #5
Nov 4th, 2006
Ancient Dragon,
I never asked anyone to hand code to me. I had already written the program and needed suggestions on what could be causing the program not to display output.

Many textbooks give snippets as examples without writing the program for someone. Thank you for your code and your comment about using my head for something other than a coat rack.

I was successful at creating a working program with only 20-lines of code.

P.S My original question was concerning a program that prompts the user for input, not a string constant. I already had a working program that reverses a hard coded string. Thank You for your help.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 24
Reputation: Ryu is an unknown quantity at this point 
Solved Threads: 0
Ryu Ryu is offline Offline
Newbie Poster

Re: I Need Assembly help w/reversing strings

 
0
  #6
Nov 4th, 2006
I think this is because you also copy null or ambigious bytes trailing the actual input string into the reversed one. For instance "abc" may be last 3 bytes of 80 byte reverse buffer as the first 77 is ambigous (most likely null's).

Using push/pop is more complicated then it needs to be in my opinion. The solution is you need the actual length of the input string, I'd do a simple string length scan with a repne scasb, once null is reached (E)DI-1 can be the starting point to copy backwards into the reversing buffer, as you already have the count.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: I Need Assembly help w/reversing strings

 
0
  #7
Nov 4th, 2006
Originally Posted by frantonio View Post
Does anyone have a solution to the problem with the above program using the assembly language from Kip Irvine's Book?
Originally Posted by frantonio View Post
Ancient Dragon,
I never asked anyone to hand code to me. .
Hummmm. But I'm glad you found the solution on your own. Now, would you mind posting it so we can all learn from it ?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 4
Reputation: frantonio is an unknown quantity at this point 
Solved Threads: 0
frantonio's Avatar
frantonio frantonio is offline Offline
Newbie Poster

Re: I Need Assembly help w/reversing strings

 
0
  #8
Nov 5th, 2006
Quoted by frantonio:
Does anyone have a solution to the problem with the above program using the assembly language from Kip Irvine's Book?

Quoted by frantonio:
Ancient Dragon,
I never asked anyone to hand code to me. .

I know that it is mid-term election time, but I thought that taking out a few words from what one says and twisting it, belongs to the political arena. This reminds me of a bad political commercial.

The second quote above was taken out of context and the central meaning was distorted. The full context of the sentence is:

I never asked anyone to hand code to me. I had already written the program and needed suggestions on what could be causing the program not to display output.

It is not uncommon for one to seek advice when one is unsure of what could be causing a problem.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: I Need Assembly help w/reversing strings

 
0
  #9
Nov 5th, 2006
This is really a common problem of not asking the right question(s). I frequently see people ask "Do you know how to .... ", to which my answer is either "Yes" or "No". Afterall if someone is going to ask a yes-or-no question that is how I will answer. In your case you asked us if we would give you code to do something. That was the wrong question. You should have asked how to improve your code to make it do whatever you want it to do.

>>My original question was concerning a program that prompts the user for input
The algorithm for reversing a string is the same whether the string is from user input or a hard-coded string. Again, you may have asked the wrong question in your original post. Do you want to know how to get a string from the keyboard? Or how to reverse the string. ? Now I am not sure of your intent.

>My original question was concerning a program that prompts the user for input
you are quite right and everyone is encouraged to ask lots of questions. But you also need to ask the right question(s).


Anyway -- good luck with your program.
Last edited by Ancient Dragon; Nov 5th, 2006 at 11:35 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Assembly Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC