CS251 - Computer Organization
Our Assembler and Machine Emulator:
The Assembler program.
The Machine emulator.
Assembling a Program:
Enter the assembly language in a text editor.
java -jar Assembler.jar <assembly code file> <machine code file>
Running a Program:
java -jar Machine.jar <machine code file>
Input/Output in Our Assembly Language:
Our machine supports a simple form of input and output.
This input/output model is called memory mapped I/O.
Input is done by reading from a special memory address.
Output is done by writing to a special memory address.

Input:
Values entered in the Input: box can be read one-by-one.
Each LOAD from the memory address STDIN reads one value.
LOAD R0 STDIN reads one value from the Input: box into R0.
STDIN is short for Standard Input and is defined as a label by our Assembler.
All values must be entered into the Input: box before the program is run.
Output:
Values stored into the special memory address STDOUT appear in the Output: box.
STORE R0 STDOUT writes the value in R0 to the Output: box.
STDOUT is short for Standard Output and is defined by a label by our assembler.
Example:
This program reads 2 values from standard input...
The larger value is then stored in MAX & written to standard output.
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?
Calling and Returning from Methods
The CALL and RET instructions:
R12 is reserved for the return address.
Do not use R12 for any other calculations.
Example:
What are some of the problems with this approach?
Several of them are:
There are no arguments or parameters:
The method MIN always operates on A, B and D.
The state of the registers is not preserved:
The MIN method modifies R0 and R1
The program may have been using R0 and R1 and be counting on their values.
For example:
This does not produce the intended effect because MIN changed R0.
Passing Parameters:
To understand how this works we first need to look at a 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.
Parameter Passing & Return Values
The calling code will PUSH parameters onto the stack.
The method uses the values on the stack to perform its function.
The method code will use a special register (R14) for the return value.
The calling code will retrieve the return value from R14.
PUSH and POP Assembly Language Instructions
R13 is reserved for the Stack Pointer.
Do not use R13 for any other calculations.
NOTICE: PUSH decreases the SP. So the stack fills from the top down.
I.E. In memory, the stack is upside down from the pictures above.
Creating and Using a Stack:
Space must be allocated for the stack.
The Stack pointer (R13) must be initialized.
Passing the arguments:
Each argument is pushed onto the stack.
Using the parameters:
We could POP the parameters but there is a better way.
Stack Addressing Mode:
The MIN routine with parameters:
Register R0 is still modified by MIN.
PUSH R0 to save its value when MIN begins.
POP R0 to restore its value when MIN ends.
The MIN routine with parameters & register preservation:
Special Purpose Registers:
R12: Return Address - PC value for RET
R13: Stack Pointer - Address of top of stack.
R14: Scratch Register - value is not preserved across CALL
R15: Reserved - value is not preserved between instructions.
Related questions and issues:
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 happens to an argument if the corresponding parameter on the stack is changed?
Changing parameters does not change arguments because parameters are copies of the argument.
Consistent with what happens in Java.
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 referred 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!
Procedure Call Protocol:
The following is a general protocol for implementing procedure calls:
Calling Code Perspective:
From the perspective of the calling code, the procedure has simply computed the result and placed it in R14. The values in the general purpose registers (R0-R11) appear not to have changed and the stack has been returned to its original condition. The calling code may pick up right were it left off.
Procedure Perspective:
The int min(int x, int y) Example:
The following code completely implements the min example from above.
Summary of new Assembly Language Instructions: