flat assembler
Message board for the users of flat assembler.
Index
> Main > MASM to FASM reverse string Goto page Previous 1, 2 |
Author |
|
edfed 08 Jul 2011, 14:37
simple, but bloat.
push a dword to save a byte is not optimal. i find it better to use two pointers to do the job. |
|||
08 Jul 2011, 14:37 |
|
AsmGuru62 08 Jul 2011, 15:09
It requires a buffer - better to do it in place.
Buffers are not easy to come up with. |
|||
08 Jul 2011, 15:09 |
|
r22 08 Jul 2011, 17:00
All of the answers on this thread are lacking over-the-top, unnecessary optimization. Since no one has presented a multithreaded solution I was forced to type the following.
Code: .data _RevStrOff dd -1 ;; current working offset _RevStrStart dd 0 ;; constant start pointer _RevStrEnd dd 0 ;; constant end pointer _RevStrSync dd 0 ;; synchronization semaphore _RevStrThrCnt dd 0 ;; number of threads in RevStr's worker thread pool .code ;; Create a bunch (optimal amount) of threads ;; with RevStrThread as the Thread Proc ... ;; This function is not thread safe LOL RevStr: MOV ecx,[esp+8] ;; len MOV eax,[esp+4] ;; ptr CMP ecx, 1 ;; useless length JLE .die CMP eax, -1 ;; useless string JE .die MOV edx, [_RevStrThrCnt] .sync: CMP [_RevStrSync], edx JNE .sync LEA edx, [eax + ecx - 1] ;;end MOV [_RevStrEnd], edx SHR ecx, 1 ;; len / 2 DEC ecx ;; (len / 2) -1 MOV [_RevStrStart], eax MOV [_RevStrOff], ecx .wait: ;;wait for the thread pool to finish CMP [_RevStrOff], 0 ;; wait while offset >= 0 JGE .wait .die: RET 8 RevStrThread: .reset: ;; reset and go into wait mode MOV [_RevStrOff], -1 LOCK ADD [_RevStrSync], 1 JMP .wait .work: LOCK SUB [_RevStrSync], 1 MOV esi, [_RevStrStart] ;; start ptr MOV edi, [_RevStrEnd] ;; end ptr .again: MOV ebx, -1 LOCK XADD [_RevStrOff], ebx ;; ebx = current offset CMP ebx, ebx ;; reset if offset < 0 JS .reset LEA ecx, [ebx*-1] ;; -offset MOV al, byte[esi + ebx] MOV ah, byte[edi + ecx] MOV byte[esi + ebx], ah MOV byte[edi + ecx], al JMP .again .wait: ;; wait for a new string CMP [_RevStrOff], 0 ;; wait while offset < 0 JL .wait JMP .work * edit * I'm kind of curious if this actually works now... Last edited by r22 on 08 Jul 2011, 19:40; edited 1 time in total |
|||
08 Jul 2011, 17:00 |
|
MHajduk 08 Jul 2011, 19:22
edfed wrote: push a dword to save a byte is not optimal. EDIT: after a while I got it. You were talking about bitRAKE's method quoted in the Picnic's post. Explicit quoting is a good manner - nobody is forced to guess somebody's thoughts then. Last edited by MHajduk on 08 Jul 2011, 20:02; edited 1 time in total |
|||
08 Jul 2011, 19:22 |
|
MHajduk 08 Jul 2011, 19:24
AsmGuru62 wrote: It requires a buffer - better to do it in place. |
|||
08 Jul 2011, 19:24 |
|
jochenvnltn 23 Jan 2013, 21:50
Code: proc StrReverse lpString:DWORD mov eax,[lpString] ; put string address in EAX mov edx,eax ; same in EDX @@: ; move EDX to the end of string add edx,1 cmp byte [edx],0 jne @B sub edx,1 ; return EDX to last not null character @@: mov cl,byte [eax] ; swap EAX and EDX, moving from two brinks mov ch,byte [edx] ; in opposite directions mov [eax],ch ; while EAX is less than EDX mov [edx],cl add eax,1 sub edx,1 cmp eax,edx jl @B ret endp |
|||
23 Jan 2013, 21:50 |
|
Goto page Previous 1, 2 < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.