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
Next revision
Previous revision
aslfaqs [2025/09/20 17:31] – [What is indirection and how do I use it?] reggieaslfaqs [2025/09/23 17:33] (current) – [How do negative numbers work?] reggie
Line 537: Line 537:
 ==== What is indirection and how do I use it? ==== ==== What is indirection and how do I use it? ====
  
-The standard way of accessing a variable in memory is via **direct extended** addressing. A 16-bit address is given, and an 8 or 16 bit operand is read from this 'effective address (EA)'. Indirect addressing adds an extra step: the 16-bits at the initial EA are read and used as the true EA of the operand.+The standard way of accessing a variable in memory is via **direct extended** addressing. A 16-bit address is given, and an 8 or 16 bit operand is read from this 'effective address (EA)'. Indirect addressing adds an extra step: the 16-bits at the initial EA are read and used as the true EA of the operand. Put square brackets around the memory expression to use indirection (some assemblers allow the more usual curved brackets).
  
   CURSPOS rmb 2   CURSPOS rmb 2
Line 651: Line 651:
 ==== How do negative numbers work? ==== ==== How do negative numbers work? ====
  
-[Fill in later]+Think of an old-fashioned clock face as an analogy with an 8-bit register. An hour has 60 minutes, marked as 0 to 59. The minute hand goes only one way, and when it come round to zero the hour hand advances. 
 + 
 +An 8-bit register has 256 values, marked as 0 to 255, and when it overflows round to zero the carry flag is set.    
 + 
 +How do negative numbers work on a clock? It's entirely down to how we choose to read them. We can say "to" the hour or "past" the hour. The mechanism of the clock doesn't have to change at all. Ten to eight in the morning is 07:50. So "minus ten" in clockface terms is calculated by "60-10". To turn the clock back another 10 minutes, we can instead turn it forward 50 to get the correct answer of 20 to the hour. 
 + 
 +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, 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. 
 + 
 +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 
 +   
 +       BPL             PLus                     
 +       BMI             MInus 
 +       BLT             Less-Than                BLO 
 +       BLE             Less-than or Equal       BLS 
 +       BGT             Greater-Than             BHI 
 +       BGE             Greater-than or Equal    BHS 
 +        
 +  Miscellaneous instructions 
 +     
 +  NEG (8-bit only)     NEGate                   COM 
 +  SEX                  Sign EXtend B to 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. 
 + 
 +  
 + 
 ==== What do all the instructions do? ==== ==== What do all the instructions do? ====
  
 [Will get round to this one] [Will get round to this one]
aslfaqs.1758389461.txt.gz · Last modified: 2025/09/20 17:31 by reggie

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki