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 (MIPS)

Date:05/13/05
Author:Dean Liu
URL:n/a
Comments:7
Info:n/a
Score: (3.52 in 266 votes)
####################################################
#Author:	Dean Liu
#Contact: 	deanliu@cs.ucsb.edu
#		deanliu@umail.ucsb.edu
#Date:		May 1st,2005
#Description:	MIPS Assembly version of
#               99 bottles 
#		of beer on the wall
####################################################
	.data			#data portion
strng:	.asciiz	" bottles of beer on the wall, "
strng2:	.asciiz	" bottles of beer "
strng3: .asciiz "\ntake one down and pass it around, "
strng4: .asciiz " bottle of beer on the wall "
strng5: .asciiz " bottle of beer "
strng6: .asciiz " bottles of beer on the wall.\n "
one:	.word	1	
	.text			#code section
main:				#main
	li	$a2, 1		#
	li	$a3, 99		#start with 99 
loop:	jal	PRNTB		#prnt bottle count
	la	$a0, strng	#print strng
	li	$v0, 4		#
	syscall			#
	jal	PRNTB		#
	la	$a0, strng2	#print strng2
	li	$v0, 4		#
	syscall			#
	la	$a0, strng3	#print strng3
	li	$v0, 4		#
	syscall			#
	sub	$a3, $a3, 1	#subtract one 
	jal	PRNTB		#
	la	$a0, strng6	#print strng6
	li	$v0, 4		#
	syscall			#
	bne	$a3, $a2, skip	#handles one 
                                #bottle on wall
	jal	ONEBOT		#
skip:	bnez	$a3, loop	#Loop if not equal
                                #to 0
	li	$v0, 10		#exit
	syscall			#
####################################################
# Routine:	PRINTB($a0)
# Description:	Prints out current bottle count
####################################################
PRNTB:	move	$a0, $a3	#prnt bottle count
	li	$v0, 1		#
	syscall			#
	jr	$ra		#return from method
####################################################
# Routine:	ONEBOT($a0)
# Description:	Handles one bottle count.
####################################################
ONEBOT: addi	$sp,$sp,-4	#allocate
	sw	$ra,0($sp)	#store rtrn 
                                #address
	jal	PRNTB		#
	la	$a0, strng4	#print strng4
	li	$v0, 4		#
	syscall			#
	jal	PRNTB		#
	la	$a0, strng5	#print strng5
	li	$v0, 4		#
	syscall			#
	la	$a0, strng3	#print strng3
	li	$v0, 4		#
	syscall			#
	sub	$a3, $a3, 1	#subtract one 
	jal	PRNTB		#
	la	$a0, strng6	#print strng6
	li	$v0, 4		#
	syscall			#
	lw	$ra,0($sp)	#load address
	addi	$sp,$sp,4	#pop
	jr	$ra		#rtrn from method

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
1Steve Checkoway06/01/050

Comments

>>  lizzy said on 05/14/05 06:20:21

lizzy -O_O-

>>  Mr. Stinky said on 05/24/05 16:58:35

Mr. Stinky *_* - googd one!

>>  . said on 05/24/05 22:40:35

. go gauchos!

>>  Yogyakarta Jogja said on 08/06/05 15:37:49

Yogyakarta Jogja iki maksude piye dab? ora mudeng aku... kok koyo dagelan wae...

>>  dylan said on 03/16/08 06:57:08

dylan sb&&iv.ftw!

>>  rahul said on 03/19/08 21:21:52

rahul PRNTB: move $a0, $a3 #prnt bottle count
li $v0, 1 #
syscall #
jr $ra #return from method

This is entirely correct. $ra is not a persistant register. so when you do a
syscall, your $ra can get overwritten by it.
You can either use another register or push it in the stack as below

PRNTB: addi $sp, $sp, -4
sw $ra, 4($sp)
move $a0, $a3 #prnt bottle count
li $v0, 1 #
syscall #
lw $ra, 4($sp)
add $sp, $sp, 4
jr $ra #return from method

>>  Dean Liu said on 12/30/08 05:43:48

Dean Liu It's been awhile since I wrote this. Oh how I miss UCSB.

E-mail address has expired - deanliu@gmail.com if anyone needs to reach me! Good luck in CS30!

Go Gauchos!

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: