  // Commodore 64 PRG executable file
.file [name="sqr-delta.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(main)
  .label SCREEN = $400
.segment Code
main: {
    .label __0 = $a
    // ifunc(8)
    jsr ifunc
    // ifunc(8)
    // SCREEN[0] = ifunc(8)
    lda.z __0
    sta SCREEN
    lda.z __0+1
    sta SCREEN+1
    lda.z __0+2
    sta SCREEN+2
    lda.z __0+3
    sta SCREEN+3
    // }
    rts
}
// __zp($a) unsigned long ifunc(unsigned long a)
ifunc: {
    .const a = 8
    .label x = $a
    .label xsqr = 6
    .label delta = 2
    .label return = $a
    lda #<3
    sta.z delta
    lda #>3
    sta.z delta+1
    lda #<3>>$10
    sta.z delta+2
    lda #>3>>$10
    sta.z delta+3
    lda #<1
    sta.z x
    lda #>1
    sta.z x+1
    lda #<1>>$10
    sta.z x+2
    lda #>1>>$10
    sta.z x+3
    lda #<1
    sta.z xsqr
    lda #>1
    sta.z xsqr+1
    lda #<1>>$10
    sta.z xsqr+2
    lda #>1>>$10
    sta.z xsqr+3
  __b1:
    // while(xsqr <=a)
    lda.z xsqr+3
    cmp #>a+1>>$10
    bcc __b2
    bne !+
    lda.z xsqr+2
    cmp #<a+1>>$10
    bcc __b2
    bne !+
    lda.z xsqr+1
    cmp #>a+1
    bcc __b2
    bne !+
    lda.z xsqr
    cmp #<a+1
    bcc __b2
  !:
    // return --x;
    lda.z return
    sec
    sbc #1
    sta.z return
    lda.z return+1
    sbc #0
    sta.z return+1
    lda.z return+2
    sbc #0
    sta.z return+2
    lda.z return+3
    sbc #0
    sta.z return+3
    // }
    rts
  __b2:
    // ++x;
    inc.z x
    bne !+
    inc.z x+1
    bne !+
    inc.z x+2
    bne !+
    inc.z x+3
  !:
    // xsqr += delta
    clc
    lda.z xsqr
    adc.z delta
    sta.z xsqr
    lda.z xsqr+1
    adc.z delta+1
    sta.z xsqr+1
    lda.z xsqr+2
    adc.z delta+2
    sta.z xsqr+2
    lda.z xsqr+3
    adc.z delta+3
    sta.z xsqr+3
    // delta += 2
    lda.z delta
    clc
    adc #2
    sta.z delta
    bcc !+
    inc.z delta+1
    bne !+
    inc.z delta+2
    bne !+
    inc.z delta+3
  !:
    jmp __b1
}
