addressing modes procedure calls and the stack frame
play

Addressing modes, Procedure calls and the Stack Frame Eric McCreath - PowerPoint PPT Presentation

Addressing modes, Procedure calls and the Stack Frame Eric McCreath Indirect load/store Indirect load (or store) uses a register to determine the address to load data from (or store data to) main memory. 0x0100 : load #varstr R2


  1. Addressing modes, Procedure calls and the Stack Frame��� Eric McCreath

  2. Indirect load/store Indirect load (or store) uses a register to determine the address to load data from (or store data to) main memory. 0x0100 : load #varstr R2 ; char str[] = "Hello"; ; char *ptr; ; ptr = str; store R2 varptr jump whilebool ; while (*ptr != 0) { loop : load varptr R3 ; printf("%c�",*ptr); load R3 R4 store R4 0xfff0 load #'�' R4 store R4 0xfff0 load varptr R3 ; ptr++; load #1 R5 add R3 R5 R3 store R3 varptr whilebool: load varptr R1 ; } load R1 R2 jumpnz R2 loop halt varptr : block 1 varstr : block #"Hello" 2

  3. Base +Displacement load/store base+displacement load (store) - this adds a value from the operand to a register to give an address, which is used as the address to load from (or store to). ; char str[] = "Hello"; 0x0100 : store ZERO varpos ; int pos = 0; jump whilebool ; while (str[pos] != 0) { loop : load varpos R3 ; printf("%c�",str[pos]); load R3 #varstr R4 store R4 0xfff0 load #'�' R4 store R4 0xfff0 load varpos R3 ; ptr++; add R3 ONE R3 store R3 varpos whilebool: load varpos R1 load R1 #varstr R2 jumpnz R2 loop ; } halt varpos : block 1 varstr : block #"Hello" 3

  4. Tracking the PC 4

  5. The Stack Frame During the execution of a function/procedure/method the executing code will need to be able to access the following data: Return values (sometimes), Parameters (sometimes), Return address, Local variables (sometimes). This information could be stored in a number of places including: registers, as global variables in main memory, or in some dynamically allocated memory (the first two of these would not work with recursive calls). However, generally a stack is used for storing this information (works with recursion and provides a simple way of allocating memory). 5

  6. The Stack Frame A 'stack frame' is all the information associated with the execution of a procedure. The 'stack frame' is located on the 'stack'. The 'stack' is made up of stack of 'stack frames'. The order of data in the stack frame is determined by a convention. The data and order within stack frames is known by both the calling code and the code of the function. The calling code will create part of the new stack frame, once the procedure completes the part of the stack frame the calling code created is removed via the calling code. 6

  7. Stack in rPeANUt A stack frame for rPeANUt is constructed using the following order to push elements onto the stack (by convention) : Return value (if there is one) - added by calling code, Parameters (0 or more of them) - added by calling code, Return address - added using the 'call' instruction, Local variables - added by the procedure code. Top of stack local variable return address parameter 2 Current stack frame parameter 1 return value Stack frame of the calling function 7

  8. Calling a function Calling code will: Use the 'push' instruction to add a spot for the return value (if there is one). Use the 'push' instruction to add the parameters for the call. Use the 'call' instruction (this adds to the stack the return address). Once returned, remove the parameters that had been added to the stack (using 'pop'). Remove the return value if one was added (using 'pop'). This example is if you are calling the "int getc()" method. push R0 ; make a spot on the stack for the return value call getc ; call the function pop R0 ; remove the returned value from the stack ; With stacks it is always important to leave them how you find them. ; i.e. pushes should be matched with pops 8

  9. The Function Code Procedure code will: Add to the stack required local variables (using 'push'). Execute the body of the functions code. During the methods execution load/store from/to the stack frame using the 'base+displacement' addressing mode (the base is the SP register or a copy of it made at the beginning of the call). Take care to get the correct location relative to the top of the stack. Remove the local variables (using 'pop'). Return from the method using the 'return' instruction (removes the return address from the stack). getc : load 0xFFF1 R2 jumpz R2 getc load 0xFFF0 R2 store R2 #-1 SP return 9

  10. Function Call Example 10

  11. String Length Example Given the code: int strlen(char *str) { int res = 0; while (str[res] != 0) { res++; } return res; } int main() { int len; char hiStr[] = "Hello"; len = strlen(hiStr); } 11

  12. String Length Example Calling code turns into: 0x0100 : push R0 ; add a spot for the return variable load #str R1 ; add the address of the string push R1 call strlen ; call the function pop R1 ; remove the address of the string pop R0 ; R0 will have the string length in it halt str : block #"Hello" 12

  13. String Length Example Function code turns into: ; strlen stack frame: ; resvar : #0 ; return address : #-1 ; strvar : #-2 ; resultvar : #-3 strlen : load #0 R0 ; res = 0; push R0 ; // add the local var to stack jump strlenwb ; while(str[res] != 0) { strlenwc: load SP #0 R0 ; res++; add ONE R0 R0 store R0 #0 SP strlenwb: load SP #-2 R0 load SP #0 R1 add R0 R1 R2 load R2 R3 jumpnz R3 strlenwc ; } load SP #0 R0 ; return res store R0 #-3 SP pop R0 ; remove the local var from stack return 13

  14. The Stack Frame Pointer Using a stack frame for function calls, it becomes difficult to keep track of the location of variables on the stack. This is because as you call other functions within a function you end up moving the top of the stack. Hence, the relative location of the variables within a function changes. 14

  15. The Stack Frame Pointer One approach to help with this is to make use of another register called the "stack frame pointer". This points to a fixed location within the stack frame. This pointer can be used for accessing local variables, parameters, and the return value's location. x86 code will normally do this with the "ebp" register ("ebp" is the stack frame pointer register, "esp" is the stack pointer register) , so a function will first save the previous "ebp" on the stack and then point the "ebp" to the current stack location: push ebp ; push calling function's stack frame location onto the stack mov ebp, esp ; make the current stack frame location point ; to the top of the stack Variables on the stack can be accessed relative to "ebp". With this approach you end up creating a linked list of stack frames. This linked list goes down the stack. 15

  16. Exercises Using the standard rPeANUt stack frame approach (that is the one given in these slides). Implement the below code: int add(int a, int b) { int res; res = a + b; return res; } int sum; int main() { sum = add(5,7); return 0; } 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend