Advertisement
paul_nicholls

Untitled

Nov 23rd, 2024
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. :BasicUpstart2(_start_)
  2.  
  3. * = $080d "main code"
  4.  
  5. _start_:
  6.   jmp _main_
  7.  
  8. .var _bp_    = $fb
  9. .var _temp_  = $fc
  10.  
  11. .function _getMSB(arg) {
  12.   .if (arg.getType()==AT_IMMEDIATE)
  13.     .return CmdArgument(arg.getType(),>arg.getValue())
  14.  
  15.   .return CmdArgument(arg.getType(),arg.getValue()+1)
  16. }
  17.  
  18. .pseudocommand push8 _value_ {
  19.   lda _value_
  20.   pha
  21. }
  22.  
  23. .pseudocommand push16 _value_ {
  24.   // push msb first, then lsb
  25.   lda _getMSB(_value_)
  26.   pha
  27.   lda _value_
  28.   pha
  29. }
  30.  
  31. .pseudocommand pop8 _variable_ {
  32.   pla
  33.   sta _variable_
  34. }
  35.  
  36. .pseudocommand pop16 _variable_ {
  37.   // pop lsb first, then msb
  38.   pla
  39.   sta _variable_
  40.   pla
  41.   sta _getMSB(_variable_)
  42. }
  43.  
  44. .pseudocommand pushax {
  45.   // a = msb
  46.   // x = lsb
  47.   pha
  48.   txa
  49.   pha
  50. }
  51.  
  52. .pseudocommand popax {
  53.   // a = msb
  54.   // x = lsb
  55.   pla
  56.   tax
  57.   pla
  58. }
  59.  
  60. .pseudocommand popa {
  61.   pla
  62. }
  63.  
  64. .macro expandStack(_size_) {
  65.   tsx
  66.   txa
  67.  
  68.   sec
  69.   sbc #_size_
  70.  
  71.   tax
  72.   txs
  73. }
  74.  
  75. .macro shrinkStack(_size_) {
  76.   tsx
  77.   txa
  78.  
  79.   clc
  80.   adc #_size_
  81.  
  82.   tax
  83.   txs
  84. }
  85.  
  86. .macro enter(_locals_size) {
  87.   // save call frame index
  88.   push8 _bp_
  89.   tsx
  90.   txa
  91.  
  92.   // make stack pointer look at last pushed value (bp), not next free place
  93.   inx
  94.   // copy stack pointer into call frame index
  95.   stx _bp_
  96.  
  97.   // add local vars space to stack
  98.   :expandStack(_locals_size)
  99. }
  100.  
  101. .macro leave(_locals_size,_params_size_) {
  102.   // remove local vars space from stack
  103.   :shrinkStack(_locals_size)
  104.  
  105.   // restore call frame index
  106.   pop8 _bp_
  107.  
  108.   // save return address
  109.   pop16 _temp_
  110.  
  111.   // remove params of the stack
  112.  :shrinkStack(_params_size_)
  113.  
  114.   // restore return address
  115.   push16 _temp_
  116. }
  117.  
  118. sum: {
  119. .var RESULT = $0100 + 5
  120. .var PARAM1 = $0100 + 3
  121. .var LOCAL1 = $0100 + -1
  122.  
  123.   // function sum(a,b) : Byte;
  124.   :enter(1) // total local var size (use 0 for no local variables)
  125.  
  126.   ldx _bp_
  127.   clc
  128.   lda PARAM1+1,x // read param b
  129.   adc PARAM1  ,x // read param a
  130.   sta RESULT,x   // store result
  131.   sta LOCAL1,x   // store in local var 1 as test
  132.  
  133.   :leave(1,2) // total local var size, total param size
  134.  
  135.   // result is left on stack after rts
  136.   rts
  137. }
  138.  
  139. _main_:
  140. // Result := sum(sum(1,2),1);
  141.  
  142.   // params go on stack in reverse order
  143.   push8 #$00 // space for func result
  144.   push8 #$01 // param b
  145.  
  146.     // func result as a param of other function
  147.     push8 #$00 // space for func result
  148.     push8 #$02 // param b
  149.     push8 #$01 // param a
  150.    
  151.     jsr sum
  152.     // func result is already on stack as param a
  153.  
  154.   jsr sum
  155.  
  156.   pop8 $0400 // displays 'd' (4)
  157.  
  158.   rts
  159.  
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement