data structure layout
play

Data Structure Layout In HERA/Assembly Today, were going to build - PowerPoint PPT Presentation

Data Structure Layout In HERA/Assembly Today, were going to build some data structures in HERA First, a note on memory Registers are very fast RAM is relatively slow We use a cache to sit between them 8-core Haswell chip Why cant you


  1. R1 = 0 // Assume t is in R1 R2 = after1 // Assume return addr in R2 // Return result in R3 R3 = 0 LABEL(sumTree) SUB(R0,R0,R1) R4 = 13 BNZ(not_null) SET(R3, 0) BR(R2) LABEL(not_null) LOAD(R4, 0, R1) 42 LOAD(R1, 1, R1) a1 SET(R2, after1) BR(sumTree) LABEL(after1) ADD(R4,R3,R4) LOAD(R1, 2, R1) SET(R2, after2) BR(sumTree) LABEL(after2) 13 63 a3 ADD(R4,R3,R4) a2 MOVE(R3,R4) BR(R2)

  2. R1 = 0 // Assume t is in R1 R2 = after1 // Assume return addr in R2 // Return result in R3 R3 = 0 LABEL(sumTree) SUB(R0,R0,R1) R4 = 13 BNZ(not_null) SET(R3, 0) BR(R2) LABEL(not_null) LOAD(R4, 0, R1) 42 LOAD(R1, 1, R1) a1 SET(R2, after1) BR(sumTree) LABEL(after1) ADD(R4,R3,R4) LOAD(R1, 2, R1) SET(R2, after2) BR(sumTree) LABEL(after2) 13 63 a3 ADD(R4,R3,R4) a2 MOVE(R3,R4) BR(R2)

  3. R1 = 0 // Assume t is in R1 R2 = after1 // Assume return addr in R2 // Return result in R3 R3 = 0 LABEL(sumTree) SUB(R0,R0,R1) R4 = 13 BNZ(not_null) SET(R3, 0) BR(R2) LABEL(not_null) LOAD(R4, 0, R1) Segmentation fault 42 LOAD(R1, 1, R1) a1 SET(R2, after1) BR(sumTree) LABEL(after1) ADD(R4,R3,R4) LOAD(R1, 2, R1) SET(R2, after2) BR(sumTree) LABEL(after2) 13 63 a3 ADD(R4,R3,R4) a2 MOVE(R3,R4) BR(R2)

  4. So… What went wrong?

  5. I assumed my locals were my own

  6. Which registers does sumTree overwrite?

  7. Which registers does sumTree overwrite? R1 R2 R3 R4

  8. So, how do we fix this?

  9. Use the stack

  10. I need to save my registers when I call functions

  11. Note: in HERA, unlike some other processors, the stack grows up Stack (This is different than my i7) … “top” of the stack …

  12. We can use a pointer to hold a reference to the top of the stack This is typically called the stack pointer The stack pointer points at the next available word on the stack

  13. Stack SP , aka R 15 … …

  14. The stack pointer by convention always points at the top of the stack Stack You can change it, nothing stops you It’s just a regular register SP , aka R 15 … …

  15. I want to make myself some space Stack for some local variables SP , aka R 15 …

  16. Stack So how do we do that? SP , aka R 15 …

  17. We increment the stack pointer

  18. ADD(SP, 4, SP) SP/R 15 Stack Stack frame

  19. Stack Frame: portion of stack that holds local variables for single invocation of some function

  20. By convention we use a frame pointer to refer to the base of the frame

  21. SP/R 15 … Stack Local variable 4 Local variable 3 Local variable 2 FP/R 14 Local variable 1

  22. Frame pointer is handy because I can now use LOAD(R d , o, FP) To load from local variable o STORE(R b , o, FP) To store into local variable o

  23. This generalizes to caller-save convention To call a function…

  24. This generalizes to caller-save convention To call a function… Caller passes arguments on the stack

  25. This generalizes to caller-save convention To call a function… Caller passes arguments on the stack Saves registers before call (Also save old SP , FP , and ret. addr)

  26. This generalizes to caller-save convention To call a function… Caller passes arguments on the stack Saves registers before call (Also save old SP , FP , and ret. addr) Result returned on the stack

  27. The Big Rule

  28. If I’m going to change a register, I had better save it first

  29. I have two possibilities: Either the caller saves the registers Args passed in registers (“Caller save”) Or the callee saves the registers Args passed on stack (“Callee save”)

  30. Caller save

  31. Here’s what I do

  32. Let’s say I’m currently using R4 and R5 And foo expects R4/R5 as arguments

  33. R4 23 R5 89

  34. R4 23 R5 89 And my stack looks like this…

  35. R4 23 R5 89 Stack And my stack looks like this… SP , aka R 15 …

  36. R4 23 R5 89 First : save stack pointer in frame pointer Stack SP , aka R 15 …

  37. FP a R4 23 R5 89 Stack MOVE(FP,SP) SP , aka R 15 a …

  38. FP a R4 23 R5 89 Stack Next: increment SP SP , aka R 15 a …

  39. FP a R4 23 R5 89 Stack INC(SP,2) SP , aka R 15 a …

  40. FP a R4 23 R5 89 Stack INC(SP,2) SP , aka R 15 a …

  41. FP a R4 23 R5 89 Stack INC(SP,2) SP , aka R 15 This is my new space a

  42. Next: save R4 and R5 there

  43. FP a R4 23 R5 89 Stack STORE(R4,0,FP) a

Recommend


More recommend