<div dir="ltr"><div>Thanks Dave!</div><div><br></div><div>The TwosComplementRegister is a good package and will be a helpful guide -- thanks for pointing me to it.</div><div>  <br></div><div>For posterity I want to post the key functionality here (which was in #asSignedInteger). It more or less describes how to take bits (as an ordered collection of some kind) that are already in two's complement negative format and convert them to a Squeak negative Integer:</div><div><br></div><div><span style="font-family:monospace">asSignedInteger<br><br></span><div style="margin-left:40px"><span style="font-family:monospace">     self negative</span><br></div><div style="margin-left:40px"><span style="font-family:monospace"></span></div><div style="margin-left:40px"><span style="font-family:monospace">           ifTrue: [^ ((bits reversed collect: [:e | e value not])</span><br></div><div style="margin-left:40px"><span style="font-family:monospace"></span></div><div style="margin-left:80px"><span style="font-family:monospace">                         inject: 0</span><br></div><div style="margin-left:40px"><span style="font-family:monospace"></span></div><div style="margin-left:80px"><span style="font-family:monospace">                               into: [:val :bit | bit value</span><br></div><div style="margin-left:40px"><span style="font-family:monospace"></span></div><div style="margin-left:120px"><span style="font-family:monospace">                                           ifTrue: [val << 1 + 1]</span><br></div><div style="margin-left:40px"><span style="font-family:monospace"></span></div><div style="margin-left:120px"><span style="font-family:monospace">                                           ifFalse: [val << 1]]) negated - 1]</span><br></div><div style="margin-left:40px"><span style="font-family:monospace"></span></div><div style="margin-left:40px"><span style="font-family:monospace">                ifFalse: [^ self asUnsignedInteger]</span></div></div><div><br></div><div>Thanks again,<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jun 16, 2020 at 2:19 PM David T. Lewis <<a href="mailto:lewis@mail.msen.com">lewis@mail.msen.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Try loading package TwosComplement from the SqueakMap loader, or<br>
install it like this:<br>
<br>
  Installer ss project: 'TwosComplement';<br>
      package: 'TwosComplement';<br>
      install.<br>
<br>
Then inspect these:<br>
<br>
  TwosComplementRegister width: 8 value: -1.<br>
<br>
  255 asRegister: 8. "same as above but note the overflow bit is set"<br>
<br>
  TwosComplementRegister width: 64 value: -1.<br>
<br>
Dave<br>
<br>
<br>
On Tue, Jun 16, 2020 at 02:09:15PM -0400, Eric Gade wrote:<br>
> Hi Karl,<br>
> <br>
> Integer readFrom: '11111111' readStream base: 2.  ?<br>
> ><br>
> <br>
> When I evaluate this in Squeak, the Integer produced is 255 (I need to get<br>
> from 255 to -1 -- and back again if possible).<br>
> <br>
> One hint that I've found in the image is that Integers respond to a message<br>
> #highBitOfMagnitude, which gives me the value of the most significant bit<br>
> in the integer. That is a starting point if I want to treat the bits in<br>
> twos-complement: I can determine if the desired representation should be<br>
> negative or positive. However, I'm not sure how to actually translate the<br>
> bits into a matching Squeak integer with the correct sign and value.<br>
> <br>
> Here is a more concrete explanation of my issue: In the implementation /<br>
> simulation, Register objects store their values as Squeak Integers, and<br>
> they are truncated at evaluation to be 32-bit integers. Some instructions<br>
> will treat a register as signed and others as unsigned. For unsigned<br>
> treatment, there's no problem: Squeak always has the correct bit values for<br>
> what I need. I just need to figure out what to "do" with the integer in the<br>
> register when I want to treat it as signed. In the case of a value like -1<br>
> ('11111111') I can send that #highBitOfMagnitude to determine if it should<br>
> be a negative or positive value, but then what? I can't just send<br>
> #negative, because that gives me -255<br>
> <br>
> PS - Perhaps at some point there was a #twosComplement method implemented<br>
> on Integers. There is an EToys object that is currently sending the message<br>
> (which has no implementors in 5.3 as far as I can see): SecurityManager >><br>
> #asn1Integer:<br>
> <br>
> On Tue, Jun 16, 2020 at 1:44 PM karl ramberg <<a href="mailto:karlramberg@gmail.com" target="_blank">karlramberg@gmail.com</a>> wrote:<br>
> <br>
> > Integer readFrom: '11111111' readStream base: 2.  ?<br>
> ><br>
> > Best,<br>
> > Karl<br>
> ><br>
> ><br>
> ><br>
> ><br>
> > On Tue, Jun 16, 2020 at 6:36 PM Eric Gade <<a href="mailto:eric.gade@gmail.com" target="_blank">eric.gade@gmail.com</a>> wrote:<br>
> ><br>
> >> Hi everyone,<br>
> >><br>
> >> I'm smack in the middle of making some RISC-V tools in Squeak, which<br>
> >> means I'm dealing with a lot of low-level bit manipulation.<br>
> >><br>
> >> One question I have is how best to deal with two's complement<br>
> >> representations of integers in Squeak, and how to translate between the<br>
> >> different (positive v negative) values based on a given set of bits (or a<br>
> >> byteArray or whatever).<br>
> >><br>
> >> For example, doing the following:<br>
> >> -1 printStringBase: 2 nDigits: 8. "11111111"<br>
> >> Gives the expected binary value "11111111"<br>
> >><br>
> >> However (and as we should expect), evaluating the following:<br>
> >> 2r11111111. "255"<br>
> >><br>
> >> Gives the (again, expected) value 255.<br>
> >><br>
> >> My question is: what is the best way to convert between the complements<br>
> >> in Squeak? How can I take 255, examine its bits, and get -1 as the response<br>
> >> (or convert in the reverse)? I'm assuming there are already ways to deal<br>
> >> with this, I just cannot find them.<br>
> >><br>
> >> Thanks!<br>
> >><br>
> >> --<br>
> >> Eric<br>
> >><br>
> >><br>
> ><br>
> <br>
> -- <br>
> Eric<br>
<br>
> <br>
<br>
<br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>Eric</div></div></div>