User Tools

Site Tools


aslfaqs

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
aslfaqs [2025/09/23 17:28] – [How do negative numbers work?] reggieaslfaqs [2025/09/23 17:33] (current) – [How do negative numbers work?] reggie
Line 659: Line 659:
 In binary the principle works just the same, only (for an 8-bit value) we subtract from 256 instead of 60, ie. "minus ten" equals "256-10", being 246, or in hex $f6. Then if we see 246 and want to see the "minus" variant, we do the same, subtract it from 256. In binary the principle works just the same, only (for an 8-bit value) we subtract from 256 instead of 60, ie. "minus ten" equals "256-10", being 246, or in hex $f6. Then if we see 246 and want to see the "minus" variant, we do the same, subtract it from 256.
  
-Which signed numbers are positive and which negative? Highest bit set means negative, so unsigned values 128 to 255 represent -128 to -1. Positive signed values range from 0 to 127. This is called "two's-complement" format, beacuse to negate we can complement (COM, or flip) all the bits and add 1 to the result.+Which signed numbers are positive and which negative? Highest bit set means negative, so unsigned values 128 to 255 represent -128 to -1. Positive signed values range from 0 to 127. This is called "two's-complement" format, because to negate we can flip (or COMplement) all the bits and add 1 to the result.
  
 This format is chosen because the underlying circuitry doesn't need to change; it's simply down to the programmer to keep track of which variables are seen as signed and unsigned. There are two flags in the condition codes to help with signed numbers: the N flag is set whenever bit 7 of a result is set, and V is set when a signed result overflows. An overflow is an unintended sign change; to use our clock analogy it's when the minute hand passes through the half-hour without going through the hour first. This format is chosen because the underlying circuitry doesn't need to change; it's simply down to the programmer to keep track of which variables are seen as signed and unsigned. There are two flags in the condition codes to help with signed numbers: the N flag is set whenever bit 7 of a result is set, and V is set when a signed result overflows. An overflow is an unintended sign change; to use our clock analogy it's when the minute hand passes through the half-hour without going through the hour first.
Line 665: Line 665:
 Which instructions deal with signed numbers? There's SEX (Sign EXtend) which changes a signed value in B to a signed value in D by simply filling A with 8 copies of bit 7 of B. To negate an 8-bit value use NEG. All the addition and subtraction instructions work with either interpretation, but note that MUL is always unsigned. The conditional branches (also having long variants) are:  Which instructions deal with signed numbers? There's SEX (Sign EXtend) which changes a signed value in B to a signed value in D by simply filling A with 8 copies of bit 7 of B. To negate an 8-bit value use NEG. All the addition and subtraction instructions work with either interpretation, but note that MUL is always unsigned. The conditional branches (also having long variants) are: 
  
-  Signed branches   Meaning             Unsigned equivalent+  Signed branches    Meaning             Unsigned equivalent
      
-       BPL             Plus                     +       BPL             PLus                     
-       BMI             Minus +       BMI             MInus 
-       BLT             Less-than                BLO +       BLT             Less-Than                BLO 
-       BLE             Less-than or equal       BLS +       BLE             Less-than or Equal       BLS 
-       BGT             Greater-than             BHI +       BGT             Greater-Than             BHI 
-       BGE             Greater-than or equal    BHS+       BGE             Greater-than or Equal    BHS
                
   Miscellaneous instructions   Miscellaneous instructions
          
-  NEG <accumulator or memory> NEGate, 8-bit only) +  NEG (8-bit only)     NEGate                   COM 
-  SEX <inherent>              (Sign EXtend B to D) +  SEX                  Sign EXtend B to D 
-  COMA; COMB; ADDD #1         (negate D)+  COMA; COMB; ADDD #1  negate D
  
 Note that other than the simple BPL/BMI, the signed variants are not commonly used. If you do choose one, think twice whether you meant the unsigned variant. Signed comparisons should not be used with addresses for example. Note that other than the simple BPL/BMI, the signed variants are not commonly used. If you do choose one, think twice whether you meant the unsigned variant. Signed comparisons should not be used with addresses for example.
aslfaqs.txt · Last modified: 2025/09/23 17:33 by reggie

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki