944,098 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Marked Solved
  • Views: 5504
  • Assembly RSS
May 3rd, 2008
0

Copy a String Backwards

Expand Post »
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:

Assembly Syntax (Toggle Plain Text)
  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...


Assembly Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Spagett912 is offline Offline
31 posts
since Apr 2008
May 3rd, 2008
0

Re: Copy a String Backwards

asm Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kermitaner is offline Offline
11 posts
since May 2008
May 3rd, 2008
0

Re: Copy a String Backwards

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Spagett912 is offline Offline
31 posts
since Apr 2008
May 3rd, 2008
0

Re: Copy a String Backwards

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)
Assembly Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kermitaner is offline Offline
11 posts
since May 2008
May 3rd, 2008
0

Re: Copy a String Backwards

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Spagett912 is offline Offline
31 posts
since Apr 2008
May 4th, 2008
0

Re: Copy a String Backwards

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
asm Syntax (Toggle Plain Text)
  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
Assembly Syntax (Toggle Plain Text)
  1. invoke StdOut,addr target
if u have included the required api files..
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kermitaner is offline Offline
11 posts
since May 2008
Oct 13th, 2009
0
Re: Copy a String Backwards
I had this same assignment. Here's how I solved it using indexed addressing:
Assembly Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vorell is offline Offline
1 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Assembly inside a C++ program
Next Thread in Assembly Forum Timeline: total rookie,pls help me





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


Follow us on Twitter


© 2011 DaniWeb® LLC