5 Replies - 4344 Views - Last Post: 03 March 2011 - 07:21 PM

#1 radicaledward   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

Memory location storage question

Posted 01 March 2011 - 12:31 PM

Hi. I'm working on a homework assignment that involves inputting values and storing them in consecutive memory locations. This is for the MIPS assembly language using PCspim.

Quote

Your main program should ask the user to enter an integer. The entered integer should be stored in
the memory.
2. The program should prompt the user to enter more integers until the user enters -1. All the numbers
that the user entered should be stored in consecutive memory locations. For example, if the user
inputs 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1; a part of the data segment should look like this:
[0x10010000] 0x00000001 0x00000002 0x00000003 0x00000004
[0x10010010] 0x00000005 0x00000006 0x00000007 0x00000008
[0x10010020] 0x00000009 0x0000000a 0x00000000 0x00000000

Obviously the way to enter the integers would be to use a simple loop that branches when the input is equal to -1. I have not problem with that part. The thing I'm having trouble with is where/how to store the data. I'm not really sure what to do to store the data in consecutive locations. We've learned about the .data and .text segments of the mips memory storage, but we haven't really learned how to store or any commands for the .data other than input and output strings. So my question would be, how do I store these values in consecutive locations, and how can I call them to be used later in the program.
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Memory location storage question

#2 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Memory location storage question

Posted 01 March 2011 - 02:22 PM

well i don't know MIPS but where is how i would do it x86.

  • label to jump back to "loop"
  • request input
  • store it at the location pointed to by a register, say 'ebx'
  • increment this pointer by the size of the data being entered
  • conditionally jump back to "loop"


