10 Replies - 1027 Views - Last Post: 24 February 2016 - 11:14 AM

#1 patriotaki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 172
  • Joined: 08-October 14

Store integers from user input in array

Posted 23 February 2016 - 10:24 AM

im trying to store 8 integers from user input in an array but i always get an error like this : "Exception occured at PC 0x00040044"

.data
	prompt: .asciiz "Enter a number: " # message
	myArray: .space 32
.text

main:

while:			  # while loop until bytes become 32(4*8)
	beq $t0,32,exit
	
	li $v0,4	#loading message prompt ,syscall to print
	la $a0,prompt
	syscall
	
	li $v0,5	#user input 
	syscall
	
	
	sw $v0, myArray
	

	addi $t0, $t0,4 #increasingto index  		

			
	
	j while		#loop back

exit:

li $v0,10
syscall





Is This A Good Question/Topic? 0
  • +

Replies To: Store integers from user input in array

#2 turboscrew  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Store integers from user input in array

Posted 23 February 2016 - 11:09 AM

Can you map that address to a source line?
Maybe you can even single-step?

What does the program run on? Some emulator?
Looks like MIPS-code. :whistling:/>

[edit]
I suspect this:

Quote

sw $v0, myArray

You probably store a word in odd address.
(The string with the end-NUL added makes odd number of bytes.)

This post has been edited by turboscrew: 23 February 2016 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 patriotaki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 172
  • Joined: 08-October 14

Re: Store integers from user input in array

Posted 23 February 2016 - 11:15 AM

View Postturboscrew, on 23 February 2016 - 11:09 AM, said:

Can you map that address to a source line?
Maybe you can even single-step?

What does the program run on? Some emulator?
Looks like MIPS-code. :whistling:/>/>

[edit]
I suspect this:

Quote

sw $v0, myArray

You probably store a word in odd address.
(The string with the end-NUL added makes odd number of bytes.)


im running QtSpim yeah its MIPS

how can i store them in the array?
Was This Post Helpful? 0
  • +
  • -

#4 patriotaki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 172
  • Joined: 08-October 14

Re: Store integers from user input in array

Posted 23 February 2016 - 11:54 AM

im stuck at this for hours..any tips please?
Was This Post Helpful? 0
  • +
  • -

#5 patriotaki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 172
  • Joined: 08-October 14

Re: Store integers from user input in array

Posted 23 February 2016 - 12:59 PM

tried this.. still no luck

.data
	prompt: .asciiz "Enter a number: " # minima  gia na dosis arithmo
	myArray: .space 32
.text

main:

while:			  # while loop mexri ta bytes na ginun 32(4*8)
	beq $t0,32,exit
	
	addi $t0,$zero,0
	
	li $v0,4	#fortonume to minima prompt ,syscall gia na tipothi
	la $a0,prompt
	syscall
	
	li $v0,5	#user input apo ton xristi arithmo
	syscall
	
	move $s0,$v0
	sw $s0, myArray(t0)
	

	addi $t0, $t0,4 #auksanume to index kathe fora oste na teliosi 		

			#kapote to loop

	
	j while		#loop piso stin arxi

exit:

li $v0,10
syscall


Was This Post Helpful? 0
  • +
  • -

#6 turboscrew  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Store integers from user input in array

Posted 24 February 2016 - 01:47 AM

Have you figured out the code location of the crash?
If it's what I suspect, there should be a directive 'align'. Use that to align the start address of your vector
to word size. probably something like '.align 4'.
.data
    prompt: .asciiz "Enter a number: " # minima  gia na dosis arithmo
    .align 4
    myArray: .space 32


Was This Post Helpful? 0
  • +
  • -

#7 patriotaki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 172
  • Joined: 08-October 14

Re: Store integers from user input in array

Posted 24 February 2016 - 04:10 AM

View Postturboscrew, on 24 February 2016 - 01:47 AM, said:

Have you figured out the code location of the crash?
If it's what I suspect, there should be a directive 'align'. Use that to align the start address of your vector
to word size. probably something like '.align 4'.
.data
    prompt: .asciiz "Enter a number: " # minima  gia na dosis arithmo
    .align 4
    myArray: .space 32



omg this works! thank you!

but why align 4?
align 2 works fine too

This post has been edited by patriotaki: 24 February 2016 - 04:11 AM

Was This Post Helpful? 0
  • +
  • -

#8 patriotaki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 172
  • Joined: 08-October 14

Re: Store integers from user input in array

Posted 24 February 2016 - 07:17 AM

this is my code it works pretty well, i get 8 integers multiply them with 6 and print them in reverse
my only issue here now is that i get a zero in output, dont know where it comes from
maybe someone can help me out?
Posted Image
.data
	prompt: .asciiz "Enter a number: " # minima  gia na dosis arithmo
	numbers: .asciiz "\n Your calculated numbers are: "
	line: .asciiz "\n"
	paules: .asciiz "----------------------------------------"
	.align 2
	myArray: .space 32
.text

main:
li $t0,0
while:			# while loop mexri ta bytes na ginun 32(4*8)
	beq $t0,32,exit
	
	li $v0,4	#fortonume to minima prompt ,syscall gia na tipothi
	la $a0,prompt
	syscall
	
	li $v0,5	#user input apo ton xristi arithmo
	syscall
	
		
	sw $v0, myArray($t0)
	
	addi $t0, $t0,4 #auksanume to index kathe fora oste na teliosi 		

			#kapote to loop
	
	j while		#loop piso stin arxi

exit:
li $t0,32
li $t3,0
	li $v0,4	
	la $a0,paules
	syscall

	li $v0,4	
	la $a0,numbers
	syscall
loopp:
	beq $t0,0,exitt

	lw $t6,myArray($t0)
	add $t3,$t6,$t6
	add $t6,$t3,$t3
	add $t3,$t6,$t3

	li $v0,1
	addi $a0,$t3,0
	syscall

	li $v0,4
	la $a0,line
	syscall

	addi $t0,$t0,-4
	li $t3,0
	j loopp

exitt:

li $v0,10
syscall



This post has been edited by patriotaki: 24 February 2016 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#9 turboscrew  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Store integers from user input in array

Posted 24 February 2016 - 09:03 AM

Alignment by 4 just because 32-bit word takes 4 bytes.

It looks like you list the numbers "backwards". Are you sure you have that many numbers stored?
Was This Post Helpful? 0
  • +
  • -

#10 patriotaki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 172
  • Joined: 08-October 14

Re: Store integers from user input in array

Posted 24 February 2016 - 10:06 AM

View Postturboscrew, on 24 February 2016 - 09:03 AM, said:

Alignment by 4 just because 32-bit word takes 4 bytes.

It looks like you list the numbers "backwards". Are you sure you have that many numbers stored?


align 4 is 2^4 right?

i fixed the other issue i had to make the bytes 28 and then decrease to -4
Was This Post Helpful? 0
  • +
  • -

#11 turboscrew  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Store integers from user input in array

Posted 24 February 2016 - 11:14 AM

Usually .align 4 means alignment to 32-bit word boundary. (Simply "bytecount".)

This post has been edited by turboscrew: 24 February 2016 - 11:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1