944,155 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 7866
  • Assembly RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 31st, 2006
0

I Need Assembly help w/reversing strings

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frantonio is offline Offline
4 posts
since Oct 2005
Nov 1st, 2006
0

Re: I Need Assembly help w/reversing strings

Here is a solution using inline assembly in c program
Assembly Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 3rd, 2006
0

Re: I Need Assembly help w/reversing strings

Does anyone have a solution to the problem with the above program using the assembly language from Kip Irvine's Book?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frantonio is offline Offline
4 posts
since Oct 2005
Nov 3rd, 2006
0

Re: I Need Assembly help w/reversing strings

Click to Expand / Collapse  Quote originally posted by frantonio ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 4th, 2006
0

Re: I Need Assembly help w/reversing strings

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frantonio is offline Offline
4 posts
since Oct 2005
Nov 4th, 2006
0

Re: I Need Assembly help w/reversing strings

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.
Ryu
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Ryu is offline Offline
24 posts
since Jul 2006
Nov 4th, 2006
0

Re: I Need Assembly help w/reversing strings

Click to Expand / Collapse  Quote originally posted by frantonio ...
Does anyone have a solution to the problem with the above program using the assembly language from Kip Irvine's Book?
Click to Expand / Collapse  Quote originally posted by frantonio ...
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 ?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 5th, 2006
0

Re: I Need Assembly help w/reversing strings

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frantonio is offline Offline
4 posts
since Oct 2005
Nov 5th, 2006
0

Re: I Need Assembly help w/reversing strings

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Jun 23rd, 2010
0
Re: I Need Assembly help w/reversing strings
How To Find Length Of String In Assembly Language Source ...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aynaz is offline Offline
2 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: Atmega 168 ADC
Next Thread in Assembly Forum Timeline: SPIM Syntax Issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC