Skip to content

Instantly share code, notes, and snippets.

@b-adams
Created July 29, 2012 13:10
Show Gist options
  • Save b-adams/3198693 to your computer and use it in GitHub Desktop.
Save b-adams/3198693 to your computer and use it in GitHub Desktop.
Example Pep/8 Programs
;Trying to use a global variable to allow function arguments
BR main
usrInput: .BLOCK 2 ;global variable #2d
showChar: NOP0
charo '\n',i
deco usrInput,d
RET0
showChP: NOP0
charo '\n',i
lda usrInput,d
adda 128,i
sta usrInput,d
deco usrInput,d
RET0
main: NOP0
charo '\n',i
charo '\n',i
deci usrInput,d
;show number, augmented number, original number, augmented number
CALL showChar
CALL showChP
CALL showChar
CALL showChP
;Doesn't work because ChP mungs the global variable
BR main
STOP
.end
BR main
x: .equate 0 ;local variable #2d
y: .equate 2 ;local variable #2d
frame: .equate 4
main: NOP0
subsp frame,i ;allocate #y #x
MOVSPA
adda y,i
STA place,d
addsp frame,i ;deallocate #x #y
STOP
place: .block 2 ;global variable #2h
.end
BR main
x: .equate 0 ;local variable #2d
y: .equate 2 ;local variable #2d
frame: .equate 4
main: NOP0
subsp frame,i ;allocate #y #x
MOVSPA
adda y,i
STA place,d
lda 3,i ;load 3 to accumulator
sta y,s ;store 3 into y
;try to access the y location without mentioning y
deco place,i
charo '\n',i
deco place,d
charo '\n',i
deco place,n
charo '\n',i
deco place,s
charo '\n',i
deco place,sf
charo '\n',i
deco place,x
charo '\n',i
deco place,sx
charo '\n',i
deco place,sxf
charo '\n',i
;end attempt to access the y location without mentioning y
addsp frame,i ;deallocate #x #y
STOP
place: .block 2 ;global variable #2h
.end
BR main
x: .equate 0 ;local variable #2d
y: .equate 2 ;local variable #2d
locplace: .equate 4 ;local variable #2h
frame: .equate 6
main: NOP0
subsp frame,i ;allocate #locplace #y #x
MOVSPA
adda y,i
STA place,d
STA locplace,s
lda 3,i ;load 3 to accumulator
sta y,s ;store 3 into y
;try to access the y location without mentioning y - via global place
stro msgGLO,d
stro msgTGT,d
deco y,s
stro lbli,d
deco place,i
stro lbld,d
deco place,d
stro lbln,d
deco place,n
stro lbls,d
deco place,s
stro lblsf,d
deco place,sf
stro lblx,d
deco place,x
stro lblsx,d
deco place,sx
stro lblsxf,d
deco place,sxf
;end attempt to access the y location without mentioning y
;try to access the y location without mentioning y - via local place
stro msgLOC,d
stro msgTGT,d
deco y,s
stro lbli,d
deco locplace,i
stro lbld,d
deco locplace,d
stro lbln,d
deco locplace,n
stro lbls,d
deco locplace,s
stro lblsf,d
deco locplace,sf
stro lblx,d
deco locplace,x
stro lblsx,d
deco locplace,sx
stro lblsxf,d
deco locplace,sxf
;end attempt to access the y location without mentioning y
addsp frame,i ;deallocate #x #y #locplace
STOP
place: .block 2 ;global variable #2h
lbli: .ascii "\ni -Immediate \t\x00"
lbld: .ascii "\nd -Direct \t\x00"
lbln: .ascii "\nn -iNdirect \t\x00"
lbls: .ascii "\ns -Stack relative \t\x00"
lblsf: .ascii "\nsf -Stack relative deFerred\t\x00"
lblx: .ascii "\nx -indeXed \t\x00"
lblsx: .ascii "\nsx -Stack-indeXed \t\x00"
lblsxf: .ascii "\nsxf-Stack-indexed deFerred \t\x00"
msgLOC: .ascii "\n\nValue of SP+offset is in a LOCAL variable\n\x00"
msgGLO: .ascii "\n\nValue of SP+offset is in a GLOBAL variable\n\x00"
msgTGT: .ascii "\tLook for the value :\x00"
.end
BR main
x: .equate 0 ;local variable #2d
y: .equate 2 ;local variable #2d
frame: .equate 4
main: NOP0
subsp frame,i ;allocate #y #x
MOVSPA
adda y,i
STA place,d
lda 3,i ;load 3 to accumulator
sta y,s ;store 3 into y
;try to access the y location without mentioning y
deco place,i
charo '\n',i
deco place,d
charo '\n',i
deco place,n
charo '\n',i
deco place,s
charo '\n',i
deco place,sf
charo '\n',i
deco place,x
charo '\n',i
deco place,sx
charo '\n',i
deco place,sxf
charo '\n',i
;end attempt to access the y location without mentioning y
addsp frame,i ;deallocate #x #y
STOP
place: .block 2 ;global variable #2h
.end
;Example of pass-by-reference
BR main
clear: NOP0
;will look like
;void clear(int& target)
;local variables: none
CLocals: .equate 0 ;size of frame for local variables
cArg: .equate 2 ;formal parameter #2h
CPasses: .equate 2 ;size of return/argument frame
CArg: .equate -2 ;caller's view of clear's argument
subsp CLocals,i ;allocate (nothing)
;To set the thing pointed to by the argument to zero
deco cArg,sf ;output value pointed to by argument
deci cArg,sf ;input to spot pointed at by argument
lda 0,i ;put 0 in accumulator
sta cArg,sf ;s because it's on the stack, f because it's a pointer
addsp CLocals,i ;deallocate (nothing)
RET0
main: NOP0
valu: .equate 0 ;local variable #2d
mainFram: .equate 2 ;size of main's local stack
subsp mainFram,i ;allocate #valu
lda 17,i ;store something non-zero...
sta valu,s ;...into the test variable
;;;;;to call clear function (by reference)
;get memory location of valu
movspa
adda valu,i ;accumulator now has memory address of valu variable
;place it as argument for clear function
sta CArg,s ;put it where the function expects it to be
;call clear function
subsp CPasses,i ;push #cArg
call clear
addsp CPasses,i ;pop #cArg
;see if it worked
deco valu,s ;show if it worked - should be 0
addsp mainFram,i ;deallocate #valu
STOP
.end
BR main
x: .equate 0 ;local variable #2d
y: .equate 2 ;local variable #2d
frame: .equate 4
main: NOP0
subsp frame,i ;allocate #y #x
MOVSPA
adda y,i
STA place,d
lda 3,i ;load 3 to accumulator
sta y,s ;store 3 into y
;try to access the y location without mentioning y
deco place,i
charo '\n',i
deco place,d
charo '\n',i
deco place,n
charo '\n',i
deco place,s
charo '\n',i
deco place,sf
charo '\n',i
deco place,x
charo '\n',i
deco place,sx
charo '\n',i
deco place,sxf
charo '\n',i
;end attempt to access the y location without mentioning y
addsp frame,i ;deallocate #x #y
STOP
place: .block 2 ;global variable #2h
.end
BR main
;make a global variable for an array with 4 cells, 2 hex-formatted bytes each
;name of variable is snglArry
snglArry: .block 8 ;global variable #2h4a
main: NOP0
;get batch input to array
;just get the first thing into snglArray[0]
deci snglArry,d
;get the next thing into snglArray[1]
;which is (2) bytes past the start of the array
ldx 2,i
deci snglArry,x
;get the next thing into snglArray[2]
;;which is 2*2 bytes past the start of the array
ldx 2,i ;two
aslx ;times two
deci snglArry,x
locArry: .equate 0 ;local variable #2h4a
framsiz: .equate 8 ;size of local frame
subsp framsiz,i ;allocate #locArry
ldx 3,i ;to access locArry[3]
aslx ;double bytes to size of integer
deci locArry,sx;
addsp framsiz,i ;deallocate #locArry
STOP
.end
br main
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;main
theArray: .block 9 ;global variable #1c9a
cntr: .block 2 ;global variable #2d
othArray: .equate 0 ;local variable #1c9a
othSize: .equate 9
main: nop0
subsp othSize,i ;allocate #othArray
ldx 0,i ;initialize counter
stx cntr,d
loop: nop0
ldx cntr,d ;load value of coutner
nop0 ;convert from index to offset
chari theArray,x
chari othArray,sx
ldx cntr,d ;cntr++
addx 1,i
stx cntr,d
CPX othSize,i
BRLT loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;NEW STUFF: pass array by reference
;get address of array
;;;either theArray
;;;or movspa and offset by othArray
;for a local
movspa ;get the value of the stack pointer
adda othArray,i ;add offset of other array from SP
;put it where function expects arguments
sta fArg,s ;put address where function expects it
subsp fpasses,i ;push #fParam
call mloop
addsp fpasses,i ;pop #fParam
;;done for local
addsp othSize,i ;deallocate #othArray
STOP
mloop: nop0
fArg: .equate -2 ;address of array from caller's perspective
fpasses: .equate 2 ;dummy value for size of args+ret value
fParam: .equate 4 ;dummy value for formal parameter #2h showing start of array
loopC: .equate 0 ;local variable #2d
fLocals: .equate 2 ;size of local variables
subsp fLocals,i ;allocate #loopC
lda 0,i ;initialize loop counter
sta loopC,s
theLoop: nop0;in a loop
ldx loopC,s ;get index
nop0 ;convert from index to offset
ldbytea fParam,sxf ;get a letter to accumulator based on offset
ANDA 0xdf,i ;mask with AND 1101 1111 to clear case bit AKA 0xDF
stbytea fParam,sxf ;store letter back
lda loopC,s ;update loop counter
adda 1,i
sta loopC,s
CPA othSize,i ;test if loop over
BRLT theLoop,i ;repeat loop if appropriate
;;done the loop
addsp fLocals,i ;allocate #loopC
ret0
.end
br main
bar: .equate 15
foo: .equate bar
.addrss this
.addrss that
.addrss other
main: nop0
this: nop0
that: nop0
other: nop0
end: stop
.end
br main
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;main
theArray: .block 9 ;global variable #1c9a
cntr: .block 2 ;global variable #2d
othArray: .equate 0 ;local variable #1c9a
othSize: .equate 9
main: nop0
subsp othSize,i ;allocate #othArray
ldx 0,i ;initialize counter
stx cntr,d
loop: nop0
ldx cntr,d ;load value of coutner
nop0 ;convert from index to offset
chari theArray,x
chari othArray,sx
ldx cntr,d ;cntr++
addx 1,i
stx cntr,d
CPX othSize,i
BRLT loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;NEW STUFF: pass array by reference
;get address of array
;;;either theArray
;;;or movspa and offset by othArray
;for a local
movspa ;get the value of the stack pointer
adda othArray,i ;add offset of other array from SP
;put it where function expects arguments
sta fArg,s ;put address where function expects it
subsp fpasses,i ;push #fParam
call mloop
addsp fpasses,i ;pop #fParam
;;done for local
;for a global
lda theArray,i ;get address of theArray into accumulator
;put it where function expects arguments
sta fArg,s ;put address where function expects it
subsp fpasses,i ;push #fParam
call mloop
addsp fpasses,i ;pop #fParam
;;done for global
addsp othSize,i ;deallocate #othArray
STOP
mloop: nop0
fArg: .equate -2 ;address of array from caller's perspective
fpasses: .equate 2 ;dummy value for size of args+ret value
fParam: .equate 4 ;dummy value for formal parameter #2h showing start of array
loopC: .equate 0 ;local variable #2d
fLocals: .equate 2 ;size of local variables
subsp fLocals,i ;allocate #loopC
lda 0,i ;initialize loop counter
sta loopC,s
theLoop: nop0;in a loop
ldx loopC,s ;get index
nop0 ;convert from index to offset
ldbytea fParam,sxf ;get a letter to accumulator based on offset
ANDA 0xdf,i ;mask with AND 1101 1111 to clear case bit AKA 0xDF
stbytea fParam,sxf ;store letter back
lda loopC,s ;update loop counter
adda 1,i
sta loopC,s
CPA othSize,i ;test if loop over
BRLT theLoop,i ;repeat loop if appropriate
;;done the loop
addsp fLocals,i ;allocate #loopC
ret0
.end
br main
;Program to get input (1,2,3) from user, and display
;the ordinal word for that number
;;Next time: Add in a default case so an input of 0 doesn't make things wacky
input: .block 2 ;global variable #2d
main: NOP0
deci input,d ;get input
ldx input,d ;prepare for a switch-style jump
subx 1,i ;convert from 1,2,3 index to 0,1,2 index
aslx ;convert from index to offset
br loctns,x ;continue execution at the address listed in loctns[X]
first: stro fmsg,d ;output first, but don't stop just yet
second: stro smsg,d ;output second, and...
br donecase,i ;...stop
third: stro tmsg,d ;output third, and...
br donecase,i ;...stop
donecase: nop0 ;done with the switch statement
charo '\n',i ;prettify output
br main,i ;no STOP, this program loops infinitely
;;;;Data section
;Strings
fmsg: .ascii "First\x00"
smsg: .ascii "Second\x00"
tmsg: .ascii "Third\x00"
;Jump table - holds addresses of blocks of code to execute
loctns: .addrss first
.addrss second
.addrss third
.end
br main
;Program to get input (1,2,3) from user, and display
;the ordinal word for that number
;;Done today: Add in a default case so an input of 0 doesn't make things wacky
;;But not in a flexible manner
input: .block 2 ;global variable #2d
main: NOP0
deci input,d ;get input
;need to add logic for jumping to the right place or default
ldx input,d ;prepare for a switch-style jump
cpx 1,i ;if below valid input range
brlt default,i
cpx 3,i ;if above valid input range
brgt default,i
;detection should be before input->index conversion, since restriction is conceptually on inputs
subx 1,i ;convert from 1,2,3 index to 0,1,2 index
;detection must be before the conversion, to talk about acceptable inputs
aslx ;convert from index to offset
;detection must be done before here or else we've already jumped
br loctns,x ;continue execution at the address listed in loctns[X]
first: stro fmsg,d ;output first, but don't stop just yet
second: stro smsg,d ;output second, and...
br donecase,i ;...stop
third: stro tmsg,d ;output third, and...
br donecase,i ;...stop
;;need to add code for doing the default thing
default: stro dmsg,d ;output fail, and...
br donecase,i ;...stop
donecase: nop0 ;done with the switch statement
charo '\n',i ;prettify output
br main,i ;no STOP, this program loops infinitely
;;;;Data section
;Strings
fmsg: .ascii "First\x00"
smsg: .ascii "Second\x00"
tmsg: .ascii "Third\x00"
dmsg: .ascii "FAIL\x00"
;Jump table - holds addresses of blocks of code to execute
loctns: .addrss first
.addrss second
.addrss third
;may need to track where the default option is
;today: we'll just use the direct label to default
.end
br main
;Program to get input (1,2,3) from user, and display
;the ordinal word for that number
;;Done today: Add in a default case so an input of 0 doesn't make things wacky
;;AND have that be part of the jump table
input: .block 2 ;global variable #2d
main: NOP0
deci input,d ;get input
;need to add logic for jumping to the right place or default
ldx input,d ;prepare for a switch-style jump
cpx 1,i ;if below valid input range
brlt makedef,i
cpx 3,i ;if above valid input range
brgt makedef,i
br swmain,i
makedef: nop0
ldx swdeflt,i ;load location of default to index register, overriding loaded user input
;detection should be before input->index conversion, since restriction is conceptually on inputs
swmain: nop0
aslx ;convert from index to offset
;detection must be done before here or else we've already jumped
br loctns,x ;continue execution at the address listed in loctns[X]
first: stro fmsg,d ;output first, but don't stop just yet
second: stro smsg,d ;output second, and...
br donecase,i ;...stop
third: stro tmsg,d ;output third, and...
br donecase,i ;...stop
;;need to add code for doing the default thing
default: stro dmsg,d ;output fail, and...
br donecase,i ;...stop
donecase: nop0 ;done with the switch statement
charo '\n',i ;prettify output
br main,i ;no STOP, this program loops infinitely
;;;;Data section
;Strings
fmsg: .ascii "First\x00"
smsg: .ascii "Second\x00"
tmsg: .ascii "Third\x00"
dmsg: .ascii "FAIL\x00"
;Jump table - holds addresses of blocks of code to execute
swdeflt: .equate 0 ;offset of default for this switch
loctns:.addrss default
.addrss first
.addrss second
.addrss third
;may need to track where the default option is
;today: we'll just use the direct label to default
.end
BR main
L01: .block 2
L02: .equate 0
L03: .equate 2
L04: .equate 3
main: NOP0
subsp L04,i
deci L01,d
movspa
sta L02,s
ldbytea 'W',i
stbytea L03,s
stbytea L14,d
lda L01,d
sta L10,s
subsp L12,i
call sub,i
addsp L12,i
lda L11,s
sta L01,d
deco L01,d
addsp L04,i
STOP
L05: .equate 0
L06: .equate 2
L07: .equate 3
L08: .equate 5
L09: .equate 7
L10: .equate -4
L11: .equate -2
L12: .equate 4
sub: NOP0
subsp L07,i
ldbytea L14,d
stbytea L06,s
lda L08,s
asla
sta L05,s
deco L05,s
lda L05,s
asla
sta L09,s
addsp L07,i
RET0
L13: .addrss L01
.addrss L02
.addrss L03
.addrss L04
L14: .block 1
.end
BR main
L01: .block 2 ;global variable #2d
L02: .equate 0 ;local (main) variable #2h (for stack pointer)
L03: .equate 2 ;local (main) variable #1c
L04: .equate 3 ;probably size of local(main) frame
main: NOP0
subsp L04,i ;allocate #L03 #L02
deci L01,d
movspa ;puts address in accumulator
sta L02,s ;puts accumulator in L02 on stack
ldbytea 'W',i
stbytea L03,s
stbytea L14,d
lda L01,d
sta L10,s
subsp L12,i ;push #L09 #L08
call sub,i
addsp L12,i ;pop #L08 #L09
lda L11,s
sta L01,d
deco L01,d
addsp L04,i ;deallocate #L02 #L03
STOP
L05: .equate 0 ;local(sub) variable #2d
L06: .equate 2 ;local(sub) variable #1c
L07: .equate 3 ;size of local(sub) frame
L08: .equate 5 ;formal parameter(sub) #2d
L09: .equate 7 ;return value #2d
L10: .equate -4 ;caller's view of L08 (argument to sub)
L11: .equate -2 ;caller's view of L09 (return value from sub)
L12: .equate 4 ;size of pass/return frame for sub
sub: NOP0
subsp L07,i ;allocate #L06 #L05
ldbytea L14,d
stbytea L06,s
lda L08,s
asla
sta L05,s
deco L05,s
lda L05,s
asla
sta L09,s
addsp L07,i ;deallocate #L05 #L06
RET0
L13: .addrss L01 ;array tag? #2h4a
.addrss L02
.addrss L03
.addrss L04
L14: .block 1 ;global variable #1c
.end
br input ;Jump over data, and some code
theNum: .block 2 ;global variable #2d
justABit: .equate 0x0004 ;Hex encoding of a certain binary number
juggle: NOP0 ;disrupt later allegations
LDA 0,i ;clear the accumulator
ldbytea DNright,d
suba justABit,i
stbytea DNleft,d
input: nop0 ;load user inputs
deci theNum,d
lda theNum,d
DoNada: NOP0 ;allegedly, these lines do nothing in the end
DNleft: ASLA
DNright: ASRA
output: NOP0 ;Save and output result of DoNada
STA theNum,d
DECO theNum,d
theEnd: STOP ;That's all, folks
.END
BR main
;make a global variable for an array with 4 cells, 2 hex-formatted bytes each
;name of variable is snglArry
snglArry: .block 8 ;global variable #2h4a
main: NOP0
;get batch input to array
;just get the first thing into snglArray[0]
deci snglArry,d
;get the next thing into snglArray[1]
;which is (2) bytes past the start of the array
ldx 2,i
deci snglArry,x
;get the next thing into snglArray[2]
;;which is 2*2 bytes past the start of the array
ldx 2,i ;two
aslx ;times two
deci snglArry,x
locArry: .equate 0 ;local variable #2h4a
framsiz: .equate 8 ;size of local frame
subsp framsiz,i ;allocate #locArry
ldx 3,i ;to access locArry[3]
aslx ;double bytes to size of integer
deci locArry,sx;
addsp framsiz,i ;deallocate #locArry
STOP
.end
;Compute Fibonacci numbers
BR main
fibo: NOP0
;fibo(which fibonacci number:int):int
;according to fibo(n)=fibo(n-1)+fibo(n-2) and fibo(1)=fibo(0)=1
;C code:
;int fibo(int arg){
; int result;
; int last;
; int lastlast;
; if(arg>1)
; {
; last=fibo(arg-1);
; lastlast=fibo(arg-2);
; }
; else result=1;
; return result; }
;Stack frame from fibo's perspective:
;local variable 'last' @ 0
;local variable 'lastlast' @ 2
;size of local frame: 4
;return address @ 4
;parameter @ 6
;return value @ 8
;size of passed parameter/return frame: 4
last: .EQUATE 0 ;local variable #2d
lastlast:.EQUATE 2 ;local variable #2d
fiboLocs:.EQUATE 4
param: .EQUATE 6 ;formal parameter #2d
retval: .EQUATE 8 ;return value #2d
fiboPass:.EQUATE 4
;Caller-visible frame from caller's perspective:
;argument @ -4
;returned value @ -2
fiboArg: .EQUATE -4
fiboRet: .EQUATE -2
SUBSP fiboLocs,i ;allocate #lastlast #last
;test if parameter > 1
LDA param,s
CPA 1,i
BRGT fiboIf
fiboElse:NOP0
LDA 1,i
STA retval,s
BR fiboFi ;exit IF
fiboIf: NOP0
LDA param,s
SUBA 1,i
STA fiboArg,s ;prepare n-1 as arg for fibo
SUBSP fiboPass,i ;push #retval #param
CALL fibo
ADDSP fiboPass,i ;pop #param #retval
LDA fiboRet,s
STA last,s ;last = fibo(n-1)
LDA param,s
SUBA 2,i
STA fiboArg,s ;prepare n-2 as arg for fibo
SUBSP fiboPass,i ;push #retval #param
CALL fibo
ADDSP fiboPass,i ;pop #param #retval
LDA fiboRet,s
STA lastlast,s ;lastlast = fibo(n-2)
LDA last,s
ADDA lastlast,s
STA retval,s ;prepare return value with fibo(n-1)+fibo(n-2)
BR fiboFi ;exit IF
fiboFi: NOP0
ADDSP fiboLocs,i ;deallocate #last #lastlast
RET0
main: NOP0
STRO prompt,d
DECI fiboArg,s
SUBSP fiboPass,i ;push #retval #param
CALL fibo
ADDSP fiboPass,i ;pop #param #retval
STRO tpmorp,d
DECO fiboRet,s
STOP
prompt: .ASCII "Which fibonacci number do you want?\n\x00"
tpmorp: .ASCII "That would be\n\x00"
.END
BR main
j: .block 2 ;global variable #2d
doneMsg: .ascii "All done!\x00"
errMsg: .ascii "This should not display\x00"
main: NOP0
bookmark: .equate 0 ;local variable #2h
Fmain: .equate 2 ;size of frame for main
subsp Fmain,i ;allocate #bookmark
lda 0,i ;clear accumulator
adda -1,i ;hacky loop adjustment
sta j,s ;initialize j
loop: NOP0
lda j,d ;load j
adda 1,i ;increment j
sta j,d ;store j
deco j,d ;display j
lda goHere,i ;load address of 'next line'
sta bookMark,s ;store address for later
BR showStuf
goHere: NOP0
lda j,d
adda 128,i ;make j bigger
sta j,d
lda goThere,i ;load address of 'next line'
sta bookMark,s ;store address for later
BR showStuf
goThere: NOP0
lda j,d
suba 128,i ;make j smaller
sta j,d
charo '\n',i ;output newline
lda j,d
cpa 128,i
BRLT loop ;If j<128 continue loop
addsp Fmain,i ;deallocate #bookmark
stro doneMsg,d
STOP
showStuf:NOP0
jchar: .equate 0 ;local variable #1c
Fshow: .equate 1 ;frame for showStuf
subsp Fshow,i ;allocate #jchar
lda j,d ;get j
stbytea jchar,s ;convert to 1-byte character
charo jchar,s ;display it
addsp Fshow,i ;deallocate #jchar
BR bookMark,s ;jump to bookmarked location, stored on stack
STRO errMsg,d ;we don't need a stop - this never happens
.end
BR main
j: .block 2 ;global variable #2d
doneMsg: .ascii "All done!\x00"
errMsg: .ascii "This should not display\x00"
main: NOP0
;OLD bookmark: .equate 0 ;local variable #2h
;OLD Fmain: .equate 2 ;size of frame for main
;OLD subsp Fmain,i ;allocate #bookmark
lda 0,i ;clear accumulator
adda -1,i ;hacky loop adjustment
sta j,s ;initialize j
loop: NOP0
lda j,d ;load j
adda 1,i ;increment j
sta j,d ;store j
deco j,d ;display j
;OLD lda goHere,i ;load address of 'next line'
;OLD sta bookMark,s ;store address for later
;OLD BR showStuf
;OLD goHere: NOP0
CALL showStuf ;NEW
lda j,d
adda 128,i ;make j bigger
sta j,d
;OLD lda goThere,i ;load address of 'next line'
;OLD sta bookMark,s ;store address for later
;OLD BR showStuf
;OLD goThere: NOP0
CALL showStuf ;NEW
lda j,d
suba 128,i ;make j smaller
sta j,d
charo '\n',i ;output newline
lda j,d
cpa 128,i
BRLT loop ;If j<128 continue loop
;OLD addsp Fmain,i ;deallocate #bookmark
stro doneMsg,d
STOP
showStuf:NOP0
jchar: .equate 0 ;local variable #1c
Fshow: .equate 1 ;frame for showStuf
subsp Fshow,i ;allocate #jchar
lda j,d ;get j
stbytea jchar,s ;convert to 1-byte character
charo jchar,s ;display it
addsp Fshow,i ;deallocate #jchar
;OLD BR bookMark,s ;jump to bookmarked location, stored on stack
RET0 ;don't deallocate anything extra, jump back to where we were
STRO errMsg,d ;we don't need a stop - this never happens
.end
stro doSTRO,d
lda noSTRO,i
sta lpcnt,d
tehLoop: ldx lpcnt,d ;not really new stuff
charo 0,x ;new stuff
addx 1,i ;not really new stuff
stx lpcnt,d ;not really new stuff
ldbytea 0,x ;new stuff
cpa '\x00',i
BRNE tehLoop,i
stro doSTRO,d
stop
noSTRO: .ascii "-A snake! A snake!-\n\x00"
doSTRO: .ascii "-Badger badger badger badger...-\n\x00"
wtf: .ascii "http://www.weebls-stuff.com/songs/badgers/\x00"
lpcnt: .block 2
.end
;Procedure Handout
;Procedure no params
;Nested calling of procedures. Do After Fig.6.18
;No parameters, no local variables, no return values.
;Just watch the stack as the procedures are called.
BR Main
Linefeed: .EQUATE 0x000A ; Global constant
;
;------void Name()
Name: NOP0
STRO Myname, d ; printf("%s\n", Myname)
CHARO Linefeed, i
RET0 ;Must return before the data storage
;
;------data for Name()
Myname: .ASCII "Slartibartfast\x00"
;"constant-like" String storage stays with procedure code, not on stack
;
;------void Nicename()
Nicename: NOP0
CALL Starz
CALL Name
CALL Starz
RET0 ; What happens if I leave this off?
;
;------void Starz()
Starz: NOP0
STRO Line1, d
CHARO Linefeed, i
STRO Line2, d
CHARO Linefeed, i
RET0
;
;------data for Starz()
Line1: .ASCII "* * * *\x00"
Line2: .ASCII " * * * \x00"
;
;------Main()
Main: NOP0
CALL Name
CHARO '?', i
CHARO Linefeed, i
CALL Nicename
STOP
.end
CHARO '!',i
CHARO ;syntax error due to lack of operand? Why not?
;Program A to see if number is even or odd
BR main ;
num: .BLOCK 2 ;
linef: .EQUATE 0x000A ;
;
main: DECI num,d
LDA num,d ;
DECO num,d ; output preliminary
CHARO ':',i
CHARO ' ',i ; space
;Calculate Condition If C = 1, number is odd, If C = 0, number is even.
ASRA ; shifts right, stores "1-bit" in C: AFFECTS C FLAG
BRC ODD ; IF C= 1, branch; otherwise proceed to next instruction
; if not odd output even marker {
CHARO 'E',i ; }
BR endIFELS ; unconditional jump over else
; if odd (branched on flag) {
ODD: CHARO 'O',i ;
; }
endIFELS:CHARO linef,i ;
STOP ;
.END
;ProgramB to do error check on signed computation
BR main ;
num1: .BLOCK 2 ;
num2: .BLOCK 2 ;
sum: .BLOCK 2 ;
linef: .EQUATE 0x000A ;
;
main: DECI num1,d
DECI num2,d ;
LDA num1,d ;
DECO num1,d ; output preliminary
CHARO '+',i
DECO num2,d ;
CHARO '=',i
ADDA num2,d ; adds num2 to num1, stores in A AFFECTS V FLAG
BRV error ; IF overflow, do error message
; if not error process sum
STA sum,d ;
DECO sum,d ;
BR endIFELS ; jump over else
; if error
error: CHARO 'N',i
CHARO 'O',i
CHARO 'T',i
STA sum,d ;
DECO sum,d ;
endIFELS:CHARO linef,i ;
STOP ;
.END
@TeppyCherry
Copy link

Hi! Thank you. It's very helpful.
In fibonacci.pep #, Could I get result = 0 when entered number = 0 , please?
Thanks again!
Cherry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment