Abstract:
A method and apparatus for detecting the null byte at the end of a character string. The method and apparatus first logically concatenates two 32-bit input values into a single 64 bit value. Next, the 64-bit value is divided into 8 bytes. Then, a logical OR operation is performed on each byte and the results are put into an encoder. Finally, the encoder interprets the results of the OR operations and places output values into processor registers which indicate whether or where a null byte was detected.

Description:
TECHNICAL FIELD OF THE INVENTION 
     This invention is related in general to data processing systems and in specific to a method and apparatus for efficiently processing null terminated character strings in a data processing system. 
     BACKGROUND OF THE INVENTION 
     In computer programming languages such as C and C++, character strings are terminated by a null byte. In other words, character strings are represented within the computer as a series of bytes followed by a byte in which all bits are zero. There are many instances in typical C programs when the computer&#39;s central processing unit (&#34;CPU&#34;) must quickly find the terminating null byte. When performing a string copy instruction, for example, the CPU determines the length of the string by detecting the string&#39;s terminating null byte. 
     Prior art CPUs have used several different methods to find a string&#39;s null byte. One such method involved processing the string one byte at a time. The processor would merely check each byte to determine whether it was null and then react accordingly. This method was very slow because each iteration of the process only handled a single byte. Thus, one iteration of the checking algorithm was required for each character in the string. 
     Another prior art method was to use a series of complex instructions to perform multi-byte string processing. This method, however, added significant complexity to the data flow because it required multiple cycles to complete. Similarly, the IBM RISC System/6000™ processor used a single complex instruction to find the null byte of a string. The RISC System/6000™ method was undesirable because it required much logic that was not used by other processor instructions. 
     Therefore, there exists a need for a technique which quickly finds the null byte of a character string yet integrates well into current processor designs. 
     SUMMARY OF THE INVENTION 
     It is an object of the present invention to provide a technique which quickly and efficiently detects a null byte at the end of a character string. 
     It is another object of the present invention to provide a technique which detects a null byte at the end of a character string that integrates well with current Reduced Instruction-set Computer (&#34;RISC&#34;) design techniques. 
     These and other objectives of the present invention are accomplished by a processor instruction in a data processing system comprising a CPU which performs the task. The instruction first logically concatenates two 32-bit input values into a single 64-bit value. Next, the 64-bit value is divided into eight 8-bit bytes. Then, a logical OR operation is performed on the bits of each byte and the results are placed in an encoder. Finally, the encoder interprets the results of the OR operations and places output values into processor registers which indicate whether and/or where a null byte was detected. 
     The foregoing has outlined rather broadly the features and technical advantages of the present invention in order that the detailed description of the invention that follows may be better understood. Additional features and advantages of the invention will be described hereinafter which form the subject of the claims of the invention. It should be appreciated by those skilled in the art that the conception and the specific embodiment disclosed may be readily utilized as a basis for modifying or designing other structures for carrying out the same purposes of the present invention. It should also be realized by those skilled in the art that such equivalent constructions do not depart from the spirit and scope of the invention as set forth in the appended claims. 
    
    
     BRIEF DESCRIPTION OF THE DRAWINGS 
     For a more complete understanding of the present invention, and the advantages thereof, reference is now made to the following description taken in conjunction with the accompanying drawing, in which: 
     FIG. 1 illustrates a data processing system embodying the present invention; 
     FIG. 2 is a block diagram of logic implementing the present invention. 
    
    
     DETAILED DESCRIPTION OF THE INVENTION 
     A preferred embodiment of the present invention is implemented as the dlmzb, or determine left-most zero byte, instruction in various members of the PowerPC 400 family of embedded controllers. A representative hardware system for practicing the present invention is depicted in FIG. 1, which illustrates a typical hardware configuration of a workstation in accordance with the subject invention having central processing unit 10, the PowerPC 400, and a number of other units interconnected via system bus 12. The workstation shown in FIG. 1 includes random access memory (RAM) 14, read only memory (ROM) 16, and input/output (I/O) adapter 18 for connecting peripheral devices such as disk units 20 and tape drives 40 to bus 12, user interface adapter 22 for connecting keyboard 24, mouse 26, speaker 28, microphone 32, and/or other user interface devices such as a touch screen device (not shown) to bus 12, communication adapter 34 for connecting the workstation to a data processing network, and display adapter 36 for connecting bus 12 to display device 38. 
     FIG. 2 is a block diagram showing a high level view of a logic circuit embodying the present invention. The logic circuit has two registers, RS 210 and RB 212, of four bytes 214-220, 222-228 each. Outputs of each register 214-228 are connected to the inputs of corresponding OR gates 230-244. The outputs of OR gates 230-244 are connected to an encoder 246. The outputs of the encoder 246 are, in turn, connected to the CRO register 248, the RA register 250 and the XER register 252. 
     Registers RS 210 and RB 212 each hold four bytes 214-220, 222-228 of eight bits each. A bit is a binary value and can be either &#34;0&#34; or &#34;1&#34;. Most programming languages, including C, internally represent characters and control codes such as null as eight bit values. A null is represented as a byte in which all eight bits are &#34;0.&#34; Thus, the two registers 210, 212 can together hold a total of eight characters or control codes. See B. Kernighan &amp; D. Ritchie, The C Programming Language, Prentice-Hall, 2d Ed. 1988, pp. 36-46, which is hereby incorporated by reference, for a more complete discussion of how C internally represents data. 
     Each of the eight OR gates 214-228 has eight inputs and one output. Each input of an OR gate 230-244 is connected to a bit of a corresponding register byte 214-228 output. For example, each input of OR gate 230 is connected to a bit output of register byte 214. Each OR gate 230-244 performs a logical OR on its inputs and sends the result to the encoder 246. Of course, an OR gate 230-244 will output a &#34;1&#34; if any of its inputs is a &#34;1&#34;. If all of the inputs are &#34;0&#34;, an OR gate 230-244 will output a &#34;0&#34;. 
     The encoder 246 receives as input the results of the eight OR operations performed by the OR gates 230-244. The encoder 246 outputs null byte detection information to the CRO 248, RA 250 and XER 252 registers which indicates whether and/or where a null byte is found. The encoder 246 outputs a number to the XER 252 and RA 250 registers which represents the byte number of the leftmost byte whose logical OR value is &#34;0&#34; with the bytes 214-228 numbered from left to right starting with &#34;1&#34;, or encoder 246 outputs an &#34;8&#34; if no byte has a logical OR value of &#34;0&#34;. For example, if both bytes 218 and 224 have a logical OR value of &#34;0&#34;, the encoder 246 will store a &#34;3&#34; in XER register 252, which is the byte number of the leftmost byte to output a &#34;0&#34; (byte 218). 
     The CRO register 48 has four bits 254-260. These bits 254-260 normally hold the results of comparison operations performed by processor instructions. Bit 254 is set to &#34;1&#34; to indicate less than (&#34;LT&#34;), bit 256 is set to &#34;1&#34; to indicate greater than (&#34;GT&#34;), bit 258 is set to &#34;1&#34; to indicate equal (&#34;EQ&#34;) and bit 260 is set to &#34;1&#34; to indicate summary overflow. The encoder 246 sets the EQ bit 258 if no input to encoder 246 has a logical OR value of &#34;0&#34;, sets GT bit 256 if any of the first four bytes 214-220 have a logical OR value of &#34;0&#34; and sets the LT bit 254 if any of the second four bytes 222-228 have a logical OR value of &#34;0&#34; and none of the first four bytes are &#34;0.&#34; 
     In operation, the dlmzb instruction is typically combined with other machine instructions. For example, the PowerPC processor can use dlmzb to execute the C language function strcmp(). This function compares two strings, A and B, and returns a 0 if the strings are equal, a 1 if A is greater than B or a 0 if A is less than B. The pseudocode in Appendix I explains how the PowerPC executes the strcmp() function. 
     The source code in Appendix II shows how strcmp() is actually performed by the PowerPC 400 processor 10. This code may appear convoluted to the reader because it has been optimized to take advantage of the instruction pipelining features of the PowerPC 403GA processor. The lines of the source code have been numbered to assist the below explanation. 
     In the code example of Appendix II, strings A and B are located at the memory addresses held in registers r3 and r4, respectively. Line 2 loads four bytes of A into r5 and four bytes into r6. Similarly, line 3 loads B into r7 and r8. Lines 4 and 5 compare r5 with r7 and r6 with r8 and store the results in CR6 and CR7, respectively. These results can be equal (&#34;=&#34;), greater than (&#34;&gt;&#34;) or less than (&#34;&lt;&#34;). 
     Line 6 contains the present invention. The instruction, dlmzb., detects the presence of a null byte in either r5 or r6 and stores the location of the null byte, or an eight if no null is found, in XER and r9. In addition, the &#34;. &#34; after the instruction implicitly tells the processor to store the condition flags (&gt;,&lt; or =) in the CRO register. 
     Line 7 branches to the a --  ne subroutine if the condition flag in CR6 is not set to =. Note that this flag was set in line 4. Lines 8 and 11 increment the pointers to A and B, respectively. Line 9 branches to rtn --  eq if the CRO register is set to &gt;. Line 10 branches to b --  ne if CR7 is not set to =. Line 12 branches back to line 2 if CRO is set to &gt;, indicating that a null was found in the first 4 bytes. 
     The rtn --  eq subroutine at line 13 returns a zero by storing that value in r3. Line 14 branches back to the routine that called the strcmp() function. 
     The a --  ne subroutine begins at line 15. Line 15 branches to a --  ex if CRO contains a &lt; or =. Lines 16-19 execute if the GT bit 258 of CRO is not set. These instructions use the value in r9 to shift the word to the right, thereby accounting for the null. Line 20 recompares the values in r5 and r7. Line 21 branches to rtn --  eq if CR6 contains =. 
     Subroutine a --  ex begins on line 22. Line 22 branches to rtn --  gt if CR6 contains &gt;. Otherwise, lines 23-24 return a -1 to the calling routine. 
     Subroutine b --  ne begins on line 25. Line 25 branches to b --  ex if CRO contains a &gt; or =. Otherwise, lines 26-31 perform a shift, recompare and a conditional branch if the LT bit 256 of CRO is not set. Thus, b --  ne is much like a --  ne. 
     Line 32 marks the beginning of b --  ex. Line 32 branches to rtn --  gt if CR7 contains a &gt;. Otherwise, lines 33-34 return a -1 to the calling routine. Similarly, rtn --  gt, starting on line 35, returns a 1 to the calling routine. 
     For a more detailed description of PowerPC program instructions, with the exception of dlmzb, the reader is advised to consult The PowerPC 403GA Embedded Controller User&#39;s Manual, IBM Doc. MPR403-UMV-01, September 1994, which is hereby incorporated by reference herein. 
     The source code and system design described above are for illustrative purposes only and various changes of the above may be within the scope of the appended claims. 
     Although the present invention and its advantages have been described in detail, it should be understood that various changes, substitutions and alterations can be made to the above without departing from the spirit and scope of the invention as defined by the appended claims. 
     
         ______________________________________APPENDIX I______________________________________Load 8 bytes of both strings being comparedLoop:If high order words are not equalIf no nulls in high order word of first operandIf a&gt;b   Return 1     Else   Return -1     ElseShift right to eliminate bytes to the right of thenullsRecompareIf a&gt;b   Return 1Else   If a=b     Return 0   Return -1ElseIf nulls in high order wordReturn 0If low order words are not equalIf no nulls in low order word of first operandIf a&gt;b   Return 1Else   Return -1ElseShift right to eliminate bytes to the right of thenullsRecompareIf a&gt;b   Return 1Else   If a=bElse goto Loop______________________________________ 
    
     
         ______________________________________APPENDIX II______________________________________loop:          lswi        r5,r3,8          lswi        r7,r4,8          cmplw       6,r5,r7          cmplw       7,r6,r8          dlmzb.      r9,r5,r6          bne         cr6,a.sub.-- ne          addi        r3,r3,8          bgt         rtn.sub.-- eq          bne         cr7,b.sub.-- ne          addi        r4,r4,8          bge         looprtn.sub.-- eq: addi        r3,0,0          blra.sub.-- ne:   ble         a.sub.-- ex          slwi        r9,r9,3          subfic      r9,r9,32          srw         r5,r5,r9          srw         r7,r7,r9          cmplw       6,r5,r7          beq         cr6,rtn.sub.-- eqa.sub.-- ex:   bgt         cr6,rtn.sub.-- gt          addi        r3,0,-1          blrb.sub.-- ne:   bge         b.sub.-- ex          slwi        r9,r9,3          subfic      r9,r9,64          srw         r6,r6,r9          srw         r8,r8,r9          cmplw       7,r6,r8          beq         cr7,rtn.sub.-- eqb.sub.-- ex:   bgt         cr7,rtn.sub.-- gt          addi        r3,0, -1          blrrtn.sub.-- gt: addi        r3,0,1          blr______________________________________