Jump to content

Simple Reverse String Algorithm

- - - - -

  • Please log in to reply
3 replies to this topic

#1
assembler_wanna_be

assembler_wanna_be

    Newbie

  • Members
  • Pip
  • 3 posts
There are many examples for reversing a string in assembler on the net but I couldn't find any which only one string is used. So I decided to write myself one. And below is what I've created. The only problem is that it doesn't work. It reverses a part of the string a adds some characters at its end. Why is that? I've tried turbo debugging it but I cannot understand where actually string1 is visualised. Thank you in advance!

.model small

.data

	string1	db	10	dup(' '), '$'

	endl	db	0dh, 0ah, '$'

	

.code

main proc

	mov ax, @data

	mov ds, ax

	

	lea si, string1

	

	mov ah, 1

READ_NEXT:

	int 21h

	cmp al, 13

	je NEXT

	mov [si], al

	inc si

	jmp READ_NEXT

	

NEXT:

	mov al, '$'

	mov [si], al

	dec si

	lea dx, endl

	mov ah, 9

	int 21h

	

	lea di, string1

BEG:

	mov bx, [di]

	mov bp, [si]

	mov [di], bp

	mov [si], bx

	dec si

	inc di

	cmp di, si

	jl BEG

	

	lea dx, string1

	int 21h

	lea dx, endl

	int 21h

	

	mov ah, 4ch

	int 21h

	

main endp

end main


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,251 posts
  • Location:C:\Countries\US
At the area where it is:

BEG:

	mov bx, [di]

	mov bp, [si]

	mov [di], bp

	mov [si], bx

	dec si

	inc di

	cmp di, si

	jl BEG

would it work if you use 'al' and 'ah', instead of 'bx' and 'bp'?

Edited by dargueta, 14 December 2010 - 10:10 PM.


#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Ooh, dude...don't use BP like that. You could cause some serious stack issues if you pass arguments using the stack, especially with external functions. RR is right, though.
sudo rm -rf /

#4
assembler_wanna_be

assembler_wanna_be

    Newbie

  • Members
  • Pip
  • 3 posts
Thanks for the quick response!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users