Board index » delphi » Help with assembler in Delphi 2

Help with assembler in Delphi 2

Hi

I'm busy writing a program that will eventually have to read millions of
lines of data (literally!) and convert the ASCII data into usable things
for printing out again, so I'm trying to optimise it heavily. One thing
that was holding things up was pos, which was mostly used for single
char checks so I wrote a single char version in Delphi with a for loop,
which helped a lot. However, I'd like to be able to write an assembler
version, which would look something like this in a 16-bit app: (skip
down to find the question, this is just to show you want I want)

asm
mov res, 0
les di, str     {the string}
mov bx, di      {save the start offset}
cld
mov cl, es:[di] {get the string length}
xor ch, ch
cld
mov al, ch      {the character being searched for}
repne scasb
jne @end
sub bx, di      {calculate distance scanned}
mov res, bx
end;
result := res;

However, I don't know much about the protected mode architecture other
than that memory addressing is totally different and so on. The question
is:
a) How do I load the address of the string
b) Given that, how does the code above need to be changed (scasb used
edi or something, doesn't it?)

Thanks in advance
Bruce
--
Please remove the ".nospam" from my address before replying
/--------------------------------------------------------------------\
| Bruce Merry (the Almighy Cheese) | bmerry at iafrica dot com       |
| Proud user of Linux!             | http://www.cs.uct.ac.za/~bmerry |
|      Heavy, adj.: Seduced by the chocolate side of the force.      |
\--------------------------------------------------------------------/

 

Re:Help with assembler in Delphi 2


Bruce Merry (the Almighty Cheese) wrote:

Quote
> Hi

> I'm busy writing a program that will eventually have to read millions of
> lines of data (literally!) and convert the ASCII data into usable things
> for printing out again, so I'm trying to optimise it heavily. One thing
> that was holding things up was pos, which was mostly used for single
> char checks so I wrote a single char version in Delphi with a for loop,
> which helped a lot. However, I'd like to be able to write an assembler
> version, which would look something like this in a 16-bit app: (skip
> down to find the question, this is just to show you want I want)

The first thing you need to decide is what is your target processor because
the optimal solution varies:  Assuming pentium target I lifted this out of
"Pentium Processor optimization tools" by Michael L. Schmidt.

lbl:                            ; cycles
        mov     ax, [di]        ;  1    read two char's
        add     di, 4           ;  0    advance ptr
        cmp     al, dl          ;  1    compare 1st with scan char
        je      exit1           ;  0    exit if a match
        mov     bx, [di-2]      ;  1    read two more char's
        cmp     ah, dl          ;  0    compare 2nd with scan char
        je      exit2           ;  1    exit if a match
        cmp     bl, dl          ;  1    compare 3rd with scan char
        je      exit3           ;  0    exit if a match
        cmp     bh, dl          ;  1    compare 4th with scan char
        je      exit4           ;  0    exit if a match
        dec     cx              ;  1
        jnz     lbl             ;  0
                                ;  7 cycles, 1.75 cycles per byte

exit1:
exit2:
exit3:
exit4:
        ret

repne scasb is 4 cycles/byte for the pentium.

It may be possible to make this yet better but I've not played with it.  If
you want to get into pentium optimization this book isn't bad, but you can
download a really good primer on the subject from something like:
http://announce.com/agner/assem/.

Enjoy,

Bob Lee

Other Threads