Copy a String Backwards

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

Copy a String Backwards

 
0
  #1
May 3rd, 2008
I had been writing a program that enforces the loop instruction with indirect addressing but I need it to copy a string from source to target, reversing the character order in the process. I need to use the variables:

source BYTE "This is the source string",0
target BYTE SIZEOF source DUP('#')

After that I need to insert the following statements right after the loop to display the hexadecimal contents of the target string:

  1. mov esi, OFFSET target : offset of variable
  2. mov ebx, 1 : byte format
  3. mov exc, SIZEOF target : counter
  4. call DumpMem


so far this is all I have and what I've been following by to help me and I'm pretty sure there is some unnecessary garbage in there...


  1. INCLUDE Irvine32.inc
  2. .data
  3. val1 WORD 1000h
  4. val2 WORD 2000h
  5.  
  6. arrayB BYTE 10h,20h,30h,40h,50h
  7. arrayW WORD 100h,200h,300h
  8. arrayD DWORD 10000h,20000h
  9.  
  10. .code
  11. main PROC
  12.  
  13.  
  14. ; in-Direct Addressing (byte array):
  15. mov eax,offset arrayB
  16. mov ecx, LENGTHOF arrayB ; This would give us "5"
  17. mov bl,0
  18.  
  19. L1:add bl, [eax]
  20. inc eax
  21. loop L1
  22.  
  23. ; Direct-Offset Addressing (word array):
  24. ; mov ax,arrayW ; AX = 100h
  25. ; mov ax,[arrayW+2] ; AX = 200h
  26.  
  27. ; Direct-Offset Addressing (doubleword array):
  28. ; mov eax,arrayD ; EAX = 10000h
  29. ; mov eax,[arrayD+4] ; EAX = 20000h
  30. ; mov eax,[arrayD+TYPE arrayD] ; EAX = 20000h
  31.  
  32. exit
  33. main ENDP
  34. END main


So if someone knows any helpful tips or something that'd be great cuz i'm pretty stuck with this.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 11
Reputation: kermitaner is an unknown quantity at this point 
Solved Threads: 1
kermitaner kermitaner is offline Offline
Newbie Poster

Re: Copy a String Backwards

 
0
  #2
May 3rd, 2008
  1. include /masm32/include/masm32rt.inc
  2. .data
  3. source db "this is the source",0
  4. .data
  5. target db 255 dup(?)
  6. .code
  7. start:
  8. mov ecx,sizeof source-1
  9. mov edi,offset target
  10. @@:
  11. mov al, byte ptr [source+ecx-1]
  12. stosb
  13. loop @b
  14. mov byte ptr[edi],0
  15. print addr target,13,10
  16. exit
  17. end start
code for masm32, think u get the idea...

greetz,
kermit
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

Re: Copy a String Backwards

 
0
  #3
May 3rd, 2008
Hey thanks a lot for helping me so quick. Unfortunately, i'm not that great at Assembly Language so I don't exactly follow where you are going with this coding. I tried to run it but I got an error on the line where its supposed to print out the output.
Last edited by Spagett912; May 3rd, 2008 at 8:39 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 11
Reputation: kermitaner is an unknown quantity at this point 
Solved Threads: 1
kermitaner kermitaner is offline Offline
Newbie Poster

Re: Copy a String Backwards

 
0
  #4
May 3rd, 2008
hm, do u get an compile error or does it crash when u run the exe ?
remember to compile with masm console assemble& link: (.bat file)
  1. @echo off
  2. if exist %1.obj del %1.obj
  3. if exist %1.exe del %1.exe
  4. \masm32\bin\ml /coff /c %1.asm
  5. if errorlevel 1 goto errasm
  6. \masm32\bin\Link /SUBSYSTEM:console %1.obj
  7. dir %1.*
  8. :errasm
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

Re: Copy a String Backwards

 
0
  #5
May 3rd, 2008
i get the error

Error 1 error A2008: syntax error : print c:\Irvine\examples\ch04\Project_sample\main.asm

and the error
Error 2 error PRJ0019: A tool returned an error code from "Assembling..." Project

when it's compiling. Sorry again but i'm a bit nooby at this so i don't understand the jargain you used above. But thanks again for getting back so soon. It's much appreciated.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 11
Reputation: kermitaner is an unknown quantity at this point 
Solved Threads: 1
kermitaner kermitaner is offline Offline
Newbie Poster

Re: Copy a String Backwards

 
0
  #6
May 4th, 2008
my code works with standard masm32 installation, i'don't use the irvine.inc file, instead
include /masm32/include/masm32rt.inc contains all the needed "macros/apis/include" commands.
u can just skip the print macro and replace it with the code u whished to use
  1. mov esi, OFFSET target : offset of variable
  2. mov ebx, 1 : byte format
  3. mov exc, SIZEOF target : counter
  4. call DumpMem
( try: mov ecx,sizeof source :-)
- or use the standard api command
  1. invoke StdOut,addr target
if u have included the required api files..
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: vorell is an unknown quantity at this point 
Solved Threads: 0
vorell vorell is offline Offline
Newbie Poster
 
0
  #7
Oct 13th, 2009
I had this same assignment. Here's how I solved it using indexed addressing:
  1. INCLUDE Irvine32.inc
  2.  
  3. .data
  4. source BYTE "This is the source string",0
  5. target BYTE SIZEOF source DUP('#')
  6.  
  7. .code
  8. reversecopy PROC
  9.  
  10. mov ESI,0 ; source register
  11. mov EDI,SIZEOF source-2D ; destination register
  12. mov ECX,SIZEOF source ; loop counter
  13.  
  14. Copy:
  15. mov DL,source[ESI] ; get a character from source
  16. mov target[EDI],DL ; store it in the target
  17. inc ESI ; move to next character - increment the source index
  18. dec EDI ; decrement the destination index
  19. loop Copy ; repeat for entire string
  20.  
  21. ;
  22. ; Outputresults
  23. ;
  24. mov ESI,OFFSET target ;offset of variable
  25. mov EBX,1 ;byte format
  26. mov ECX,SIZEOF target ;counter
  27. call DumpMem
  28.  
  29. ;
  30. ; Our results should be: 67 6E 69 72 74 73 20 65 63 72 75 6F 73 20 65 68
  31. ; 74 20 73 69 20 73 69 68 54
  32. ;
  33. ;Return to operating system
  34. ;
  35. ; exit
  36.  
  37. reversecopy ENDP
  38. END reversecopy
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC