0

C :

int main(){
  int A[2];
  A[1] = A[0] + A[1];
  return 0;
}

Assembly :

main:
    addiu   $sp,$sp,-16
    sw      $fp,12($sp)
    move    $fp,$sp
    lw      $3,0($fp)
    lw      $2,4($fp)
    addu    $2,$3,$2
    sw      $2,4($fp)
    move    $2,$0
    move    $sp,$fp
    lw      $fp,12($sp)
    addiu   $sp,$sp,16
    jr      $31
    nop

For the array which store A[1] and A[0]

I think might be store into page table first

  1. Based on the compiler read code from left to right ? A[1] first

  2. Based on the MIPS format setting from I learned on Computer Organization. A[0] first


    |opcode | rs | rt | rd | shmat | funct |


I am so curious about it.

jackson
  • 11
  • 3
  • 9
  • The format of a single instruction has nothing to do with C order of evaluation of operands for `+`, so IDK what your point is with that. C doesn't define an order (no sequence point within a `+` expression); compilers are allowed to choose whichever one they want. – Peter Cordes Feb 01 '22 at 08:33
  • Also, the observable behaviour of that C program doesn't include anything related to how it uses `A[]`, so a compiler can fully optimize it away. In fact, reading uninitialized `A[0]` and `A[1]` is undefined behaviour so a compiler is free to do literally anything, or emit any sequence of asm instructions, including no instructions for the whole `main`, if it wants. With optimization, GCC chooses to just return 0: https://godbolt.org/z/do68rqK6Y. But un-optimized code just naively reads it, like you're seeing in your asm block. – Peter Cordes Feb 01 '22 at 08:38
  • BTW, what page table are you talking about? Classic MIPS does software TLB management, but assuming the OS is somewhat normal then data values aren't loaded or stored "into the page table"; instead the OS consults the page table when handling a TLB miss. – Peter Cordes Feb 01 '22 at 08:40
  • @PeterCordes Thanks for answering my question. My question may not take into account practical aspects. – jackson Feb 01 '22 at 09:43
  • The entire question is about an implementation detail, so without considering practical aspects for a particular implementation, there no answer, and one could even say the question is nonsensical (or at least based on at least one false premise). Is that what you wanted to know? – Peter Cordes Feb 01 '22 at 09:45
  • I should find some compiler books, thanks. – jackson Feb 01 '22 at 09:46
  • Yes. I agree with your opinion – jackson Feb 01 '22 at 09:47
  • Looks like [Operator Precedence vs Order of Evaluation](https://stackoverflow.com/q/5473107) does a good job explaining why order of evaluation is totally up to the compiler, and not predictable based on anything in the source or the target ISA. – Peter Cordes Feb 01 '22 at 09:51
  • If you want to study compiler generated assembly, write *a function that takes parameters and returns a value*, that way you can turn on optimization, and see what the compiler has done when its smarts are applied. – Erik Eidt Feb 02 '22 at 17:20

0 Answers0