Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :BasicUpstart2(_start_)
- * = $080d "main code"
- _start_:
- jmp _main_
- .var _bp_ = $fb
- .var _temp_ = $fc
- .function _getMSB(arg) {
- .if (arg.getType()==AT_IMMEDIATE)
- .return CmdArgument(arg.getType(),>arg.getValue())
- .return CmdArgument(arg.getType(),arg.getValue()+1)
- }
- .pseudocommand push8 _value_ {
- lda _value_
- pha
- }
- .pseudocommand push16 _value_ {
- // push msb first, then lsb
- lda _getMSB(_value_)
- pha
- lda _value_
- pha
- }
- .pseudocommand pop8 _variable_ {
- pla
- sta _variable_
- }
- .pseudocommand pop16 _variable_ {
- // pop lsb first, then msb
- pla
- sta _variable_
- pla
- sta _getMSB(_variable_)
- }
- .pseudocommand pushax {
- // a = msb
- // x = lsb
- pha
- txa
- pha
- }
- .pseudocommand popax {
- // a = msb
- // x = lsb
- pla
- tax
- pla
- }
- .pseudocommand popa {
- pla
- }
- .macro expandStack(_size_) {
- tsx
- txa
- sec
- sbc #_size_
- tax
- txs
- }
- .macro shrinkStack(_size_) {
- tsx
- txa
- clc
- adc #_size_
- tax
- txs
- }
- .macro enter(_locals_size) {
- // save call frame index
- push8 _bp_
- tsx
- txa
- // make stack pointer look at last pushed value (bp), not next free place
- inx
- // copy stack pointer into call frame index
- stx _bp_
- // add local vars space to stack
- :expandStack(_locals_size)
- }
- .macro leave(_locals_size,_params_size_) {
- // remove local vars space from stack
- :shrinkStack(_locals_size)
- // restore call frame index
- pop8 _bp_
- // save return address
- pop16 _temp_
- // remove params of the stack
- :shrinkStack(_params_size_)
- // restore return address
- push16 _temp_
- }
- sum: {
- .var RESULT = $0100 + 5
- .var PARAM1 = $0100 + 3
- .var LOCAL1 = $0100 + -1
- // function sum(a,b) : Byte;
- :enter(1) // total local var size (use 0 for no local variables)
- ldx _bp_
- clc
- lda PARAM1+1,x // read param b
- adc PARAM1 ,x // read param a
- sta RESULT,x // store result
- sta LOCAL1,x // store in local var 1 as test
- :leave(1,2) // total local var size, total param size
- // result is left on stack after rts
- rts
- }
- _main_:
- // Result := sum(sum(1,2),1);
- // params go on stack in reverse order
- push8 #$00 // space for func result
- push8 #$01 // param b
- // func result as a param of other function
- push8 #$00 // space for func result
- push8 #$02 // param b
- push8 #$01 // param a
- jsr sum
- // func result is already on stack as param a
- jsr sum
- pop8 $0400 // displays 'd' (4)
- rts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement