Computer Science 210 Computer Organization

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Computer Science 210 Computer Organization Strings, I/O, and Trap Service Routines.
The University of Adelaide, School of Computer Science
Introduction to Computer Engineering ECE 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin –
1 System Calls (TRAPS) and Subroutines Patt and Patel Ch. 9.
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
Computer Science 210 Computer Organization Recursive Subroutines System Stack Management.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
S. Barua – CPSC 240 CHAPTER 9 TRAP ROUTINES AND SUBROUTINES The TRAP mechanism allows the user program.
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
Computer Science 210 Computer Organization Introduction to Subroutines.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Chapter 9 TRAP Routines and Subroutines. 9-2 System Calls Certain operations require specialized knowledge and protection: specific knowledge of I/O device.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Science 210 Computer Organization Machine Language Instructions: Control.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Computer Science 210 Computer Organization
Computer Architecture & Operations I
Computer Science 210 Computer Organization
Introduction to Computer Engineering
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
HKN ECE 220: Fall 2017 Midterm 1 AJ Schroeder, Evan Lissoos, Utsav Kawrani 23rd September, 2017.
Computer Science 210 Computer Organization
COSC121: Computer Systems: LC3 Traps and Subroutines
Procedures (Functions)
Functions and Procedures
Chapter 7 Subroutines Dr. A.P. Preethy
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Introduction to Computer Engineering
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
HKN ECE 220: Spring 2018 Midterm 1
Computer Science 210 Computer Organization
Computer Science 210 Computer Organization
The University of Adelaide, School of Computer Science
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
Introduction to Computer Engineering
Computer Science 210 Computer Organization
Chapter 9 TRAP Routines and Subroutines
Computer Science 210 Computer Organization
TRAP Routines Privileged Instructions Subroutines
Chapter 9 TRAP Routines and Subroutines
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Computer Science 210 Computer Organization
Introduction to Computer Engineering
Chapter 9 TRAP Routines and Subroutines
Chapter 9 TRAP Routines and Subroutines
Computer Organization and Assembly Language
Introduction to Computer Engineering
Introduction to Computer Engineering
Introduction to Computer Engineering
Introduction to Computer Engineering
Chapter 9 TRAP Routines and Subroutines
Presentation transcript:

Computer Science 210 Computer Organization Managing the Namespace Parameters and Local Data

Example: Subtraction ;; Author: Ken Lambert ;; This program subtracts the number in the variable SECOND from FIRST and ;; and stores the result in DIFF .ORIG x3000 ;; Pseudocode design: diff = first - second ;; Main program register usage: ; R1 = first ; R2 = second ; R3 = diff ; Main program code LD R1, FIRST ; Set the parameter for the minuend LD R2, SECOND ; Set the parameter for the subtrahend JSR SUB ST R3, DIFF ; Store the returned difference HALT ; Main program data variables FIRST .BLKW 1 SECOND .BLKW 1 DIFF .BLKW 1 ;; Subroutine SUB ; Subtracts R2 from R1 and stores result in R3 ; Input parameters: R1 (minuend) and R2 (subtrahend) ; Output parameter: R3 (difference) SUB NOT R3, R2 ADD R3, R3, #1 ADD R3, R1, R3 RET .END Note that the SUB routine does not modify its input parameters!

The Namespace A subroutine should communicate with its caller only via registers A subroutine may have its own data variables, but should not access anyone else’s data variables (those of other routines or the main program) A subroutine should leave its input registers unchanged (may save and restore)

Saving and Restoring Registers Called routine -- “callee-save” At startup, save any registers that will be altered (unless altered value is desired by calling program!) Before return, restore those same registers Calling routine -- “caller-save” Save registers destroyed by own instructions or by called routines (if known), if values needed later save R7 before TRAP save R0 before TRAP x23 (input character) Or avoid using those registers altogether

Saving and Restoring Registers Generally use “callee-save” strategy, except for return values. Save anything that the subroutine will alter internally that shouldn’t be visible when the subroutine returns. It’s good practice to restore incoming arguments to their original values (unless overwritten by return value). Remember: You MUST save R7 if you call any other subroutine or TRAP.

Example: ORDER makes R1 <= R2 Is there a bug here? ;; Author: Ken Lambert ;; Calls the subroutine ORDER to guarantee that FIRST <= SECOND .ORIG x3000 ;; Main program register usage: ; R1 = initial value of FIRST ; R2 = initial value of SECOND ; Main program code LD R1, FIRST LD R2, SECOND JSR ORDER ST R1, FIRST ST R2, SECOND HALT ; Main program data FIRST .BLKW 1 SECOND .BLKW 1 ;; Subroutine ORDER ; Guarantees that R1 <= R2 ; Input parameters: R1 and R2 ; Output parameters: R1 and R2 ; R3 = temporary working storage ORDER ST R3, ORDERR3 ; Save R3 JSR SUB BRnz ENDORD ; Exit if difference <= 0 ADD R3, R2, #0 ; Swap values in R1 and R2 ADD R2, R1, #0 ADD R1, R3, #0 ENDORD LD R3, ORDERR3 ; Restore R3 RET ; Data variable for subroutine ORDER ORDERR3 .BLKW 1 ;; Subroutine SUB . . . Example: ORDER makes R1 <= R2 Is there a bug here?

Example: ORDER makes R1 <= R2 Back up ret address ;; Author: Ken Lambert ;; Calls the subroutine ORDER to guarantee that FIRST <= SECOND .ORIG x3000 ;; Main program register usage: ; R1 = initial value of FIRST ; R2 = initial value of SECOND ; Main program code LD R1, FIRST LD R2, SECOND JSR ORDER ST R1, FIRST ST R2, SECOND HALT ; Main program data FIRST .BLKW 1 SECOND .BLKW 1 ;; Subroutine ORDER ; Guarantees that R1 <= R2 ; Input parameters: R1 and R2 ; Output parameters: R1 and R2 ; R3 = temporary working storage ORDER ST R3, ORDERR3 ; Save R3 ST R7, ORDERR7 ; Save R7 JSR SUB BRnz ENDORD ; Exit if difference <= 0 ADD R3, R2, #0 ; Swap values in R1 and R2 ADD R2, R1, #0 ADD R1, R3, #0 ENDORD LD R3, ORDERR3 ; Restore R3 LD R7, ORDERR7 ; Restore R7 RET ; Data variables for subroutine ORDER ORDERR3 .BLKW 1 ORDERR7 .BLKW 1 ;; Subroutine SUB . . . Example: ORDER makes R1 <= R2 Back up ret address before another call

Rules of Thumb for Namespace Each program component owns its own labels (variables and instructions) Each subroutine utilizes caller save for R7 and callee save for all other registers Most parameters are input-only

Implementing a runtime stack For Wednesday Implementing a runtime stack