From: Jacek Generowicz
Subject: ldb, dpb, byte ...
Date: 
Message-ID: <tyfk7o6jpfl.fsf@pcitapi22.cern.ch>
Hi,

I had never heard of ldb and its friends until Gabe mentioned them in
another thread. I looked at the HyperSpec to see what they were about,
and was left wondering why they are the way they are. The 'Spec's
examples haven't inspired an intuitive understaning of why the
functions were designed to work in the way they do. Clearly, there
must be something that I'm missing.

I fear that, unless I get an "Aha!" moment, these functions will
disappear from my memory (and be overlooked at some time in the future
when they would have provided the perfect solution to a problem at
hand).

Could you please give some explanation of how these beasts are used ?

Thanks,

From: Joe Marshall
Subject: Re: ldb, dpb, byte ...
Date: 
Message-ID: <7phW8.327611$6m5.325883@rwcrnsc51.ops.asp.att.net>
"Jacek Generowicz" <················@cern.ch> wrote in message ····················@pcitapi22.cern.ch...
> Hi,
>
> I had never heard of ldb and its friends until Gabe mentioned them in
> another thread. I looked at the HyperSpec to see what they were about,
> and was left wondering why they are the way they are. The 'Spec's
> examples haven't inspired an intuitive understaning of why the
> functions were designed to work in the way they do. Clearly, there
> must be something that I'm missing.
>
> I fear that, unless I get an "Aha!" moment, these functions will
> disappear from my memory (and be overlooked at some time in the future
> when they would have provided the perfect solution to a problem at
> hand).
>
> Could you please give some explanation of how these beasts are used ?

They are based on the old PDP instruction set.

LDB (mnemonic for load byte, pronounced "LID-ib") is used for extracting
a bit field.

DPB (mnemonic for deposit byte, pronounced "d-PIB") is used for
inserting a bit field.


Extract the field 5 bits wide, 6 bits over:

(format t "~b" (ldb (byte 5 6) #b100010111010001))
 => 10111

Modify the field 5 bits wide, 6 bits over

(format t "~b" (dpb #b01010 (byte 5 6) #b1111111111111))
 => 1101010111111
From: Petter Gustad
Subject: Re: ldb, dpb, byte ...
Date: 
Message-ID: <87r8ierwvg.fsf@filestore.home.gustad.com>
"Joe Marshall" <·············@attbi.com> writes:

> "Jacek Generowicz" <················@cern.ch> wrote in message ····················@pcitapi22.cern.ch...
> > Hi,
> >
> > I had never heard of ldb and its friends until Gabe mentioned them in
> > another thread. I looked at the HyperSpec to see what they were about,
> > and was left wondering why they are the way they are. The 'Spec's
> > examples haven't inspired an intuitive understaning of why the
> > functions were designed to work in the way they do. Clearly, there
> > must be something that I'm missing.
> >
> > I fear that, unless I get an "Aha!" moment, these functions will
> > disappear from my memory (and be overlooked at some time in the future
> > when they would have provided the perfect solution to a problem at
> > hand).
> >
> > Could you please give some explanation of how these beasts are used ?
> 
> They are based on the old PDP instruction set.
> 
> LDB (mnemonic for load byte, pronounced "LID-ib") is used for extracting
> a bit field.

LDB is also settable so it can also be used to stuff bits into bytes,
e.g. setting bits 2,3,4,5 in x:

(setf x #x0000)
(setf (ldb (byte 4 2) x) #xf)
(format nil "~B" x)

would return "111100"

"LISP is a higher level machine language"

BYTE August 1979 Volume 4 Number 8, page 10 "An Overview of LISP" by
John Allen, Signetics


Petter
-- 
________________________________________________________________________
Petter Gustad   8'h2B | (~8'h2B) - Hamlet in Verilog   http://gustad.com
From: Christopher C. Stacy
Subject: Re: ldb, dpb, byte ...
Date: 
Message-ID: <uwus6tg39.fsf@grant.org>
>>>>> On 08 Jul 2002 15:54:06 +0200, Jacek Generowicz ("Jacek") writes:

 Jacek> Hi,
 Jacek> I had never heard of ldb and its friends until Gabe mentioned them in
 Jacek> another thread. I looked at the HyperSpec to see what they were about,
 Jacek> and was left wondering why they are the way they are. The 'Spec's
 Jacek> examples haven't inspired an intuitive understaning of why the
 Jacek> functions were designed to work in the way they do. Clearly, there
 Jacek> must be something that I'm missing.

 Jacek> I fear that, unless I get an "Aha!" moment, these functions will
 Jacek> disappear from my memory (and be overlooked at some time in the future
 Jacek> when they would have provided the perfect solution to a problem at
 Jacek> hand).

 Jacek> Could you please give some explanation of how these beasts are used ?

Those functions are for bit twiddling.

(defun decode-ip-address (address)
  (values (ldb (byte 8 24) address)
	  (ldb (byte 8 16) address)
	  (ldb (byte 8 08) address)
	  (ldb (byte 8 00) address)))


The functions are modeled after the PDP-10 machine instructions LDB
and DPB; the PDP-10 supported arbitrary sized bytes in this way.
From: Barry Margolin
Subject: Re: ldb, dpb, byte ...
Date: 
Message-ID: <01kW8.13$4P.2101@paloalto-snr1.gtei.net>
In article <···············@pcitapi22.cern.ch>,
Jacek Generowicz  <················@cern.ch> wrote:
>I had never heard of ldb and its friends until Gabe mentioned them in
>another thread. I looked at the HyperSpec to see what they were about,
>and was left wondering why they are the way they are. The 'Spec's
>examples haven't inspired an intuitive understaning of why the
>functions were designed to work in the way they do. Clearly, there
>must be something that I'm missing.

If you're familiar with C, they're often used for similar things as C's
bit-fields.  In Lisp-based OS or device interface programming they're used
to access pieces of registers.

They're also useful in bit-twiddling numeric applications.  For instance,
a traditional pseudo-random number generator works by multiplying the
recent values by a factor and then extracting some number of middle bits of
the product, and LDB is perfect for that extraction.

In other languages you would have to do a shift and mask.  LDB and DPB save
you the effort of writing multiple steps and computing the appropriate mask.

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Jacek Generowicz
Subject: Re: ldb, dpb, byte ...
Date: 
Message-ID: <tyfbs9gjbzm.fsf@pcitapi22.cern.ch>
Thank you, gentlemen, for your input.