-1

I'm trying to write the code below in C but I have a real problem with doing that so if someone can help me out and explain to me in the same time I'd be grateful.

array .word ? 
mystery: 
  add r2,r0,r0
  addi r7,r0,32
loop: 
  movia r4, array
  slli r3,r2,2
  add r5,r3,r4
  ldw r14, (r5)
  add r15, r3, r14
  stw r0, (r15) 
  addi r2, r2, 1
  bge r7,r2,loop
  ret 
Jester
  • 56,577
  • 4
  • 81
  • 125
jack bloom
  • 45
  • 3
  • You should ask a more specific question. See http://stackoverflow.com/help/how-to-ask. – Bob Apr 15 '15 at 15:51
  • 1
    why isn't it specific ? the code is in ASSEMBLY and I have to write it in C – jack bloom Apr 15 '15 at 15:58
  • I assume that this is a function named "mystery", but arguments to a function should be passed in r4-r7. However that doesn't appear to be the case here. What is the assumption on the value of r0? It is the only register used before being written. Is this function supposed to have a return value? The convention is to return the value in r2-r3. Basically we need to understand the calling convention used here in order to translate to C. – Brad Budlong Apr 15 '15 at 17:06
  • the register r0 containts zeros, and I don't have the calling code. and that's right the registers r4-r7 are used to pass the arguments and the register r2 is for the returning the values. How about the first line ? does somebody know what it serves for ? – jack bloom Apr 15 '15 at 17:19

1 Answers1

0

I don't believe that array .word ? is correct syntax. I think it is just representing that there is an array of some size at the location array. Here's the translation:

int *array[];
void mystery () {
    int r2;
    int *r14;
    for (r2 = 0; r2 <= 32; r2++) {
        r14 = array[r2];
        r14[r2] = 0;
    }
}
Brad Budlong
  • 1,775
  • 11
  • 10
  • ldw r14, (r5) = *r14 ? is it because theres nothing beside the (r5) ? can you explain a little bit please ? – jack bloom Apr 15 '15 at 18:31
  • ldw loads the value pointed to by r5 in r14. The address in r5 is a byte address, but the item there is a word. r5 was created by taking the array address and adding 4 times the offset which is r2. That 4 times piece is there to create a word address instead of a byte address. C does that for you automatically in the array operation. So that all becomes 'r14 = array[r2]' – Brad Budlong Apr 15 '15 at 18:35