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 Assembler (motorola 6809)

(used in the CBM Pet, and the Dragon 32)

Date:08/16/07
Author:Rik Hughes
URL:http://www.rikaitch.plus.com
Comments:1
Info:http://en.wikipedia.org/wiki/Motorola_6809
Score: (3.53 in 145 votes)
c:\meta\i\6809/
	intel
	title   "99 bottles of beer"
	.tab    8,8,8,80
;
	public  bottlebeer
;
	segment 'ram'
tmp     ds.b    1
;
	segment 'eprom'
;
;*** 99 bottles of Beer
;*** Rik Hughes, 16/08/2007
;*** written for the intel 6809 cpu
;
bottlebeer:
bbeer0:	ldab	#99
bbeer1:	ldx	#0
	tstb
	beq	bbeer2:
	jsr	shwnum		; show number of bottles left
	bra	bbeer3
bbeer2:	ldx	#notxt		; 0 = "no"
	jsr	shwtxt
bbeer3:	ldx	#btltxt		; bottle
	jsr	shwtxt
	cmpb	#1
	beq	bbeer4
	ldaa	#'s'		; not 1, then show an 's'
bbeer4:	ldx	#bertxt		; "of beer"
	jsr	shwtxt
	ldx	#waltxt		; "on the wall"
	jsr	shwtxt
	ldx	#crtxt		; do a cr
	jsr	shwtxt
bbeer5:	ldx	#0
	tstb
	beq	bbeer6:
	jsr	shwnum		; show number of bottles left
	bra	bbeer7
bbeer6:	ldx	#notxt		; 0 = "no"
	jsr	shwtxt
bbeer7:	ldx	#btltxt		; bottle
	jsr	shwtxt
	cmpb	#1
	beq	bbeer8
	ldaa	#'s'		; not 1, then show an 's'
bbeer8:	ldx	#bertxt		; "of beer"
	jsr	shwtxt
	ldx	#crtxt		; do a cr
	jsr	shwtxt
	tstb			; is it 'no bottles of beer?'
	beq	bbeer9
	ldx	#pastxt		; "take one down, pass it around" \n
	jsr	shwtxt
	decb			; lose a bottle
	bra	bber10
bbeer9:	ldx	#mortxt
bber10:	bra	bbeer0		; go back to the start
	rts

; show 2 digit number stored in x
shwnum:	pshs	a		; preserve accumulator
	pshs	x
	ldx	#10
	pshs	x
	jsr	div16		; divide x on stack by 10
	puls	a		; get result
	puls	a		; remembering to forget high byte
	tsta			; is it 0?
	beq	shwnm1		; yes, so don't display it.
	staa	stdout		; display 10's
shwnm1:	puls	a		; get remainder
	puls	a
	staa	stdout		; display units always
	puls 	a		; restore accumulator

;show text string stored in x
shwtxt:	pshs	b		;preserve register
	clr	b		;reset pointer
shwtx1:	pshs	x		;preserve address of text
	puls 	x
	abx			;get pointer in text
	lda	0,x
	beq	shwtx2
	staa	stdout		;display char
	incb
	bra	shwtxt1		;move to next char
shwtxt2	puls	b
	rts

;
;*** Divide two sixteen bit values on the system stack
;
div16:	pshs	d,x
	tfr	s,x
	ldx	8,x	; get dividend
	pshs	x
	ldx	#0	
	pshs	x
	lda	#16	
	tfr	s,x
div162:	rol	13,x	
	rol	12,x	
	rol	1,x	
	rol	0,x	
	pshs	a
	ldd	0,x	
	subd	10,x	
	bcs	div161	
	std	0,x	
div161:	puls	a
	deca	
	bne	div162	
	rol	13,x	
	rol	12,x	
	com	13,x	
	com	12,x	
	ldd	0,x	
	std	10,x	
	puls	x
	puls	x
	puls	d,x,pc

.btltxt	dc.b	"bottle",0
.bertxt dc.b	" of beer",0
.waltxt	dc.b	" on the wall",13,10,0
.notxt	dc.b	"no ",0
.pastxt dc.b	"take one down, pass it around,",13,10,0
.crtxt	dc.b	13,10,0
.mortxt	dc.b	"Go to the store and buy some more,",13,10,0

	end

Download Source | Write Comment

Alternative Versions

Comments

>>  Ricardo Malerba said on 01/08/08 19:30:26

Ricardo Malerba The correct chip manufacturer is Motorola, no Intel !!!. In my opinion, the 6809, one of the best microprocessor than I have seen.

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: