CS251 - Computer Organization
Consider the following Java Program:
Think about the following questions:
What assembly language might a compiler generate for this?
Do x, y and m hang around during the whole program execution?
Should x, y and m be declared as variables at the start of the program?
Do we want to repeat the assembly language for min every time it is called?
Main Memory Contents When a Program is Running:
The contents of main memory when a program is executing:
Program Instructions - the instructions of the program.
Global Data - Space for .word, .space, .half, .byte assembler directives.
Heap - Used to hold objects created with the new keyword in Java.
Stack - Used to hold method parameters, local variables, return values and return addresses.
The size of the stack and heap change dynamically as the program runs.
Each new operation increases the size of the heap.
Garbage collection of objects with no references to them decreases the size of the heap.
Each method call increases the size of the stack.
Each return from a method decreases the size of the stack.
A Stack:
LIFO - Last In First Out
Two Operations:
PUSH - Put an item onto the top of the stack.
POP - Remove an item from the top of the stack.
High Level View of a Method Call
In general a method call consists of 10 steps:
Applying this to the program from the top of the page:
The stack for each step of the method call to the min method is shown below:
Related questions and issues:
Changing parameters does not change arguments because parameters are on the stack.
Consistent with what happens in Java.
What happens when a method calls another method?
What happens if that method calls yet another method?
What happens when these nested method calls start to return?
What would happen to the stack when a recursive method runs?
What about an argument that is a reference to an array or an object?
Recall, that in Java if the parameter is changed to refer to a new array there is no change to the argument.
However, if the array refered to by the parameter is changed then the array referred to by the argument is also changed.
You'll explore this some more through the homework!
Some New Assembly Language Instructions:
Stacks in Our Assembly Language:
PUSH and POP Assembly Language Instructions:
R13 is reserved by the assembler to point to the first empty space on the stack.
Do not use R13 for any of your own calculations.
PUSH puts the value of a register onto the stack and updates the stack pointer.
POP updates the stack pointer and copies the value from the top of the stack into a register.
NOTE: The stack in our machine will grow from the top down!
Calling and Returning from a Method in Assembly Language:
CALL and RET Assembly Langauge instructions.
R12 is reserved by the assembler to hold return addresses.
Do not use R12 for any of your own calculations.
CALL places the address of the next instruction into R12.
This is used to return to the calling location at the end of the method.
RET uses the address in R12 to return from the method.
Making Space for the Stack:
In our machine we will explicitly allocate space to hold the stack.
We will also need to manually setup the stack pointer.
The following code snippet:
Reserves space for the stack
Sets up the stack pointer.
This is something that will happen at the start of every significant program you write.
The size of 128 will allow for 32 values to be on the stack at the same time.
This size must be customized to your application.
What are the issues involved in selecting a stack size?
Implementing the min example:
The following assembly langauge implements the program from the top of the page.
The Main program is as follows:
Now the minroutine:
Summary of the new LOAD and STORE formats: