Relaunch64 icon indicating copy to clipboard operation
Relaunch64 copied to clipboard

Refactor - Extract Subroutine

Open oziphantom opened this issue 9 years ago • 2 comments

Yeah total pie in the sky request but

  • select code
  • right click, extract as subroutine
  • then it prompts you to name the subroutine
  • put a JSR where the code was
  • back up till before the current non local label ( not a 100% but easy enough for the programmer to find and move if needed.
  • inserts
  • inserts the code
  • inserts rts

oziphantom avatar Nov 24 '15 14:11 oziphantom

Could you post a code snippet that shows how the asm-code would look like before vs. after doing this? Including local labels etc., so I get a concrete picture of what you mean.

sjPlot avatar Nov 24 '15 15:11 sjPlot

So you start with this code

buildTimerFractions
    jsr cleartimerPreShiftsBase
    lda # <kVectors.timerPreShiftsBase
    sta pointer1
    lda # >kVectors.timerPreShiftsBase
    sta pointer1+1
    lda #0
    sta NormalTemp1
_loopMakeFirstDigit
    ldx # size(ByteBuffer)-1
    lda #0
-   sta ByteBuffer,x
    dex
    bpl -
_loopShiftDigit 
    ldx NormalTemp1 
    ldy #0
_copyLoop
    lda kFont.digitsPointer,x
    sta ByteBuffer,y
    inx
    iny
    iny
    cpy #10
    bcc _copyLoop
    ldy #3 ; num times to shift
    ldx #0
_rowLoop
    lsr ByteBuffer,x
    ror ByteBuffer+1,x
    inx 
    inx
    cpx #11
    bcc _rowLoop
    dey
    bpl shiftBuffer5RowsRight16Bit
    lda ByteBuffer+2    
    ora #%01100000 ; the :
    sta ByteBuffer+2
    lda ByteBuffer+6
    ora #%01100000
    sta ByteBuffer+6
    lda #4
    sta NormalTemp2

To which I decide that I need to do _loopMakeFirstDigit part again, it clears the buffer, so I pull it out into its own function like so

clearByteBuffer
    ldx # size(ByteBuffer)-1
    lda #0
-   sta ByteBuffer,x
    dex
    bpl -
    rts

So I would select the LDX line to the BPL line This then makes the above code become

buildTimerFractions
    jsr cleartimerPreShiftsBase
    lda # <kVectors.timerPreShiftsBase
    sta pointer1
    lda # >kVectors.timerPreShiftsBase
    sta pointer1+1
    lda #0
    sta NormalTemp1
_loopMakeFirstDigit
    jsr clearByteBuffer
_loopShiftDigit 
    ldx NormalTemp1 
    ldy #0
_copyLoop
    lda kFont.digitsPointer,x
    sta ByteBuffer,y
    inx
    iny
    iny
    cpy #10
    bcc _copyLoop
    ldy #3 ; num times to shift
    ldx #0
_rowLoop
    lsr ByteBuffer,x
    ror ByteBuffer+1,x
    inx 
    inx
    cpx #11
    bcc _rowLoop
    dey
    bpl shiftBuffer5RowsRight16Bit
    lda ByteBuffer+2    
    ora #%01100000 ; the :
    sta ByteBuffer+2
    lda ByteBuffer+6
    ora #%01100000
    sta ByteBuffer+6
    lda #4
    sta NormalTemp2

Then the next bit of code copies the char I want, which I need to do again elsewhere so I pull it out into a function, which will get inserted after the clearByteBuffer function like so

clearByteBuffer
    ldx # size(ByteBuffer)-1
    lda #0
-   sta ByteBuffer,x
    dex
    bpl -
    rts

copyCharTOBuffer
    ldy #0
_copyLoop
    lda kFont.digitsPointer,x
    sta ByteBuffer,y
    inx
    iny
    iny
    cpy #10
    bcc _copyLoop
    rts

buildTimerFractions
    jsr cleartimerPreShiftsBase
    lda # <kVectors.timerPreShiftsBase
    sta pointer1
    lda # >kVectors.timerPreShiftsBase
    sta pointer1+1
    lda #0
    sta NormalTemp1
_loopMakeFirstDigit
    jsr clearByteBuffer
_loopShiftDigit 
    ldx NormalTemp1 
    jsr copyCharTOBuffer
    ldy #3 ; num times to shift
    ldx #0
_rowLoop
    lsr ByteBuffer,x
    ror ByteBuffer+1,x
    inx 
    inx
    cpx #11
    bcc _rowLoop
    dey
    bpl shiftBuffer5RowsRight16Bit
    lda ByteBuffer+2    
    ora #%01100000 ; the :
    sta ByteBuffer+2
    lda ByteBuffer+6
    ora #%01100000
    sta ByteBuffer+6
    lda #4
    sta NormalTemp2

oziphantom avatar Nov 25 '15 08:11 oziphantom