hi, i've a question to string commands like LODS, STOS, etc. i've found out that i should first load the string into SI register before using LODS. but shouldn't i use esi when i'm programming in 32 bit? What for example is DI, is it EDI in 32 bit applications? tnx for help.
Posted on 2001-02-04 05:34:00 by [SaFc0n]
Because you are using the flat memory model, pointers should be 32-bit so you must use esi & edi, not si and di. LODS al/ax/eax loads a byte/word/dword from the memory location pointed to by esi and increments or decrements (depending on the direction flag) esi with the size of the operand. Thomas
Posted on 2001-02-04 06:13:00 by Thomas
Safcon, Thomas is right, in 32 bit, if you want to use the old string instructions, use the 32 bit source and destination indexes, ESI & EDI. You will also normally use ECX as the counter for the string instructions. You can usually improve on the string instructions when moving data around by using a different style of code, mov esi, src ; source address mov edi, dst ; destination address mov ecx, ln ; byte count add ecx, esi ; set exit condition lbl: mov al, inc esi ; modify byte in AL here if needed mov , al inc edi cmp esi, ecx jne lbl This type of code is faster than using lodsb / stosb Regards, hutch@pbq.com.au
Posted on 2001-02-04 06:57:00 by hutch--
Isn't that back jump taking too much time hutch? i suggest makin it unconditional jump and making the test at the top of the loop... so first test will fail allmost all of the time making for a faster jump and the last jump will be unconditional and also faster ... Am i wrong or right?
Posted on 2001-02-04 08:04:00 by BogdanOntanu
ok, tnx. you know, i'm going to write a little tutorial about string commands. i would appreciate it if someone could take a look at it when it is finished. i'll contact you then.
Posted on 2001-02-04 12:33:00 by [SaFc0n]
I would definently appreciate any type of string tutorial for ASM since this is where i usually get lost with it.
Posted on 2001-02-04 14:34:00 by Devin
Of the string instructions, MOVS, and possibly CMPS, are the only ones that are optimized by Intel. They are also the most used ones. If you can process four bytes at a time, that is a major optimization. As for the jumps, putting a JMP at the bottom and a Jcc at the top means each iteration requires executing two jumps, not one. Doesn't branch prediction make the difference between branching and nonbranching moot? Watching cache behavior is important, too.
Posted on 2001-02-04 15:22:00 by tank
Bogdan, From all the testing I have done and the benchmarks I have seen, a conditional jump runs at about the same speed as an unconditional jump. Two jumps are slower than one and the algo would be slower if you had a seperate test and an unconditional jump at the end. Generally jump reduction in a small loop will show improvements in performance. Tank is right about MOVSD, it is about the only string instruction that is still fast enough but it is only fast when used with REP MOVSD. Regards, hutch@pbq.com.au This message was edited by hutch--, on 2/5/2001 2:05:13 AM
Posted on 2001-02-05 02:03:00 by hutch--
ok, the string commands tutorial is finished. you can download it from my site This message was edited by , on 2/5/2001 12:41:10 PM
Posted on 2001-02-05 12:38:00 by [SaFc0n]
Below is a reasonably simple string procedure that filters characters in a zero terminated string so that only numbers are written to the destination buffer. source is the bufer to be filtered and dest is the destination buffer which must be big enough to receive the result, this is usually the same size buffer as the source for safety reasons.

; ########################################################################

NumOnly proc source :DWORD, dest:DWORD

    mov ecx, source     ; put address into ecx
    mov edx, dest       ; put address into edx

  @@:
    mov al,        ; copy byte at address in ecx to al
    inc ecx             ; increment address in ecx
  ; ------------------------------------------------------
  ; perform byte modification or omissions here
  ; ------------------------------------------------------
    cmp al, 0           ; test for zero first
    je  @F              ; exit loop on ascii zero
    cmp al, "0"
    jb @B               ; if below 48 ("0"), jump back
    cmp al, "9"
    ja @B               ; if above 57 ("9"), jump back
  ; ------------------------------------------------------
    mov , al       ; copy byte in al to address in edx
    inc edx             ; increment address in edx
    jmp @B

  @@:
    mov , al       ; copy ascii zero to address in EDI

    ret

NumOnly endp

; ########################################################################

Regards, hutch@pbq.com.au
Posted on 2001-02-07 16:23:00 by hutch--