Navigation


 



Assignment 1 - Questions

Questions


  1. What is a parameter, where would it be used?

      Parameters are varibles that can be used in functions.They transfeer data to a function allowing the function to carry out a specific task. It works like a local variable with the difference that it is already
      declared. This data can be returned at the end of the function. Almost in every subroutine at least one parameter is sent. Sub-routines are normally used to be clearer, otherwise if
      all code in the main routine it would be very difficult to read and understand.

  2. What is the advantage over alternative methods?

      Apart from sending a parameter to a function, we could declare every single parameter inside the function or
      we could use global variables or by passing in different registers before calling the function.


      1. The first case is the worst ever, because in a function the maximum value of return values is one, meaning
        that if we want one function to return two variables we would have to do the same function twice,
        except for the return value, which would have to be different.

      2. The second one is a dangerous option mainly when someone is programming with a team because anyone can change
        its value and the whole program may fall apart. It is also not recommended because it is just declared the functions and,
        therefore it is harder to understand and find where the variable has been declared (less visibility).

      3. The last option is the best between the three given, but even so is worse than sending parameters to a function.
        Since there is a limit number of register, the number of variables that can be sent to a function is less then sending through the
        Stack as parameters. Sending through registers also reduces the maximum size of each variable and, therefore, the best and simplest
        option is by sending variables through the Stack as parameters.
      4. Passing data by parameters also has an advantage, which consists of making a subroutine re-entrant. This means that a subroutine
        can be called at any time and can also be called by itself.
        Comparing this to the other methods:

        - using global variables, it would also be re-entrant, but with the risk of changing the value of its variables and, again, it would
        be harder to understand the code;
        - having just local variables would make the subroutine be non re-entrant and inflexible for possible use in the wider project;
        - passing through registers has one big problem, which is when the sub-routine calls itself, all the previous data will be overwritten,
        meaning that the subroutine is not re-entrant.

  3. What is a Stack Frame?

      Stack Frames are blocks of memory on the stack. Parameters are pushed onto the stack in reverse or when calling a function in assembler.

  4. How is the Base Frame Pointer used?

      Base Frame Pointer, shortly known as BP, is the pointer to the 32-bit (sometimes can get up to 64-bit like in AMD64) register EBP, also known as Frame. Its main purpose is to allow access to the stack variables, by adding a number to itself, since the STACK grows downwards.

      To access this pointer (EBP) the compiler has to set up in the function prologue, which depends on the processor in use. After this it just needs to add or subtract an offset (one integer value) to EBP to get a different address on the Stack.


  5. Is there any similarity between Local Variables and function Parameters?

      The local variables and the function parameters are used similarly inside one function, been possible to access and change its value (or address) in the same way. The only difference is that a function parameter
      is already declared in another function and therefore can have a previous value, while a local variable has to be declared inside the function and can just be initialized after being declared.

      Normally, local variables are used as temporary variables which will not return to the calling function, while the function parameters return to the calling function after being used inside the function. This last option
      can be called by value or reference. In the first case, the variable gets the same value before being called, while, in the second case, if the variable is changed inside the function, it will be different than previously
      being called.

      Both are saved inside the Stack Frame, when a function is called, but the parameters are saved before the return address and the local variables are after it. Both can be access either by through the base frame
      pointer or by using the instruction POP, which removes the last variable of the Stack and saves it into EAX.