4 Replies - 20634 Views - Last Post: 18 April 2012 - 04:10 PM

#1 Dallal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-April 12

How can I store sequence in an array in MIPS?

Posted 17 April 2012 - 06:34 AM

I have this homework problem .

Write a program that produces the following sequence; 1,2,2,4,8, 32, 256,… and stores it in an array, depending on the number of terms chosen by the user. Each element in the sequence can be calculated by multiplying the two elements preceding it. In other words the nth sequence number Sn is calculated by the equation Sn=Sn-1×Sn-2.

I tried but it didn't run

My code

^
^


	.data	# Data declaration section
str1: .ascii "Please enter the number of terms to produce: "
arr: .space 40

	.text

main:		# Start of code section
	
	
li $v0, 4	# system call code for printing string = 4
la $a0, str1	# load address of string to be printed into $a0
syscall			# call operating system to perform print operation

li $v0, 5	# get ready to read in integers
syscall		# system waits for input
move $s0,$v0	# store the result of the read (returned in $v0) in num1


la $s1,arr
addi $t2,$zero,1    
addi $t0,$zero,1
addi $t3,$zero,1
add $t1,$t0,$t0



L1:
beq $t2,$s0,print 
add $t0,$t1,$zero
add $t1,$t3,$zero
mul $t3,$t0,$t1
sw $t3,0($s1)
addi $t2,$t2,1       
addi $s1,$s1,4
j L1

print:
beq $t2,$s0,Exit 
lw $t3,0($s1)
li $v0, 1	# system call code for print_int
move $a0, $t3	# integer to print
syscall		# print it
addi $s1,$s1,4
j print


Exit:
li $v0, 10		# exits program
syscall


	

# END OF PROGRAM


Is This A Good Question/Topic? 0
  • +

Replies To: How can I store sequence in an array in MIPS?

#2 turboscrew  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 152
  • View blog
  • Posts: 1,029
  • Joined: 03-April 12

Re: How can I store sequence in an array in MIPS?

Posted 18 April 2012 - 01:09 AM

I added some comments to your code (beginning with ";").

Maybe you can see:

; $a0 = format string
; $s0 = number count
; $s1 = storage-pointer

; $t0 = factor1
; $t1 = t1 = factor2
; $t2 = loop counter
; $t3 = product

    .data   # Data declaration section
str1: .ascii "Please enter the number of terms to produce: "
arr: .space 40

    .text

main:       # Start of code section

li $v0, 4   # system call code for printing string = 4
la $a0, str1    # load address of string to be printed into $a0
syscall         # call operating system to perform print operation
 
li $v0, 5   # get ready to read in integers
syscall     # system waits for input
move $s0,$v0    # store the result of the read (returned in $v0) in num1

la $s1,arr
addi $t2,$zero,1	; t2=1
addi $t0,$zero,1	; t0=1
addi $t3,$zero,1	; t3=1
add $t1,$t0,$t0		; t1=2

L1:
beq $t2,$s0,print	print	; if count == given num, print and exit
add $t0,$t1,$zero	; t0 = t1 => factor1 = factor2
add $t1,$t3,$zero	; factor2 = old product
mul $t3,$t0,$t1		; t3 = t0 * t1 => t3 = new product
sw $t3,0($s1)		; store new product
addi $t2,$t2,1      	; inc loop counter
addi $s1,$s1,4		; inc storage pointer
j L1			; next round
 
print:
beq $t2,$s0,Exit	; if count == given num goto exit
lw $t3,0($s1)		; load last result into the storage
li $v0, 1   # system call code for print_int
move $a0, $t3   # integer to print
syscall     # print it
addi $s1,$s1,4		; inc storage pointer (still)
j print			; next round

Exit:
li $v0, 10      # exits program
syscall

# END OF PROGRAM



This jump is always taken, because the condition is the same that lead here:

print:
beq $t2,$s0,Exit ; if count == given num goto exit

from:
beq $t2,$s0,print print ; if count == given num, print and exit

Also in the print routine, I didn't see the storage pointer returned to the beginning., but I saw it incremented further.
That might cause segmentation fault (or bus error or similar):
=> access to memory not allocated for you.

Be careful in this, I really don't know MIPS assembly (yet), so my comments are based mostly in guesswork.

This post has been edited by turboscrew: 18 April 2012 - 01:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 Dallal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-April 12

Re: How can I store sequence in an array in MIPS?

Posted 18 April 2012 - 11:10 AM

I Change in my Code but it display Exeption [Address Error ...etc]
I think it wrong in store in an array :((





this
data	# Data declaration section
str1: .ascii "Please enter the number of terms to produce: "
arr: .space 40

.text

main:		# Start of code section
	
	
li $v0, 4	# system call code for printing string = 4
la $a0, str1	# load address of string to be printed into $a0
syscall			# call operating system to perform print operation

li $v0, 5	# get ready to read in integers
syscall		# system waits for input
move $s0,$v0	# store the result of the read (returned in $v0) in num1


la $s1,arr
addi $t2,$zero,1    
addi $t0,$zero,1
addi $t3,$zero,1
add $t1,$t0,$t0

L1: 
lw $t3,0($s1)
add $t0,$t1,$zero
add $t1,$s1,$zero
mul $s1,$t0,$t1
sw $t3,0($s1)
addi $t2,$t2,1       
addi $s1,$s1,4
beq $t2,$s0,print
j L1

print:
la $s1,arr
addi $t2,$zero,1   
addi $t3,$zero,1
L2:  
lw $t3,0($s1)
li $v0, 1	# system call code for print_int
move $a0, $t3	# integer to print
syscall		# print it
addi $s1,$s1,4
addi $t2,$t2,1 
slt $s1,$t2,$s0
bne $s1,$zero,L2


Exit:
li $v0, 10		# exits program
syscall


Was This Post Helpful? 0
  • +
  • -

#4 turboscrew  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 152
  • View blog
  • Posts: 1,029
  • Joined: 03-April 12

Re: How can I store sequence in an array in MIPS?

Posted 18 April 2012 - 02:55 PM

It looked to me that setting the array pointer back to the beginning of the array and resetting the counter to zero in the print-part before the test and branch to exit should be enough. (I'm referring to the version I commented)

Like I said, I don't know MIPS assembly, but if the numbers are 32-bit (4 bytes) like the increment suggests, maybe the table needs to be aligned at 4-byte border?

Is there a directive ".align"?
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: How can I store sequence in an array in MIPS?

Posted 18 April 2012 - 04:10 PM

Perhaps you'd be able to tell what's going on in your code, if you commented it like turboscrew did. In addition, other people would quickly be able to understand your code and point out any mistakes. You've commented everything but the actual computation part! Also, I suggested earlier that you run this code through a debugger, so you can see what's wrong. Please use it. It's a simple way to correct your program. I see you haven't done much work on it since then.

This post has been edited by blackcompe: 18 April 2012 - 04:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1