| | |
Copy a String Backwards
Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
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...
So if someone knows any helpful tips or something that'd be great cuz i'm pretty stuck with this.
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)
mov esi, OFFSET target : offset of variable mov ebx, 1 : byte format mov exc, SIZEOF target : counter 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)
INCLUDE Irvine32.inc .data val1 WORD 1000h val2 WORD 2000h arrayB BYTE 10h,20h,30h,40h,50h arrayW WORD 100h,200h,300h arrayD DWORD 10000h,20000h .code main PROC ; in-Direct Addressing (byte array): mov eax,offset arrayB mov ecx, LENGTHOF arrayB ; This would give us "5" mov bl,0 L1:add bl, [eax] inc eax loop L1 ; Direct-Offset Addressing (word array): ; mov ax,arrayW ; AX = 100h ; mov ax,[arrayW+2] ; AX = 200h ; Direct-Offset Addressing (doubleword array): ; mov eax,arrayD ; EAX = 10000h ; mov eax,[arrayD+4] ; EAX = 20000h ; mov eax,[arrayD+TYPE arrayD] ; EAX = 20000h exit main ENDP END main
So if someone knows any helpful tips or something that'd be great cuz i'm pretty stuck with this.
•
•
Join Date: May 2008
Posts: 11
Reputation:
Solved Threads: 1
asm Syntax (Toggle Plain Text)
include /masm32/include/masm32rt.inc .data source db "this is the source",0 .data target db 255 dup(?) .code start: mov ecx,sizeof source-1 mov edi,offset target @@: mov al, byte ptr [source+ecx-1] stosb loop @b mov byte ptr[edi],0 print addr target,13,10 exit end start
greetz,
kermit
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.
•
•
Join Date: May 2008
Posts: 11
Reputation:
Solved Threads: 1
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)
remember to compile with masm console assemble& link: (.bat file)
Assembly Syntax (Toggle Plain Text)
@echo off if exist %1.obj del %1.obj if exist %1.exe del %1.exe \masm32\bin\ml /coff /c %1.asm if errorlevel 1 goto errasm \masm32\bin\Link /SUBSYSTEM:console %1.obj dir %1.* :errasm
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.
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.
•
•
Join Date: May 2008
Posts: 11
Reputation:
Solved Threads: 1
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 ( try: mov ecx,sizeof source :-)
- or use the standard api command if u have included the required api files..
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)
mov esi, OFFSET target : offset of variable mov ebx, 1 : byte format mov exc, SIZEOF target : counter call DumpMem
- or use the standard api command
Assembly Syntax (Toggle Plain Text)
invoke StdOut,addr target
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#7 Oct 13th, 2009
I had this same assignment. Here's how I solved it using indexed addressing:
Assembly Syntax (Toggle Plain Text)
INCLUDE Irvine32.inc .data source BYTE "This is the source string",0 target BYTE SIZEOF source DUP('#') .code reversecopy PROC mov ESI,0 ; source register mov EDI,SIZEOF source-2D ; destination register mov ECX,SIZEOF source ; loop counter Copy: mov DL,source[ESI] ; get a character from source mov target[EDI],DL ; store it in the target inc ESI ; move to next character - increment the source index dec EDI ; decrement the destination index loop Copy ; repeat for entire string ; ; Outputresults ; mov ESI,OFFSET target ;offset of variable mov EBX,1 ;byte format mov ECX,SIZEOF target ;counter call DumpMem ; ; Our results should be: 67 6E 69 72 74 73 20 65 63 72 75 6F 73 20 65 68 ; 74 20 73 69 20 73 69 68 54 ; ;Return to operating system ; ; exit reversecopy ENDP END reversecopy
![]() |
Similar Threads
- Open In New Window Php (PHP)
- Reversing a string (C)
- Setting an array and reading in a txt file backwards (C++)
- error C2375: 'my_strdup' : redefinition; different linkage (C++)
- I Need Assembly help w/reversing strings (Assembly)
- Question about reversal and array storage for Palindrome (C++)
- Source Code that don't work? (Java)
Other Threads in the Assembly Forum
- Previous Thread: Assembly inside a C++ program
- Next Thread: total rookie,pls help me
| Thread Tools | Search this Thread |





