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 16:18] – [How do I use virtual methods to create polymorphic objects?] reggieaslfaqs [2025/09/23 17:33] (current) – [How do negative numbers work?] reggie
Line 365: Line 365:
     decb                ; loop count     decb                ; loop count
     bne sprEraseLoop     bne sprEraseLoop
-    rts +    rts 
-    +
 ==== How do I use local variables? ==== ==== How do I use local variables? ====
  
Line 538: 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 544: Line 543:
     ldx #$0400    ; initialise cursor position     ldx #$0400    ; initialise cursor position
     ldd #$4849    ; two character message     ldd #$4849    ; two character message
-    std ,x        print it +    std ,x        print it 
     stx CURSPOS   ; save cursor position     stx CURSPOS   ; save cursor position
      
     ldb [CURSPOS] ; what character is at the cursor position?     ldb [CURSPOS] ; what character is at the cursor position?
     rts     rts
- 
-As well as direct extended mode, indirection can be used on PC relative and indexed addressing modes too. But note that because we are always specifying a 16-bit address, auto-increment/decrement by just one (rather than two) can not be used.    
  
 As well as direct extended mode, indirection can be used on PC relative and indexed addressing modes too. But note that because we are always specifying a 16-bit addresss, auto-increment/decrement by just one (rather than two) cannot be used.    As well as direct extended mode, indirection can be used on PC relative and indexed addressing modes too. But note that because we are always specifying a 16-bit addresss, auto-increment/decrement by just one (rather than two) cannot be used.   
Line 569: Line 566:
     clr ,-s             ; reserve and zero value of longest strng so far     clr ,-s             ; reserve and zero value of longest strng so far
   loop:   loop:
-    lda [,x++]   +    lda [,x++]          ; read EA from X, then inc X by 2, then load A from EA
     cmpa ,s             ; is A higher than longest?     cmpa ,s             ; is A higher than longest?
     bls short           ; skip if not     bls short           ; skip if not
Line 610: Line 607:
 Suppose we have a sprite library that deals with simple movement and animation, and want to expand it to include features such as collision detection, keeping in bounds, and more. To our sprite object we can add a field SPRMETHODS to point to a table of subroutines. Thus every sprite instance can have its own dynamic type specification. Suppose we have a sprite library that deals with simple movement and animation, and want to expand it to include features such as collision detection, keeping in bounds, and more. To our sprite object we can add a field SPRMETHODS to point to a table of subroutines. Thus every sprite instance can have its own dynamic type specification.
  
 +We can create a heirarchical structure where one class inherits its methods from its parent. So for a game we might start with:
 +
 +Basic:    A movable image with just a single frame that interacts with nothing.\\
 +--Animated: As Basic, but with a framelist displaying animated images\\
 +----Morpher:  As Animated, but morphing when a counter runs down, to turn into a bonus drop for example
 +
 +  ; Structure offsets
 +  
   sprStatus EQU 0   sprStatus EQU 0
   sprMask EQU 1   sprMask EQU 1
   sprXPos EQU 2   sprXPos EQU 2
   sprYPos EQU 4   sprYPos EQU 4
-  sprFList EQU 6+  sprGraphic EQU 6
   sprMethods EQU 8   sprMethods EQU 8
 +  sprCounter EQU 10  
 +  sprMorphTo EQU 12
      
 +  ; Subroutine library
      
-   sprInitBasic rts+  sprInitBasic rts
   sprEraseBasic rts   sprEraseBasic rts
   sprDrawBasic rts   sprDrawBasic rts
   sprMoveBasic rts   sprMoveBasic rts
   sprDoneBasic rts   sprDoneBasic rts
-  sprDrawExplosion rts +  sprDrawAnimated rts 
-  sprDoneDropbonus rts +  sprDoneMorpher rts 
 +   
 +  ; Class definitions:  Basic - - > Animated - - > Morpher 
 +    
 +  sprMethodsBasic     fdb sprInitBasic, sprEraseBasic, sprDrawBasic, sprMoveBasic, sprDoneBasic 
 +  sprMethodsAnimated  fdb sprInitBasic, sprEraseBasic, sprDrawAnimated, sprMoveBasic, sprDoneBasic 
 +  sprMethodsMorpher   fdb sprInitBasic, sprEraseBasic, sprDrawAnimated, sprMoveBasic, sprDoneMorph 
 +   
 +  ; Instance templates 
 +   
 +  cloud fdb 0,0,0,bmCloud,0,sprMethodsBasic 
 +  explosion fdb 0,0,0,flExplosion,60,sprMethodsAnimated 
 +  bonus fdb 0,0,0,bmBonus,0,sprMethodsBasic 
 +  bonusDrop fdb 0,0,0,flExplosion,30,sprMethodsMorph, bonus 
 +   
 +Note that all the organisational work is down to the programmer; the assembler won't give any help keeping the structures in order. 
  
 ==== How do negative numbers work? ==== ==== How do negative numbers work? ====
 +
 +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.1758385134.txt.gz · Last modified: 2025/09/20 16:18 by reggie

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki