Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.

Slides:



Advertisements
Similar presentations
Introduction to Computer Engineering ECE 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin –
Advertisements

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.
EECC250 - Shaaban #1 Lec # 6 Winter Stack-Related Instructions PEA Push Effective Address Calculates an effective address and pushes it.
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
Computer Organization and Architecture
Processor System Architecture
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Chapter 7 Interupts DMA Channels Context Switching.
Computer System Organization S H Srinivasan
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
Midterm Tuesday October 23 Covers Chapters 3 through 6 - Buses, Clocks, Timing, Edge Triggering, Level Triggering - Cache Memory Systems - Internal Memory.
EECC250 - Shaaban #1 lec #7 Winter Local workspace of a subroutine: A number of temporary memory locations required by the subroutine for temporary.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Review Web Page (
Chapters 4 & 5: LC-3 Computer Architecture Machine Instructions Assembly language Programming in Machine and Assembly Language.
Midterm Wednesday 11/19 Overview: 25% First Midterm material - Number/character representation and conversion, number arithmetic - DeMorgan’s Law, Combinational.
TCSS 372A Computer Architecture. Getting Started Get acquainted (take pictures) Purpose, scope, and expectations of the course Expectations & strategy.
CEG 320/520: Computer Organization and Assembly Language Programming1 CEG 320/520 Computer Organization and Assembly Language Programming.
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
9/20/6Lecture 2 - Prog Model Architecture, Data Types and Addressing Modes.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
by Richard P. Paul, 2nd edition, 2000.
Processor Organization
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
1 Basic Processor Architecture. 2 Building Blocks of Processor Systems CPU.
Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
EET 2261 Unit 6 The Stack; Subroutines
Modularity Most useful abstractions an OS wants to offer can’t be directly realized by hardware Modularity is one technique the OS uses to provide better.
Computer Science 210 Computer Organization
MIPS Assembly Language Programming
Figure 8.1 of course package
Computer Architecture
Subroutines … passing data
Chapter 10 And, Finally... The Stack
Stack Frames Stack frame = block of memory located in the system stack that contains: return address input parameters (from calling program to subroutine)
Arithmetic using a stack
Lecture 3 - Instruction Set - Al
6.001 SICP Stacks and recursion
Number Representations and Basic Processor Architecture
Application Binary Interface (ABI)
Passing Parameters Data passed to a subroutine is called a parameter.
Chapter 9 TRAP Routines and Subroutines
by Richard P. Paul, 2nd edition, 2000.
Self-modifying Code program intentionally modifies code in the program by overwriting machine code may be used by code designers to obscure branch addresses.
68000 Architecture, Data Types and Addressing Modes
; main program ; main move Param2, -(sp) move Param1, -(sp) Call Sub1
Figure 8.1 of course package
The MOVE Multiple: MOVEM Instruction
Chapter 10 And, Finally... The Stack
TRAP Routines Privileged Instructions Subroutines
Subroutines … passing data
Basic Instruction Cycle
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
EET 2261 Unit 6 The Stack; Subroutines
Some Assembly (Part 2) set.html.
Where is all the knowledge we lost with information? T. S. Eliot
CPU Structure and Function
Mechanism: Address Translation
Lecture 3 - Instruction Set - Al
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ECE511: Digital System & Microprocessor
MIPS R3000 Subroutine Calls and Stack
Lecture 3 - Instruction Set - Al
Presentation transcript:

Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with the main routine less than 100 lines long Today’s objective: introduction to really simple subroutines to simplify program structure for I/O use JSR, jump to subroutine, to call subroutine use RTS, return from subroutine, to return from subroutine

Subroutines …sample structure ORG $1000 MAIN ….  main program LEA msg1,A1 ; point to string JSR PRT_S ; jump to subroutine PRT_S … LEA msg2,A1 ; point to string STOP #$2700 PRT_S …  subroutine PRT_S RTS ; return from subroutine msg1 …  data area msg2 … END MAIN

Subroutines … calling the subroutine Addressing Range jump to subroutine unlimited JSR <ea> e.g. JSR MAXIMUM → normally code using JSR* branch to subroutine limited BSR <ea> e.g. BSR MINIMUM → smaller machine code BSR = BSR.W BSR.S = BSR.B *once program complete, can use BSR to tighten code

Subroutines … key registers PC = program counter contains the address of the next instruction to be executed how the processor tracks where you are in the program (simplified): processor sends address in PC to memory while memory is retrieving the instruction, PC is incremented to point to next instruction memory returns the instruction processor executes the instruction

Subroutines … key registers SP = stack pointer ≡ A7 A7 is not used as a regular address register A7 is used as a system stack pointer If you are in supervisor mode, A7 = SSP (supervisor stack pointer) use USP to access the user stack pointer If you are in user mode, A7 = USP (user stack pointer) SSP not visible USP and SSP are physically different registers.

Subroutines JSR/BSR label Operation: memory - stack memory - program JSR label JSR/BSR label Operation: save PC (program counter) on the stack - i.e. save the address of the next instruction - why? execution continues at specified address In register transfer language (rtl): (SP) ← (SP) – 4 ((SP)) ← (PC) (PC) ← address

Subroutines Returning from subroutine: Return from subroutine RTS → get PC from stack & return → used most of the time Return & restore condition codes RTR → get condition codes from stack → get PC from stack & return How did the condition codes get on the stack? → programmer put them on stack in the subroutine in rtl: (PC) ← ((SP)) (SP) ← (SP) + 4 in rtl: (CCR) ← ((SP)) (SP) ← (SP) + 2 (PC) ← ((SP)) (SP) ← (SP) + 4

Subroutines start CLR.B D0 … JSR max MOVE.W D0,D1 … STOP #$2700 max … ORG $400 start CLR.B D0 … JSR max MOVE.W D0,D1 … STOP #$2700 max … RTS data area END start stack grows _______ in memory stack pointer points to Memory Map: 0000 03FF 0400 5000

Defining the system stack -> assumes no operating system Memory map #1 0000 … 03FF 0400 ???? Memory map #2 0000 … 03FF 0400 ????

Defining the system stack If there is an operating system, the operating system will initialize the stack pointer. If there is a monitor program, check the SSP/USP registers. for EASy68K, SSP = SS = $01000000 USP = US = $00FF0000 for Teesside, SSP = SS = $00A00000 If there is no monitor program or if the monitor program does not initialize the SSP/USP, it is the programmer’s responsibility to initialize the SSP/USP in the 1st line of the program. if stack is at $10000, use LEA $10000,A7 or LEA $10000,SP

Subroutines …saving/restoring context programmer’s responsibility to save registers when entering the S/R and to restore registers when exiting the S/R save registers that ______________________ do not save registers that ________________ save/restore registers with move multiple MOVEM.L register list,-(SP) store order A7 -> A0; e.g. MOVEM.L D0-D3/A2-A5,-(SP) D7 -> D0 MOVEM.L (SP)+,register list restore as D0 -> D7; e.g. MOVEM.L (SP)+,D0-D3/A2-A5 A0 -> A7

Subroutines …saving context e.g. ORG $400 main LEA $10000,A7 ;initialize SP  not … ;necessary for Easy68K JSR find_min MOVE.L D0,D1 … STOP #$2700 find_min ;save registers ;restore registers RTS … data here … END main

Reading, Expectations: M68000 Assembly Language [pdf, 92p; N. Znotinas] review operation of JSR, BSR, RTS review operation of MOVEM Run Stack_mem1.x68 and Stack_mem2.x68 under the Easy68K simulator. To demonstrate the stack behaviour, I have loaded values into some of the registers. The mem1/mem2 examples use different memory mapping techniques. Single step through the programs. Make sure you understand how the stack is working. Use the view stack feature so you can see the stack and the program simultaneously. Can you see the stack and all of its entries? Can you see the program? For mem2, use the view memory feature to verify that both the program and the stack are located at $8000. Expectations: you can use JSR, BSR, RTS, MOVEM correctly you can write subroutines correctly you can explain the stack structure for a program with multiple subroutines using diagrams to illustrate stack contents