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

Here is a solution using inline assembly in c program

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
	char s1[] = "Hello World";
	char s2[255] = {0};
	int len = 0;
	_asm
	{
		push	esi
		push	edi
		lea		esi,s1
		lea		edi,s2
		; get length of string s1
		xor		eax,eax	
		mov		ecx,0ffh ; max length of string ?
		repnz	cmpsb	  ;locate 0 byte, if it exists
		jnz		l1		; oops! not a null terminated string
		not		ecx		; make it an integer
		and		ecx,0000000ffh ; mask off high dword
		lea		edi,[len]	; save for output later
		mov		[edi],cx	; 

		;
		;
		; reverse the string
		lea		edi,s2		; set up registers for moving string
		lea		esi,s1
		add		esi,ecx
		dec		esi
		dec		esi
		xor		eax,eax
l2:		; move each byte from ds:esi to es:edi in reverse order
		; would be nice if we could use rep movsb, but we can't
		; so have to do it the lard way.
		mov		al,[esi]
		mov		[edi],al
		inc		edi
		dec		esi
		loopnz	l2
		mov		[edi],0



	l1: nop ; not found
		pop		edi
		pop		esi


	}
	cout << len << endl;
	cout << s2 << endl;
	return 0;
}

Does anyone have a solution to the problem with the above program using the assembly language from Kip Irvine's Book?

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.

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.

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.

Does anyone have a solution to the problem with the above program using the assembly language from Kip Irvine's Book?

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 ?

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. :)

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.

How To Find Length Of String In Assembly Language Source ...

I don't know if has any function for this, but you can do a loop with jmp until you find a '$' and increases CX for each loop. The result is in CX.

mov cx,00h
  push si
  xor si,si
getlength:
  cmp text[si],'$'
  je any_end
  inc cx
  inc si
  jmp getlength

or only uses SI... :)

Edited 6 Years Ago by makako nido: n/a

AYNAZ - You need to post your own problem as a new posting!

Depends on the string! If an ASCIIZ (NULL terminated ASCII string) merely check each character until you find the NULL character. You can count with each loop, or once the NULL is found, merely subtract the new pointer from the starting pointer. The difference is the length!

If the terminator is a '$' then the same logic applies.

Edited 6 Years Ago by wildgoose: n/a

This article has been dead for over six months. Start a new discussion instead.