So my assignment was to write a program in assembly code that could make a statement, recieve a user inputted string. Print that string then reverse it using the cpu stack and print it again. this is what I have thus far.
INCLUDE Irvine32.inc
.data
buffer byte 20 DUP(0)
byteCount DWORD ?
Question byte "Please enter your name." ,0
Greeting byte "Hello " ,0
Statement byte " Here is your name backwards"
.code
main proc
mov edx , OFFSET Question
call WriteString
call CRLF
Call CRLF
mov edx, OFFSET buffer
mov Ecx, SIZEOF buffer
call ReadString
push edx
mov EDX ,OFFSET greeting
Call WriteString
pop edx
call WriteString
Call CRLF
Call CRLF
As you can see this successfully accepts a user entered input and displays it but Im really struggling trying to reverse it.
I tried these here that I copied from the book from a chapter about reversing strings.
; Push the name on the stack.
mov ecx,nameSize
mov esi,0
L1: movzx eax,aName[esi] ; get character
push eax ; push on stack
inc esi
loop L1
; Pop the name from the stack in reverse
; and store it in the aName array.
mov ecx,nameSize
mov esi,0
L2: pop eax ; get character
mov aName[esi],al ; store in string
inc esi
loop L2
Invoke ExitProcess,0
main endp
end main
but I get as output nothing.
it says "hello, (yourname here)"
it says "this is your name backwards "
ive tried just about every different incarnation of this I can think of and no avail. im at the end of my "string" here
This is against my better judgement since the snippet of code for reversing hasn't even been integrated into the code the original poster created. The variable names differ. A quick and dirty integration of the code is to create a variable nameSize that holds the number of characters read from a call to ReadString. ReadString (part of the Irvine32 library) returns the number of characters read in register EAX.
In the
.data
section add the variable:After the ReadString move contents of EAX register to nameSize. This code:
Should be:
In the code snippet for reversing code remove the lines off the bottom for the end of procedure etc. These aren't needed since we will do this in our original code.
Everywhere in the string reversal code where we see the variable aName change it to buffer since that is where we placed the user's name. Place that code into our program and use WriteString to print the reversed buffer at the end. The code could look something like:
If you get linking errors, you may not be linking in all the prerequisite libraries. Try adding these lines to the top of your program:
I should point out that this is a very inefficient way to reverse a string if you don't mind destroying the original. It can be done without a secondary buffer on the stack by reversing the string in place.
At a high level:
Doing this without allocating a new buffer is possible as well, but should be avoided in general because of the cost of invoking a system call (which you would need to do after each character)