Hi this is what I need to do

  1. Declare a word array A with 2 elements with initialized values.
  2. Declare a word variable B with initialized value as 0.
  3. Calculate the cvalue of B with the following expression: B= A[0] + A[1] -6.
  4. Make sure B's value is saved back into memory.

This is what I have so far.

#Program to read a number and print its square

 .data 				#variable used follow this line
 A: .word 4,8
 B: .word 0


 .text 				#program's code after this line

 main:
 add A[0], 	A[1],     $t1	#Move the integer into $t1
 sub 	$t1,	6,	$t1 	#Multiply the contents of $t1 with itself
 move 	$a0,	$t1 		#Load $a0 with the value in $t1

 end:
 li 	$v0,	10 		#System call code to Exit
 syscall			 #call OS to Exit the program

I initialized the values, but I'm not sure how or where I need to store them. I was thinking of using a load address, but am unsure how to use it at this point. Also I can't do the math, like add because I don't know where the values are being stored. I'm also not sure what to do about the -6, like if I should somehow store that value as well. Finally I'm not sure how to check to make sure the value was stored in B. Any ideas or any help will be greatly apprecitated.

Recommended Answers

All 8 Replies

Are you using SAL (or are you supposed to be using MAL or TAL)? I'll assume SAL.

Remember that the result is stored in the first item listed. For example: add $t0, $a0, 12 adds the value of register a0 and the value 12 together, and stores the result in register t0.

Your math looks good. Add A[0] and A[1], then subtract 6. Just work on getting the instructions written correctly.

Once you have loaded the answer in to register t1, you can store the result in B. move B, $t1 You can prove that B has the correct value by printing it put B Hope this helps.

Thanks that does help a lot, just to clerify, just put put B at the end and in the console it will print that value. I am using Spim and MIPS instruction code. Also is A automatically stored is $a0, or do i have to use la $a0, A but again thanks for the help.

That depends on whether or not you are using SAL.

to be honest idk what SAL is I'm very new to this as you can see, how can I tell whats I'm using?

I'm using MIPS with PCSpim, this is my actual code

#Program to read a number and print its square

 .data 				#variable used follow this line
 A: .word 4,8
 B: .word 0


 .text 				#program's code after this line

 main:
 lw	$t0,	4($a0)
 add	$t0,	4($a0),	$a0
 addi	$t0,	$t0,	-6
 mov	B,	$t0	
 put	B

 end:
 li 	$v0,	10 		#System call code to Exit
 syscall			 #call OS to Exit the program

PCSpim gives me a syntax error when trying to load it into it. Any ideas if this is right or why I am getting this error? Any help is appreciated in advance.

MIPS assembly language is confusingly structured into SAL, MAL, and TAL modes. I will assume MAL (based on your latest code). Your professor should have mentioned what you have been using so far.

You are getting an error because you can't have a reference to memory in an add instruction. Everything to be added must be in stored registers. The last thing to be added may be a constant (or "immediate", like 12). So, add $t0, $a0, $a1 is OK, as is add $t0, $a0, 12 However add $t0, 4($a0), $a0 is not, because you are trying to reference memory.

You need to be very careful of what the values in a register mean. For each register, are you using it to hold a number or an address? It makes a difference. Hence, add $t0, 4($a0), $a0 makes no sense, since you are trying to add a value stored at (address $a0+4) to the (value $a0). The register $a0 should be treated as either a number or an address, but not both.

Be careful to remember that in MIPS assembly, the register used to store the arithmetic result is always listed first. Hence, add $t0, $t1, $t2 means
$t0 := $t1 + $t2

Finally, you'll need to get the address of the array A before you can use lw and sw. Example:

.data
x: .word 98, 3

.text
main:
	la	$t0, x		# $t0 := address of x[]
	lw	$s0, 0($t0)	# $s0 := value of x[ 0 ]
	lw	$s1, 4($t0)	# $s1 := value of x[ 1 ]

Make sense? Now you can add $s0 and $s1.

Hope this helps.

Ok that helped a ton, here is my code, that I believe works because the $s2 register ends up being 0000006, which is 8+4-6. My question is, for B since it is a single variable do I need to load its address, then load the word like I do for A? I did in my code and it worked, but was wondering if it was necessary.

.data 				#variable used follow this line
 A: .word 4,8
 B: .word 0


 .text 				#program's code after this line

 main:
	la	$t0, A		# $t0 = address of A[]
	lw	$s0, 0($t0)	# $s0 = value of A[ 0 ]
	lw	$s1, 4($t0)	# $s1 = value of A[ 1 ]
	add	$t1, $s0, $s1	# add $s0 and $s1
	addi	$t1, $t1, -6	# subtract 6 from  $t1
	la	$t2, B		# $t2 = address of B[]
	lw	$s2, 0($t2)	# $s2 = value of B[ 0 ]
	move	$s2, $t1	# $s2 = value of $t1

 end:
 li 	$v0,	10 		#System call code to Exit
 syscall			 #call OS to Exit the program

In MAL, no. Just use sw $s2, B You really need to get yourself a good MIPS reference.
The one on Wikipedia is pretty good.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.