User Tools

Site Tools


base:two_s_complement_system

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

base:two_s_complement_system [2015-04-17 04:34]
127.0.0.1 external edit
base:two_s_complement_system [2021-09-09 07:52] (current)
strobe Add alternate 2's complement method, fix typo.
Line 3: Line 3:
 A two's-complement system or two's-complement arithmetic is a system in which negative numbers are represented by the two's complement of the absolute value;[1] this system is the most common method of representing signed integers on computers. In such a system, a number is negated (converted from positive to negative or vice versa) by computing its two's complement. A two's-complement system or two's-complement arithmetic is a system in which negative numbers are represented by the two's complement of the absolute value;[1] this system is the most common method of representing signed integers on computers. In such a system, a number is negated (converted from positive to negative or vice versa) by computing its two's complement.
  
-//The importance of this system is that it helps us represent negative numbers, while addition (ADC) and substraction (SBC) operations can be used exactly the same way as with unsigned numbers.//+//The importance of this system is that it helps us represent negative numbers, while addition (ADC) and subtraction (SBC) operations can be used exactly the same way as with unsigned numbers.//
  
 In finding the two's complement of a binary number, the bits are inverted, or "flipped", by using the EOR #$FF operation; the value of 1 is then added (CLC ADC #$01) to the resulting value. Bit overflow is ignored, which is the normal case with zero. In finding the two's complement of a binary number, the bits are inverted, or "flipped", by using the EOR #$FF operation; the value of 1 is then added (CLC ADC #$01) to the resulting value. Bit overflow is ignored, which is the normal case with zero.
Line 63: Line 63:
  ROR A       ;ROR shifts in Carry from the left, thus keeping our sign the same  ROR A       ;ROR shifts in Carry from the left, thus keeping our sign the same
 </code> </code>
 +
 +====== Alternate method ======
 +
 +An alternative to the inverse-then-increment 2's complement is to simply subtract from zero.
 +
 +Unlike the above method, this also works for 16bit or larger numbers.
 +
 +//This assumes your value is in memory.//
 +
 +<code>
 + SEC
 + LDA #$00
 + SBC Value
 +</code>
 +
 +16 bit version:
 +<code>
 + SEC
 + LDA #$00
 + SBC ValueLo
 +        STA ValueLo
 + LDA #$00
 + SBC ValueHi
 + STA ValueHi
 +        ; Repeat the last 3 instructions as needed for any additional bytes
 +</code>
 +
 +Tip: If the value of the carry flag is known on entry, you can either drop the SEC if the carry is set or drop the SEC and change the initial LDA #0 to a LDA #1 if the carry is clear, saving 1 byte and 2 cycles.
base/two_s_complement_system.txt ยท Last modified: 2021-09-09 07:52 by strobe