0
votes

I'm new to MIPS and am confused on how to go about writing mips could for a certain question that is given where I'm asked to write mips code to pulse bit 6 at a memory location of 0xABCDABC0 for a total of 2*n+74 times and assuming n is in $s1.

I'm not sure exactly how to deal with accessing a specific bit at the given address, I know that I'd have to use or immediate to specifically turn on the bit with 1 and then and it to turn the bit back to 0 using 1. The Loop (without multiplication) I could do add $s2, $s1, $s1 then addi $s2, $s2,74 but how do you loop it?

1
Pulse as in flip? You can alternate between AND'ing a bitmask with 0 in the position of the bit you are interested in (...11011111 - to turn it off) and ORing a bitmask with 1 in the position you are interested in (...00100000 - to turn it on).Andon M. Coleman

1 Answers

2
votes

The easiest part here is writing the loop, I can help you with that. I'm pretty rusty, but I'll do my best to get you started.

Looping can be achieved through temporary values, adding immediate values, branch instructions, and/or jump instructions.

The basic flow you're looking for sounds like:

  1. Create a counting variable (the i part in for (int i=..; i < ...; i++), as an example)
  2. Label the beginning instruction of your loop (that will look something like below) label_name: loop instructions
  3. Perform the loop instructions
  4. Increment your loop variable from step 1
  5. Populate $s2 with 2*n+74 (that's the part you have now)
  6. Branch to the beginning of the loop if your counter is not equal to your desired iteration count Note: if you have enough loop instructions, you might have to use a more complicated branch/jump construct.

Example loop code:

   andi $t1, $t1, 0 # set a counter variable to 0 (t1)
loop:
   # do some work - whatever you want to accomplish by looping
   addi $t1, $t1, 1 # increment the counter
   add  $s2, $s1, $s1  # this is the code you devised
   addi $s2, $s2, 74
   bne  $s2, $t1, loop #branch to the beginning of the loop if you need more iterations

The next part you need is grabbing a value at a memory address, there are a few ways to go about that. You should look into the load/store instructions available in MIPS. In particular, I think you will want to utilize the lw (load word) instruction.

lw r1, label -> loads the word from memory stored at the addressed stored in 'label' into register r1

I Googled a little and http://pages.cs.wisc.edu/~cs354-2/cs354/karen.notes/MAL.instructions.html had some reasonable explanations and and examples of those instructions.

Accessing a specific bit requires utilizing some bit operations. In particular, I think you will find bit shifting and masking helpful.
MIPS offers a few instructions you might like:

sll d, s1, s2 d = logical left shift of s1 by s2 places

sra d, s1, s2 d = arithmetic right shift of s1 by s2 places

srl d, s1, s2 d = logical right shift of s1 by s2 places

and d, s1, s2 d = s1 & s2; bitwise AND

-- From the source I cited above

I hope that is sufficient to answer your question, and least get you going on the problem. I didn't want to just hand you the code, since it sounded like homework. If anything isn't clear or needs clarification, please just say so.

Hope at least some of all of that helps you!