[CSCI2321] Array example

John Howland jhowland at ariel.cs.trinity.edu
Fri Feb 3 13:16:02 CST 2006


I have edited out the unnecessary lines in the array-puck-cc.s
example we were looking at today and added some comments.

I have attached this file as well as an image of the xspim system
after running this program.  You can see the sum 6 and the
array values of 0, 1, 2, and 3 in memory after running the
program.  I used a breakpoint at 0x004000b0 ( the location of
the addiu $sp,$sp,48 instruction ) so that you could see
the values on the stack.  xspim doesn't easily allow you to
view the stack after the allocation frame on the stack is
released.

I have extended the due date for the second homework problem
set to Wednesday, February 8, 2006.

For subroutine linkage examples for problem 2.15, study section
2.7 of the text.  Because the two subroutines have arguments,
you will need to preserve the arguments in memory (on the stack
in the allocation frame) and restore (might not be necessary)
upon leaving a subroutine.  There are two potential strategies
for this:

1) make saving/restoring state the responsibility of the caller

2) make saving/restoring state the responsibility of the routine
    being called.

Which is preferrable?  Why?

_______________________________________________________________
John E. Howland       url: http://www.cs.trinity.edu/~jhowland/
Computer Science    email: jhowland at ariel.cs.trinity.edu
Trinity University  voice: (210) 999-7364
One Trinity Place     fax: (210) 999-7477
San Antonio, Texas  78212-7200
-------------- next part --------------
	.ent	main
main:	# Location of vars on stack
	# a = 8
	# sum = 0
	# i = 4
        addiu $sp,$sp,-48               #  allocate space on stack
        sw $4,36($sp)                   #  argc
        sw $5,44($sp)                   #  argv
	sw $0,0($sp)                  	#  zero sum
	sw $0,4($sp)                  	#  zero i
init:	# 0x18
	lw $4,4($sp)                  	#  Reg4 = i
	addiu $5,$sp,8                	#  Reg5 = address of a[0]
	lw $6,4($sp)                  	#  Reg6 = i
	sll $6,$6,2                   	#  multiply i by 4
	addu $5,$5,$6                 	#  address of a[i]
	sw $4,0($5)                   	#  a[i] = i
	lw $3,4($sp)                  	#  Reg3 = i
	addiu $3,$3,1                 	#  i + 1
	sw $3,4($sp)                  	#  i = i + 1
	lw $12,4($sp)                  	#  Reg12 = i
	addiu $2,$0,4                 	#  Reg2 = 4
	bne $12,$2,init                	#  loop back if not = 4
	nop                           	#  
	sw $0,4($sp)                  	#  zero i
loop:	# 0x50
	addiu $10,$sp,8               	#  Reg10 = address of a[0]
	lw $11,4($sp)                 	#  Reg11 = i
	sll $11,$11,2                 	#  multiply i by 4
	addu $10,$10,$11              	#  address of a[i]
	lw $10,0($10)                 	#  a[i]
	lw $11,0($sp)                 	#  sum
	addu $10,$10,$11              	#  a[i] + sum
	sw $10,0($sp)                 	#  sum = a[i] + sum
	lw $9,4($sp)                  	#  Reg9 = i
	addiu $9,$9,1                 	#  i + 1
	sw $9,4($sp)                  	#  i = i + 1
	lw $7,4($sp)                  	#  Reg7 = i
	addiu $8,$0,4                 	#  Reg8 = 4
	bne $7,$8,loop                	#  loop back if not = 4
	nop                           	#  
	or $2,$0,$0                   	#  Reg2 = 0  (return code)
        addiu $sp,$sp,48                #  deallocate memory on stack
	jr $31                        	#  
	nop                           	#  
	.end	main
-------------- next part --------------
A non-text attachment was scrubbed...
Name: xspim.jpg
Type: image/jpeg
Size: 117980 bytes
Desc: 
Url : http://Sol.CS.Trinity.Edu/pipermail/csci2321/attachments/20060203/257b5776/xspim-0001.jpg


More information about the CSCI2321 mailing list