Voting

Category

assembly language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language 6502 Assembler

(Apple 1 Version)

Date:06/03/10
Author:barrym
URL:n/a
Comments:0
Info:n/a
Score: (3.50 in 20 votes)
;BEER SONG IN 6502 ASSEMBLY LANGUAGE
;   BY BARRYM 2010-05-30
;THANKS TO:
;   SBPROJECTS.COM FOR LOTS OF VALUABLE
;      INFO AND A VERY NICE ASSEMBLER! 
;SEE THE END OF THIS LISTING FOR
;   IMPORTANT INFORMATION ABOUT THE
;   TARGET MACHINE!
;
;CONFIGURE.
;
   .CR 6502
   .TF AP1BEER.O,BIN
   .OR $0C00
;
;DEFINE.
;
;X REG. HOLDS NUMBER OF BOTTLES, (AND
;   TENS DIGIT IN THE BINARY-TO-ASCII
;   CONVERSION ROUTINE).
;Y REG. IS THE STRING INDEX POINTER.
;A REG. TAKES CARE OF EVERYTHING ELSE
;   (WITH A LITTLE HELP FROM THE STACK)
;ZERO PAGE IS NOT DIRECTLY DISTURBED.
;
;PRINT COMPLETE, CORRECT SONG (ADJUSTED
;   FOR UPPER-CASE, 40-COLUMN DISPLAY).
;
   LDX #MAXBEER   ;INIT. BEER COUNT
   BNE PRSONG     ;SING THE SONG
;
;EMIT ENTIRE SONG EXCEPT LAST SENTENCE.
;
BEERME:
   LDY #TAKE1-TXT ;? "TAKE ... AROUND,"
   JSR PRBOB      ;? X;" BOT ... WALL."
PRSONG:
   LDY #CR-TXT    ;BLANK LINE  
   JSR PRBOB      ;? X;" BOT ... WALL";
   LDY #COMCR-TXT ;? ","
   JSR PRBOB      ;? X;" BOT ... BEER."
   DEX            ;X=X-1
   BPL BEERME     ;IF X>-1 THEN BEERME
;
;EMIT LAST SENTENCE AND FALL THROUGH.
;
   LDX #MAXBEER   ;RESET BEER COUNT
   LDY #GSTOR-TXT ;? "GO TO ... MORE,"
;
PRBOB:
   TYA            ;SAVE PRE-STRING PTR
   PHA            ;   FOR LATER USE.
   JSR PUTS       ;EMIT THE PRE-STRING.
   TXA
   BEQ PRNONE     ;IF X>0 THEN
   PHA            ;   CONVERT X TO
   LDX #-1        ;   ASCII AND EMIT.
   SEC            ;   (
TENS:
   INX
   SBC #10
   BCS TENS
   PHA
   TXA
   BEQ ONEDIG
   ORA #"0"       ;   IF X>10 THEN
   JSR PUTCH      ;      ? INT(X/10);
ONEDIG:
   PLA            
   CLC
   ADC #10
   ORA #"0"
   JSR PUTCH      ;   ? X MOD 10;
   PLA
   TAX
   LDY #BOTTL-TXT
   BNE PRBOTT     ;   )
PRNONE:
   LDY #NOMOR-TXT
   JSR PUTS       ;ELSE ? "NO MORE";
PRBOTT:
   JSR PUTS       ;? " BOTTLE";
   INY
   DEX
   BEQ SINGLE
   DEY            ;IF X<>1 THEN ? "S";
SINGLE:
   INX
   JSR PUTS       ;? " OF BEER";
   PLA            ;RETRIEVE PRE-PTR
   CMP #COMCR-TXT
   BNE PRWALL
   LDY #DOTCR-TXT
   BPL PUTS
PRWALL:
   PHA            ;IF APPROPRIATE THEN
   JSR PUTS       ;   ? " ON THE WALL";
   PLA
   CMP #CR-TXT    ;IF APPROPRIATE THEN
   BEQ KPUT       ;   ? "."
;
;EMIT A HI-BIT-SET TERMINATED STRING
;   @ Y AND LEAVE Y @ BEGINNING OF NEXT
;   STRING.
;
PUTS:
   INY
   LDA TXT-1,Y
PUTCH:
   PHP
   ORA #ORMASK
   AND #ANDMSK    ;FORMAT CHAR FOR ECHO
   JSR ECHO
   PLP
   BPL PUTS
KPUT:
   RTS
;
TXT:
TAKE1:
   .AS /TAKE ONE DOWN AND PASS /
   .AS /IT AROUND,/
   .AS #13
   .AT / /
GSTOR:
   .AS /GO TO THE STORE AND BUY/
   .AS / SOME MORE/
COMCR:
   .AS /,/
   .AS #13
   .AT / /
NOMOR:
   .AT /NO MORE/
BOTTL:
   .AT / BOTTLE/
   .AT /S OF BEER/
   .AT / ON THE WALL/
DOTCR:
   .AS /./
CR:
   .AT #13
;
;THE TARGET MACHINE FOR THIS ASSEMBLY
;   IS THE APPLE 1, BUT IT IS VERY EASY
;   TO MAKE IT RUN ON DIFFERENT 65XX
;   MACHINES BY CHANGING THE NEXT TWO
;   EQUATES.  TWO MACHINE-TESTED
;   EXAMPLES:
;   APPLE II: $FDED, $80
;   COMMODORE 64: $FFD2, $00
;
ECHO    .EQ $FFEF ;EMIT ASCII CHAR
ORMASK  .EQ $80   ;($00 FOR + ASCII)
;
ANDMSK  .EQ ORMASK^127
MAXBEER .EQ 99    ;STARTING BEER COUNT
        .EN

Download Source | Write Comment

Alternative Versions

Comments

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: