[comp.arch] while

hascall@atanasoff.cs.iastate.edu (John Hascall) (02/08/89)

In article <3300050@m.cs.uiuc.edu> gillies@m.cs.uiuc.edu writes:

>/* Written 12:25 am  Feb  5, 1989 by PLS@cup.portal.com in m.cs.uiuc.edu:comp.arch */
>This deserves a new subject.


>Second, I was once told that the following C code compiles into 1
>instruction (or something amazingly short) on the PDP-11, C's mother
>machine:

>while (*p++ = *q++);

>This is perhaps part of the reason why strings were designed with
>null-termination

  First, I assume you mean:

     char   *p,*q;

     while (*p++ == *q++);

  I can see no way to code this in 1 PDP instruction, the best I can see
  is (assume R1 is p, R2 is q):

     L:    MOVB  (R1)+,R0              ; temp <- *p++
	   CMPB  (R2)+,R0              ; compare *q++ to temp
	   BEQ   L                     ; again if equal

  now on a machine with more flexibility in addressing modes (i.e., a
  VAX):

     L:    CMPB  (R1)+,(R2)+           ; compare *p++ to *q++
	   BEQL  L                     ; again if equal

  or if we make the assumption that "strings" p and q are less than 64K
  long:

	   CMPC3 #65535,(R1)+,(R2)+    ; while (*p++ == q++);

  now on a MC680x0 (A1,A2):

     L:    CMPB  (A1)+,(A2)+           ; compare *p++ to *q++
	   DBNE  D0,L                  ; "loop mode", assume D0=LARGE_NUMBER
	      or
	   BEQ   L                     ; non-"loop mode"

  Are their any machines which have a combined "compare-and-branch"
  instruction?  Are their any (other) machines which can do this in
  1 instruction (with or without assumptions)?


  John Hascall
  ISU Comp Center

kolding@uw-june (Eric Koldinger) (02/09/89)

In article <775@atanasoff.cs.iastate.edu> hascall@atanasoff.cs.iastate.edu (John Hascall) writes:
>In article <3300050@m.cs.uiuc.edu> gillies@m.cs.uiuc.edu writes:
>
>>instruction (or something amazingly short) on the PDP-11, C's mother
>>machine:
>
>>while (*p++ = *q++);
>
>>This is perhaps part of the reason why strings were designed with
>>null-termination
>
>  First, I assume you mean:
>
>     char   *p,*q;
>     while (*p++ == *q++);
>
>  I can see no way to code this in 1 PDP instruction, the best I can see
>  is (assume R1 is p, R2 is q):

Piece of cake:
For the loop as he put it, with your assumptions:
loop:	movb	(r2)+, (r1)+
	bneq	loop

Two instructions for the loop.  The loop as you wrote it (which doesn't really
do very much, but here goes), again with  your assumptions:
loop:	cmpb	(r2)+, (r1)+
	beq	loop
    
God it's good to write some PDP-11 code again.  Now there was a great machine
even if it did only have 64K of memory.

------
	_   /|				Eric Koldinger
	\`o_O'				University of Washington
  	  ( )     "Gag Ack Barf"	Department of Computer Science
       	   U				kolding@cs.washington.edu