Skip to content

Instantly share code, notes, and snippets.

@b-adams
Created October 22, 2012 15:23
Show Gist options
  • Save b-adams/3932052 to your computer and use it in GitHub Desktop.
Save b-adams/3932052 to your computer and use it in GitHub Desktop.
Loop in loop for triangle star
Br main
LCOut: .block 2 ;global variable #2d
LCIn: .block 2 ;global variable #2d
UserInPt: .block 2 ;global variable #2d
msg1: .ASCII "Please enter a number (of stars wide):\x00" ;Don't forget the string terminator!
;Welcome user, get input, descend into outer loop
main: NOP0
STRO msg1,d
DECI UserInPt,d
BR prepOut,i
;Initialize Loop Control variable LCOut for outer loop
prepOut: NOP0
LDA 0,i
STA LCOut,d
BR startOut,i
;Begin outer loop. Loops TEST (startOut), execute BODY (bodyOut), make PROGRESS (incrOut), and go back to the TEST
startOut: NOP0
LDA LCOut,d
CPA UserInPt,d
BRGE endOut,i ;Jump to end of loop if LCOut >= UserInPt
BR bodyOut,i ;Code-rearrangement failsafe jump
;loop BODY
bodyOut: NOP0
BR innerSt,i ;Jump to inner loop, which is responsible for printing LCOut many stars
innerDn: NOP0 ;Point to return to from inner loop
CHARO '\n',i ;Always put a newline
BR incrOut,i ;Code-rearrangement failsafe jump
;loop PROGRESS
incrOut: NOP0
LDA LCOut,d
ADDA 1,i
STA LCOut,d ;LCOut = LCOut + 1
BR startOut,i ;Always return to top of loop
endOut: NOP0
;STRO msg2,d
;LDA 3,i
;STA LCOut,d
;BR innerSt,i
;STRO msg3,d
STOP
;
; the... INNER LOOP! (dun dun dun)
;
innerSt: NOP0
;inner loop INITIALIZE
PrepIn: NOP0
LDA 0,i
STA LCIn,d
BR startIn,i
;inner loop TEST
startIn: NOP0
LDA LCIn,d
CPA LCOut,d
BRGE endIn,i
BR bodyIn,i
;inner loop BODY
bodyIn: NOP0
CHARO '*',i
Br incrIn,i
;inner loop INCREMENT
incrIn: NOP0
LDA LCIn,d
ADDA 1,i
STA LCIn,d
BR startIn,i
endIn: NOP0
;return from inner loop
BR innerDn,i
.END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment