flat assembler
Message board for the users of flat assembler.

Index > Main > reverse string - help please

Author
Thread Post new topic Reply to topic
jacko221



Joined: 12 Nov 2005
Posts: 22
jacko221
Hi everyone. Ive been practicing my programming skills by doing a programming problem but its not going as i planned. The problems was to get a string in reverse and display it. So what i have done is put the address of the string in si and bx, then increased si to the sizeof.buffer and decreased it by one so it points to the last byte of the string.
Then the idea is that you decrease si and increase bx while exchanging the bytes of at the address' to form a reverse string. But for some reason the string in the end is not being displayed reversed. Anyway i have commented the code.

Code:
format PE GUI 4.0
include '%fasminc%/win32ax.inc'
entry start

section '.data' data readable writeable
buffer db 'reverse buffer'
sizeof.buffer = $-buffer
section '.code' code readable executable
start:
lea bx,[buffer] ;put the address of the buffer into esi
mov si,bx    ;also put the address of the buffer into  ebx
add si,sizeof.buffer  ;increase esi by the size of the buffer
dec si   ;decrease it by one so its at the last byte

exchange:
cmp bx,si    ;compare the addresses
jae print    ;jump if they are equal or esi is above
xchg si,bx  ;else exchange bytes
dec si      ;decrease si
inc bx      ;increase bx

print:
invoke MessageBox,0,buffer,buffer,0
invoke ExitProcess,0
section '.idata' import data readable writeable
        library kernel32,'KERNEL32.DLL',\
                    user32,'USER32.DLL'

  include '%fasminc%\apia\kernel32.inc'
  include '%fasminc%\apia\user32.inc'
    


Help is appreciated Smile .
Note: i havnt been coding in FASM for long (4 months on and off) so the code could probably be optimized and a bit more cleaner.
Post 25 Jul 2006, 04:49
View user's profile Send private message Reply with quote
jacko221



Joined: 12 Nov 2005
Posts: 22
jacko221
one last thing... i forgot to add the jump for the loop in the label exchange.
The code should be like this

exchange:
cmp bx,si ;compare the addresses
jae print ;jump if they are equal or esi is above
xchg si,bx ;else exchange bytes
dec si ;decrease si
inc bx ;increase bx
jmp exchange
Post 25 Jul 2006, 05:22
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1607
Location: Ukraine
shoorick
1.it is strange you are using 16-bit registers in protected mode - why not ebx/esi?
2.it would be good practice to preserve such registers like ebx/esi/edi
3.also, sizeof.buffer should be 1 - size of db. assuming to it something other is not correct, i think. you have to use other word for this purpose, like sizeof_buffer.
4.at last, you have place jump to exchange after inc (e)bx to make loop

should be enough, i think...
Post 25 Jul 2006, 05:22
View user's profile Send private message Visit poster's website Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1607
Location: Ukraine
shoorick
no, not enough:
after sizeof.buffer = $-buffer
next line you have to place db 0 (string terminator)
you may not found this problem if there will be placed zeros of aligning sections, but otherwise you may got unexpected effect Wink regards!
Post 25 Jul 2006, 05:28
View user's profile Send private message Visit poster's website Reply with quote
jacko221



Joined: 12 Nov 2005
Posts: 22
jacko221
Smile thanks mate
Post 25 Jul 2006, 05:31
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 490
Location: Belarus
zhak
here's what you need, i think...
Code:
format PE GUI 4.0
include '%fasminc%/win32ax.inc'
entry start

section '.data' data readable writeable
buffer db 'reverse buffer',0
sizeof.buffer = $-buffer

section '.code' code readable executable
start:
lea ebx,[buffer]
lea esi, [ebx+sizeof.buffer-2]

exchange:
mov al, [esi]
mov dl, [ebx]
mov [esi], dl
mov [ebx], al
inc ebx
dec esi
cmp ebx, esi
jle exchange

invoke MessageBox,0,buffer,buffer,0
invoke ExitProcess,0
section '.idata' import data readable writeable
        library kernel32,'KERNEL32.DLL',\
                    user32,'USER32.DLL'

  include '%fasminc%\apia\kernel32.inc'
  include '%fasminc%\apia\user32.inc'     
Post 25 Jul 2006, 07:18
View user's profile Send private message Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 382
Location: Finland
okasvi
here is one way of doing, I know it could be done alot more nicely

Code:
format PE GUI 4.0
include 'win32a.inc'
entry $
;revstring
 push   BUFFER_LEN
 push   buffer2
 push   buffer
 call   RevString
;messagebox
 push   0
 push   buffer
 push   buffer2
 push   0
 call   [MessageBox]
;exitprocess
 push   0
 call   [ExitProcess]
;- imports
data import
library kernel32, 'KERNEL32.DLL',\
                user32, 'USER32.DLL'
import  kernel32, ExitProcess, 'ExitProcess'
import  user32, MessageBox, 'MessageBoxA'
end data
;- data  
buffer  db 'reverse buffer',0
BUFFER_LEN = $-buffer
buffer2 rb BUFFER_LEN
;-
proc RevString uses ebx esi edi, source, destination, length
        mov             esi, [source]
        mov             ebx, [length]
        mov             edi, [destination]      
        add             esi, ebx        
        xor             ecx, ecx
        .loop:
        mov             al, byte [esi]
        cmp             al, 0
        je              @f
        mov             byte [edi], al  
        inc             edi
        inc             ecx
        @@:
        dec             esi     
        cmp             ecx, ebx
        jne             .loop
        mov             byte [edi-1], 0
        mov             eax, [destination]                              
        ret
endp    
Post 25 Jul 2006, 13:19
View user's profile Send private message MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.