in x86(im winging this, i don't have an assembler handy)

   mov ebx,0         ;starting address
.loop:
   push msg          ;request input
   call [puts]       ;puts(msg)
   add esp,4         ;adjust the stack
   mov eax,ebp       ;get base of stack to get the address of a local
   sub eax,4         ;subtract by 4 to get the actual address of the local
   push eax          ;pass address of local
   push formatstring ;format string for scanf, "%i"
   call [scanf]      ;scanf("%i",&somelocal)
   add esp,4         ;adjust the stack
   mov [edx],[ebp-4] ;*edx=somelocal
   add edx,4         ;change the adress to the next location
   cmp [ebp-4],-1    ;compare somelocal to -1 to end input
   jne loop          ;so long as somelocal is not -1 loop back 


Was This Post Helpful? 0
  • +
  • -

#3 radicaledward   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

Re: Memory location storage question

Posted 01 March 2011 - 07:07 PM

Ok this is what I have so far, I know I'm missing a decrement to the memory so the inputs can be read but I'm not sure where to put it right now. What I'm most concerned about is if I'm updating the memory locations correctly.
	.data
str1: 	.asciiz "Please enter an integer "
str2: 	.asciiz "The final sum is "
cr:	.asciiz "\n"
	.text
	.globl main
main:	
Loop:	li $v0, 4
	la $a0, str1
	syscall
	li $v0, 5
	syscall 
	add $t0, $v0, $zero		# load inputs
	beq $t0, -1, L1			# if the input is equal to -1 branch to L1
	sw $t0, $s0			# store the input in memory
	addi $s0, $s0, 4		# increment memory location
	j Loop				# loop back to start 
L1:	add $a0, $s0, $zero		# add the current input into a location where the procedure can find it
	jal calc			# call procedure
calc:	addi $sp, $sp, -12		#allocate stack room
	sw $ra, 8($sp)			#store return address
	sw $a0, 4($sp)			#store value of a0/value to be checked
	sw $v0, 0($sp)			#save output from previous
	addi $t4, $zero, -1		# t4 = -1
	bne $a0, $t4, L2		# if $t4 is not equal to -1 go to L2	
	jr $ra				# return to address from jal
L2:	blt $a0, $zero, L3		# go to L3 if input is negative
	addi $t2, $zero, 4		# $t2 = 4
	div $a0, $t2			# divide input by 4
	mfhi $t3			# load remainder of divison to t3
	bne $t3, $zero, L3		# if the remainder does not equal 0 go to L3
	add $v0, $v0, $a0		# add the current input to the output
	jal calc			# calls recursion for procedure
	lw $v0, 0($sp)			# load output from stack
	lw $a0, 4($sp)			# load a0 from stack
	lw $ra, 8($sp)			# load the address to return to from stack
	addi $sp, $sp, 12		# return memory
	jr $ra				# return to the address from jal
L3:	add $t5, $v0, $zero		# put the output of the procedure into t5 
	li $v0, 4			# load output messages
	la $a0, str2			# ""
	syscall				# system call
	li $v0, 1			# load outputs
	add $a0, $t5, $zero		# display outputs
	syscall
	li $v0, 4
	la $a0, cr
	syscall
	li $v0, 10
	syscall
	

Was This Post Helpful? 0
  • +
  • -

#4 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Memory location storage question

Posted 01 March 2011 - 08:26 PM

View Postishkabible, on 01 March 2011 - 09:22 PM, said:

  • label to jump back to "loop"
  • request input
  • store it at the location pointed to by a register, say 'ebx'
  • increment this pointer by the size of the data being entered
  • conditionally jump back to "loop"


look at 4, after you store the value entered by the user, increment the pointer to the next address(or deincrement, what ever the case may be)
Was This Post Helpful? 0
  • +
  • -

#5 radicaledward   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

Re: Memory location storage question

Posted 03 March 2011 - 06:58 PM

I don't know if you can answer this question or not as this being MIPS but it must be similar. I can't seem to figure out how to read from the memory locations after I store my inputs to get the actual integer values i.e. I get the actual address of where the data is stored instead of the integer stored there. Here is what I have:
	.data
str1: 	.asciiz "Please enter an integer "
str2: 	.asciiz "The final sum is "
cr:	.asciiz "\n"
	.text
	.globl main
main:	li $s0, 0x10010030		# set starting memory address
Loop:	li $v0, 4
	la $a0, str1
	syscall
	li $v0, 5
	syscall 
	add $t0, $v0, $zero		# load inputs
	beq $t0, -1, L1			# if the input is equal to -1 branch to L1
	sw $t0, ($s0)			# store the input in memory
	addi $s0, $s0, 4		# increment memory location
	j Loop				# loop back to start 
L1:	add $a0, $s0, $zero		# add the current input into a location where the procedure can find it
	jal calc			# call procedure
calc:	addi $sp, $sp, -12		#allocate stack room
	sw $ra, 8($sp)			#store return address
	sw $a0, 4($sp)			#store value of a0/value to be checked
	sw $v0, 0($sp)			#save output from previous
	addi $t4, $zero, -1		# t4 = -1
	bne $a0, $t4, L2		# if $t4 is not equal to -1 go to L2	
	jr $ra				# return to address from jal
L2:	blt $a0, $zero, L3		# go to L3 if input is negative
	addi $t2, $zero, 4		# $t2 = 4
	div $a0, $t2			# divide input by 4
	mfhi $t3			# load remainder of divison to t3
	bne $t3, $zero, L3		# if the remainder does not equal 0 go to L3
	add $v0, $v0, $a0		# add the current input to the output
	jal calc			# calls recursion for procedure
	lw $v0, 0($sp)			# load output from stack
	lw $a0, 4($sp)			# load a0 from stack
	lw $ra, 8($sp)			# load the address to return to from stack
	addi $sp, $sp, 12		# return memory
	jr $ra				# return to the address from jal
L3:	add $t5, $v0, $zero		# put the output of the procedure into t5 
	li $v0, 4			# load output messages
	la $a0, str2			# ""
	syscall				# system call
	li $v0, 1			# load outputs
	add $a0, $t5, $zero		# display outputs
	syscall
	li $v0, 4
	la $a0, cr
	syscall
	li $v0, 10
	syscall
	


Was This Post Helpful? 0
  • +
  • -

#6 radicaledward   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 15-November 10

Re: Memory location storage question

Posted 03 March 2011 - 07:21 PM

I actually just figured this out lw is the correct command obviously -_-. However I still have a problem with the recursion in this if anyone was actually looking lol. I don't really know where to place the command for decrementing the stack so I can read every input from the memory, my code as of know does produce an output, but its definitely not correct.
	.data
str1: 	.asciiz "Please enter an integer "
str2: 	.asciiz "The final sum is "
cr:	.asciiz "\n"
	.text
	.globl main
main:	li $s0, 0x10010030		# set starting memory address
Loop:	li $v0, 4
	la $a0, str1
	syscall
	li $v0, 5
	syscall 
	add $t0, $v0, $zero		# load inputs
	beq $t0, -1, L1			# if the input is equal to -1 branch to L1
	sw $t0, ($s0)			# store the input in memory
	addi $s0, $s0, 4		# increment memory location
	j Loop				# loop back to start 
L1:				# add the current input into a location where the procedure can find it
	jal calc			# call procedure
calc:	addi $sp, $sp, -12		#allocate stack room
	sw $ra, 8($sp)			#store return address
	sw $a0, 4($sp)			#store value of a0/value to be checked
	sw $v0, 0($sp)			#save output from previous
	addi $t6, $zero, 4
	sub $s0, $s0, $t6
	lw $a0, ($s0)				#save output from previous
	addi $t4, $zero, -1		# t4 = -1
	bne $a0, $t4, L2		# if $t4 is not equal to -1 go to L2	
	jr $ra				# return to address from jal
L2:	blt $a0, $zero, L3		# go to L3 if input is negative
	addi $t2, $zero, 4		# $t2 = 4
	div $a0, $t2			# divide input by 4
	mfhi $t3			# load remainder of divison to t3
	bne $t3, $zero, L3		# if the remainder does not equal 0 go to L3
	add $v0, $v0, $a0		# add the current input to the output
	jal calc			# calls recursion for procedure
	lw $v0, 0($sp)			# load output from stack
	lw $a0, 4($sp)			# load a0 from stack
	lw $ra, 8($sp)			# load the address to return to from stack
	addi $sp, $sp, 12		# return memory
	jr $ra				# return to the address from jal
L3:	add $t5, $v0, $zero		# put the output of the procedure into t5 
	li $v0, 4			# load output messages
	la $a0, str2			# ""
	syscall				# system call
	li $v0, 1			# load outputs
	add $a0, $t5, $zero		# display outputs
	syscall
	li $v0, 4
	la $a0, cr
	syscall
	li $v0, 10
	syscall
	


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1