* Suggested Change to Alpha Books (Correction)
@ 1997-07-15 4:00 Edward Lee
1997-07-16 4:00 ` Stephen Hoff Hoffman
1997-07-17 4:00 ` Stephen Hoff Hoffman
0 siblings, 2 replies; 4+ messages in thread
From: Edward Lee @ 1997-07-15 4:00 UTC (permalink / raw)
(Here's a correction to my correction.)
I would like to suggest a change to a code sample in the Alpha AXP
Architecture Reference Manual, 2nd edition, in section 4.6.1. The same
code sample also appears in the Alpha Architecture Handbook, version 3, in
section 4.6.1. The original code to compare two character strings is as
follows:
<initialize R1 to aligned QW address of string1>
<initialize R2 to aligned QW address of string2>
LOOP:
LDQ R3, 0(R1) ; Pick up 8 bytes of string1
LDA R1, 8(R1) ; Increment string1 pointer
LDQ R4, 0(R2) ; Pick up 8 bytes of string2
LDA R2, 8(R2) ; Increment string2 pointer
XOR R3, R4, R5 ; Test for all equal bytes
BEQ R5, LOOP ; Loop if all equal
CMPBGE R31, R5, R5 ;
... ; At this point, R5 can be used to
; determine the first not-equal
; byte position.
The original code will work most of the time, but it will fail in certain
situations where the two strings are identical, such as (in big endian):
This is a string.\0\0\0\0\0\0...\0\0\0\0\0\0Alpha\0...
------------------- String1
This is a string.\0\0\0\0\0\0...\0\0\0\0\0\0Bravo\0...
------------------- String2
What the original code does is successively compare corresponding groups of
8 bytes in the two strings until there is a inequality, even if the
comparison CONTINUES PAST THE ENDS OF BOTH STRINGS. In such a situation,
you will eventually get a false inequality or a segmentation fault, or a
crash, depending on what operating system you use.
Here is one possible alternative to the original code:
<initialize R1 to aligned QW address of string1>
<initialize R2 to aligned QW address of string2>
LOOP:
LDQ R3, 0(R1) ; Pick up 8 bytes of string1
LDA R1, 8(R1) ; Increment string1 pointer
LDQ R4, 0(R2) ; Pick up 8 bytes of string2
LDA R2, 8(R2) ; Increment string2 pointer
XOR R3, R4, R5 ; Test for all equal bytes
BNE R5, NOTEQUAL ; Branch if string1 != string2
CMPBGE R31, R3, R5 ; R3==R4. Look for a 0 byte in R3 or R4
BEQ R5, LOOP ; Loop if no terminating 0 byte was found
EQUAL:
... ; Handle the case when string1==string2
NOTEQUAL:
CMPBGE R31, R5, R5 ;
... ; At this point, R5 can be used to
; determine the first not-equal
; byte position.
I hope that someone at Digital gets this message to the appropriate people
to change the books and change any libraries which may have used the book's
original code example for comparing two strings.
-Ed L
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Suggested Change to Alpha Books (Correction)
1997-07-15 4:00 Suggested Change to Alpha Books (Correction) Edward Lee
@ 1997-07-16 4:00 ` Stephen Hoff Hoffman
1997-07-17 4:00 ` Stephen Hoff Hoffman
1 sibling, 0 replies; 4+ messages in thread
From: Stephen Hoff Hoffman @ 1997-07-16 4:00 UTC (permalink / raw)
In article <5qgipn$7q1$1@chinet.chinet.com>, edlee@chinet.chinet.com (Edward Lee) writes:
:I hope that someone at Digital gets this message to the appropriate people
:to change the books and change any libraries which may have used the book's
:original code example for comparing two strings.
The posting has been passed along to the folks that maintain the Alpha
Architecture Reference Manual, for their consideration.
-------------------------- pure personal opinion ---------------------------
Hoff (Stephen) Hoffman OpenVMS Engineering h*ffman@xdelta.enet.dec.c*m
headers and addresses munged to avoid automated spammers: junk-e-mail
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Suggested Change to Alpha Books (Correction)
1997-07-15 4:00 Suggested Change to Alpha Books (Correction) Edward Lee
1997-07-16 4:00 ` Stephen Hoff Hoffman
@ 1997-07-17 4:00 ` Stephen Hoff Hoffman
1997-07-17 4:00 ` Edward Lee
1 sibling, 1 reply; 4+ messages in thread
From: Stephen Hoff Hoffman @ 1997-07-17 4:00 UTC (permalink / raw)
In article <5qgipn$7q1$1@chinet.chinet.com>, edlee@chinet.chinet.com (Edward Lee) writes:
:...I would like to suggest a change to a code sample in the Alpha AXP
:Architecture Reference Manual, 2nd edition, in section 4.6.1...
:...The original code will work most of the time, but it will fail in certain
:situations where the two strings are identical...
:...I hope that someone at Digital gets this message to the appropriate people
:to change the books and change any libraries which may have used the book's
:original code example for comparing two strings.
The following response is a quote from one of the folks responsible
for the Alpha Architecture Reference Manual (ARM):
"Thanks for your comment. The example in the ARM is intended to
demonstrate possible uses of the CMPBGE instruction, and should
not be considered a complete routine for comparing character
strings. As you point out, the example provides no way to indicate
the length of the strings to be compared.
Assuming one wants to compare NUL-terminated strings, as in C's
strcmp routine, your suggested replacement code would work. Note
that if control reaches your "NOTEQUAL" label, the strings might
still be equal up to the terminating NUL in each string.
We may change the ARM example to something like the following:
To compare two null-terminated strings for greater/equal/less:
<initialize R1 to aligned QW address of string1>
<initialize R2 to aligned QW address of string2>
LOOP:
LDQ R3, 0(R1) ; Pick up 8 bytes of string1
LDA R1, 8(R1) ; Increment string1 pointer
LDQ R4, 0(R2) ; Pick up 8 bytes of string2
LDA R2, 8(R2) ; Incremente string2 pointer
CMPBGE R31, R3, R6 ; Test for zeros in string1
XOR R3, R4, R5 ; Test for all equal bytes
BNE R6, DONE ; Exit if a zero found
BEQ R5, LOOP ; Else repeat if all equal
DONE: CMPBGE R31, R5, R5 ;
...
; At this point, R5 can be used to determine the first not-equal
; byte position (if any), and R6 can be used to determine the
; position of the terminating zero in string 1 (if any).
By the way, none of Digital's library string-compare routines run off
the end of a string onto a new page. (Some routines may fetch beyond
the point of first difference, for better performance.)"
-------------------------- pure personal opinion ---------------------------
Hoff (Stephen) Hoffman OpenVMS Engineering h*ffman@xdelta.enet.dec.c*m
headers and addresses munged to avoid automated spammers: junk-e-mail
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Suggested Change to Alpha Books (Correction)
1997-07-17 4:00 ` Stephen Hoff Hoffman
@ 1997-07-17 4:00 ` Edward Lee
0 siblings, 0 replies; 4+ messages in thread
From: Edward Lee @ 1997-07-17 4:00 UTC (permalink / raw)
In article <5qlpi7$g3c@usenet.pa.dec.com>,
Stephen Hoff Hoffman <hoffman@xdelta.zko.dec.nospam> wrote:
> The following response is a quote from one of the folks responsible
> for the Alpha Architecture Reference Manual (ARM):
>
>"[....]
>
>Assuming one wants to compare NUL-terminated strings, as in C's
>strcmp routine, your suggested replacement code would work. Note
>that if control reaches your "NOTEQUAL" label, the strings might
>still be equal up to the terminating NUL in each string.
I was assuming that the ends of strings would be padded with '\0' characters
up to the next quadword boundary if someone were to go to the trouble of
quadword aligning every string. I should have stated that.
>We may change the ARM example to something like the following:
>
>
>To compare two null-terminated strings for greater/equal/less:
>
> <initialize R1 to aligned QW address of string1>
> <initialize R2 to aligned QW address of string2>
>LOOP:
> LDQ R3, 0(R1) ; Pick up 8 bytes of string1
> LDA R1, 8(R1) ; Increment string1 pointer
> LDQ R4, 0(R2) ; Pick up 8 bytes of string2
> LDA R2, 8(R2) ; Incremente string2 pointer
> CMPBGE R31, R3, R6 ; Test for zeros in string1
> XOR R3, R4, R5 ; Test for all equal bytes
> BNE R6, DONE ; Exit if a zero found
> BEQ R5, LOOP ; Else repeat if all equal
>DONE: CMPBGE R31, R5, R5 ;
> ...
>; At this point, R5 can be used to determine the first not-equal
>; byte position (if any), and R6 can be used to determine the
>; position of the terminating zero in string 1 (if any).
>[....]
In my suggested code change, I deliberately tried to keep the number
of registers that were used down to the original 5. The code above
looks fine to me as well.
Thanks for the prompt response.
-Ed L
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~1997-07-17 4:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1997-07-15 4:00 Suggested Change to Alpha Books (Correction) Edward Lee
1997-07-16 4:00 ` Stephen Hoff Hoffman
1997-07-17 4:00 ` Stephen Hoff Hoffman
1997-07-17 4:00 ` Edward Lee
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox