That is, we branch to JUMP_HERE, do whatever we need to do, and then branch to JUMP_BACK, which is where we started.

Similar documents
CIT 593: Intro to Computer Systems Homework #4: Assembly Language Due October 18, 2012, 4:30pm. Name

LC-3 Subroutines and Traps. (Textbook Chapter 9)"

Memory Usage in Programs

LC-3 Instruction Set Architecture. Textbook Chapter 5

Chapter 9 TRAP Routines and Subroutines

Chapter 9 TRAP Routines and Subroutines

Chapter 9 TRAP Routines and Subroutines

appendix a The LC-3 ISA A.1 Overview

System Calls. Chapter 9 TRAP Routines and Subroutines

Fortunately not. In LC-3, there are a variety of addressing modes that deal with these concerns.

LC-3 Instruction Set Architecture

CS 2461: Computer Architecture I

Trap Vector Table. Interrupt Vector Table. Operating System and Supervisor Stack. Available for User Programs. Device Register Addresses

TRAPs and Subroutines. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Introduction to Computer Engineering. CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Midterm 2 Review Chapters 4-16 LC-3

Introduction to Computer. Chapter 5 The LC-3. Instruction Set Architecture

20/08/14. Computer Systems 1. Instruction Processing: FETCH. Instruction Processing: DECODE

Intro. to Computer Architecture Homework 4 CSE 240 Autumn 2005 DUE: Mon. 10 October 2005

Introduction to Computer Engineering. CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison

Subroutines & Traps. Announcements. New due date for assignment 2 5pm Wednesday, 5May

CS 135: Computer Architecture I

LC-3 ISA - II. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 10 February 17, 2011

EEL 5722C Field-Programmable Gate Array Design

Homework 06. Push Z Push Y Pop Y Push X Pop X Push W Push V Pop V Push U Pop U Pop W Pop Z Push T Push S

ECE 109 Sections 602 to 605 Final exam Fall December, 2007

ECE 206, Fall 2001: Lab 3

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Load -- read data from memory to register. Store -- write data from register to memory. Load effective address -- compute address, save in register

Instruction Set Architecture

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Implementing Functions at the Machine Level

LC-3 TRAP Routines. Textbook Chapter 9

Chapter 5 The LC-3. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University 5-2

ECE/CS 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Ch. 5: The LC-3. PC-Relative Addressing Mode. Data Movement Instructions. James Goodman!

Register Files. Single Bus Architecture. Register Files. Single Bus Processor. Term Project: using VHDL. Accumulator based architecture

Computing Layers. Chapter 5 The LC-3

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON. Instructor: Rahul Nayar TAs: Annie Lin, Mohit Verma

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING COMPUTER SCIENCES DEPARTMENT UNIVERSITY OF WISCONSIN-MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING COMPUTER SCIENCES DEPARTMENT UNIVERSITY OF WISCONSIN-MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

Introduction to Computer Engineering. CS/ECE 252, Fall 2016 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison

CS 2461: Computer Architecture I

11/10/2016. Review the Problem to Be Solved. ECE 120: Introduction to Computing. What Shall We Keep in the Registers? Where Are the Pieces in Memory?

Introduction to Computer Engineering. Chapter 5 The LC-3. Instruction Set Architecture

UNIVERSITY OF WISCONSIN MADISON

10/31/2016. The LC-3 ISA Has Three Kinds of Opcodes. ECE 120: Introduction to Computing. ADD and AND Have Two Addressing Modes

Chapter 7 Assembly Language. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University

(Refer Slide Time: 1:40)

University of Illinois at Urbana-Champaign First Midterm Exam, ECE 220 Honors Section

Chapter 10 And, Finally... The Stack. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University

Chapter 7 Assembly Language

ECE/CS 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

COSC121: Computer Systems: Review

The LC-3 Instruction Set Architecture. ISA Overview Operate instructions Data Movement instructions Control Instructions LC-3 data path

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING COMPUTER SCIENCES DEPARTMENT UNIVERSITY OF WISCONSIN-MADISON

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON. Instructor: Rahul Nayar TAs: Mohit Verma, Annie Lin

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Computing Layers

Introduction to Computer Engineering. CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison

Lab 1. Warm-up : discovering the target machine, LC-3

Computing Layers. Chapter 7 Assembly Language

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

COSC121: Computer Systems: Review

ECE 411 Exam 1. This exam has 5 problems. Make sure you have a complete exam before you begin.

SOEN228, Winter Revision 1.2 Date: October 25,

Assembly Language. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

ECE Homework 8

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

UNIVERSITY OF WISCONSIN MADISON

LC-2 Programmer s Reference and User Guide

Introduction to Computer Engineering. CS/ECE 252, Spring 2017 Rahul Nayar Computer Sciences Department University of Wisconsin Madison

1/30/2018. Conventions Provide Implicit Information. ECE 220: Computer Systems & Programming. Arithmetic with Trees is Unambiguous

Chapter 4 The Von Neumann Model

The Assembly Language of the Boz 5

Chapter 6 Programming the LC-3

Chapter 14 Functions. Function. Example of High-Level Structure. Functions in C

Lec-memMapIOtrapJSR SYS_BUS

11/16/2016. Review Our Process for Programming. ECE 120: Introduction to Computing. A Typical Programming Process. Can a Computer Help Us Program?

Microprogrammed Control. ECE 238L μseq 2006 Page 1

LC-3 Assembly Language. (Textbook Chapter 7)"

Syntax of LC-3 assembly: Language elements. Instructions

CS/ECE 252: INTRODUCTION TO COMPUTER ENGINEERING UNIVERSITY OF WISCONSIN MADISON

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

This exam is worth 40 points, or 30% of your total course grade. The exam contains eight

ECE/CS 252 Fall 2011 Homework 7 (25 points) // Due in Lecture on Nov. 30, 2011 (Wed)

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Introduction to Computer Engineering. CS/ECE 252, Fall 2012 Prof. Guri Sohi Computer Sciences Department University of Wisconsin Madison

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Computing Layers

컴퓨터개념및실습. 기말고사 review

11/6/2016. Let s Solve a Problem. ECE 120: Introduction to Computing. Overview of Our Task. Programs are Finite State Machines

MITOCW ocw f99-lec07_300k

Problem Solving in C. Stepwise Refinement. ...but can stop refining at a higher level of abstraction. Same basic constructs. as covered in Chapter 6

Our Simulation Equivalent: -- readmemb( "os.bin" ) -- PC <== x0200

LC-3 Assembly Language

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

Transcription:

CIT 593 Intro to Computer Systems Lecture #11 (10/11/12) Previously we saw the JMP and BR instructions. Both are one-way jumps : there is no way back to the code from where you jumped, so you just go in one direction. What if we wanted to come back to where we had been when we branched? That would be pretty easy: we'd just use two jump or branch statements, like this: JUMP_BACK some instruction the rest of the program BRnzp JUMP_BACK That is, we branch to JUMP_HERE, do whatever we need to do, and then branch to JUMP_BACK, which is where we started. One limitation is that these two jumps are inextricably linked: after we do the stuff at JUMP_HERE, we necessarily go to JUMP_BACK. But what if we wanted to do be able to go-to-and-come-backfrom JUMP_HERE from arbitrary points in the program, not just one? For instance, how could we do this: JUMP_BACK_1 some instruction JUMP_BACK_2 some instruction BRnzp where??? That is, there are two places from which we want to jump to JUMP_HERE, but how does the last instruction know where to jump back to when that part of the program is done? Clearly we need some way of knowing where we came from. That is, when we jump to JUMP_HERE, we have to store the value of the PC (which would be referring to the next instruction after ), and then restore the PC when we want to go back. How can our program get the value of the PC? An easy way is to use the LEA instruction. Remember, LEA puts an address into a register, not the data at that address. And that address is calculated relative

to the current value of the PC. Okay, if we can get the value of the PC, what should we do with it? We can put it in a register. Let's use R7 to do that. So we can modify our program as follows (changes are in bold): LEA R7, JUMP_BACK_1 JUMP_BACK_1 some instruction LEA R7, JUMP_BACK_2 JUMP_BACK_2 some instruction JMP R7 The designers of the LC-3 ISA realized that this sequence of instructions (LEA R7, ADDR; BRnzp SOMEWHERE) was common enough that it should have its own instruction. In this case, the code starting at JUMP_HERE is considered a subroutine and the single instruction that will both store the PC in R7 and then jump down there is JSR (for jump subroutine, I guess?). A subroutine is a piece of code that is separate from the rest of the program but can be used by the program by jumping to that part of the code. In other languages it is referred to as a procedure, function, or method. It often is a piece of code that serves a specific purpose and has well-defined (and well-documented) behavior in terms of its inputs ( operands or arguments ) and outputs. In the code above, we could jump to the subroutine starting at JUMP_HERE by using the instruction JSR JUMP_HERE like this: JSR JUMP_HERE JUMP_BACK_1 some instruction JSR JUMP_HERE JUMP_BACK_2 some instruction JMP R7 The JSR instruction works as follows: put the current value of the PC into R7 (note that the PC will already be pointing to the next

instruction when JSR is executed) calculate the address to which to jump by adding the offset (from the instruction) to the (already incremented) PC set the PC to that address When the subroutine is finished, it must call JMP R7 to go back. In your LC-3 program, you can also use the instruction RET, which is just shorthand for JMP R7. Before we proceed, let's discuss some of the issues with this. You will surely notice that there is an assumption made that R7 will not be modified by the subroutine, otherwise you won't be able to jump back to where you came from (or, if R7 is modified, its value is first written to memory and then read right before calling RET). The other issue is that there is an assumption made that the subroutine will not modify any registers that the calling code is using. This is, of course, much harder to enforce: the author of the subroutine may not know which registers are being used when the subroutine is called. When we start studying C, we will look at how this is addressed in real programming languages. The third issue is that the start of the subroutine must be close enough to the address that is calling JSR so that the offset can fit into nine bits (though there are workarounds that you've seen before). Multiplication example Here's an example of a subroutine to perform multiplication. Since there's no multiply operator in LC- 3, we need to do a sequence of instructions in order to multiply two numbers. If our program does this more than once, it's probably worth creating a subroutine so that the code is only written once and does not have to be repeated every time we want to multiply. Before creating this subroutine, we need to consider: where will the operands go? that is, the numbers we're multiplying? where will the result go? Let's say that our subroutine will expect the operands to be in registers R1 and R2, and that the result will be in R0. Here's a possible implementation of this subroutine: MULTIPLY AND R0, R0, #0 ; initialize R0 to zero LOOP ADD R0, R0, R1 ; add R1 to the running sum ADD R2, R2, #-1; subtract 1 from R2 BRp LOOP ; if positive, keep looping RET ; if we get here, we're done As you can see, this works by adding R1 to itself R2 times, with the result stored in R0. Note that there are a few assumptions made here, specifically that R1 and R2 contain positive numbers, and that we don't have to worry about overflow. Also, as a side effect, R2 is modified; this is generally bad but okay for now. =)

How would this fit into an entire program? Let's say we have memory locations A, B, and C. We want to first calculate B * C, and then put the value in A. Then we want to multiply that new value of A by 5 and put it in C. Our program would look like this (line numbers, of course, are not part of the program):.orig x3000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ;; calculate A = B * C LD R1, B ; populate the first operand LD R2, C ; then the second JSR MULTIPLY ; multiply them ST R0, A ; write the result to A ;; calculate C = A * 5 ADD R1, R0, 0 ; move A from R0 into R1 AND R2, R2, #0 ; set R2 to 5 ADD R2, R2, #5 JSR MULTIPLY ; multiply again ST R0, C ; write the result to C HALT ; here is our multiply subroutine MULTIPLY AND R0, R0, #0 LOOP ADD R0, R0, R1 ADD R2, R2, #-1 BRp LOOP RET.END A few comments: Note that the registers R1 and R2 were not arbitrarily chosen on lines 1 and 2; our multiply subroutine expects the values to multiply to be in those registers. Likewise, on line 4, we know that the product is in R0, so that's why we use it to write to A. On line 5, since we're going to use this new value of A in the second call to the multiply subroutine, we need it in R1 (or R2). We could just load it from memory using LD R1, A, but this is faster. Lines 6 and 7 put the value 5 into R2, of course. Before we wrap this up, consider this: we could have written the above program without using subroutines, but it would be a lot longer. Here is that version, with the original line numbers on the left and the inserted code in bold:

.ORIG x3000 1 2 4 5 6 7 ;; calculate A = B * C LD R1, B ; populate the first operand LD R2, C ; then the second AND R0, R0, #0 LOOP1 ADD R0, R0, R1 ADD R2, R2, #-1 BRp LOOP1 ST R0, A ; write the result to A ;; calculate C = A * 5 ADD R1, R0, 0 ; move A from R0 into R1 AND R2, R2, #0 ; set R2 to 5 ADD R2, R2, #5 AND R0, R0, #0 LOOP2 ADD R0, R0, R1 ADD R2, R2, #-1 BRp LOOP2 9 10 ST R0, C HALT.END ; write the result to C The sequence of instructions is exactly the same, of course, but writing code this way is error-prone (e.g. note that the first loop and second loop have to have different labels) and so using subroutines makes a lot of sense here. Before we move on, let's talk about the encoding of JSR. There are actually two versions of JSR: JSR, which we just discussed and takes an offset as its operand JSRR, in which the address of the start of the subroutine is stored in a register Both instructions have the same opcode (0100). The next bit indicates the mode: 0 means to use an offset; 1 means to use a register. If using an offset, it is represented in the remaining 11 bits. If using a register (for JSRR), the next two bits are zeroes, then three bits for the register that's holding the address of the start of the subroutine, then six zeroes at the end. Traps Sometimes a subroutine may be so common that it should be shared by all programs on the computer, e.g. reading from the keyboard or writing to the display. After all, what's the point of having all LC-3 programmers reimplement those few lines over and over? Such common subroutines can be implemented in the operating system and are known as service routines, or traps. These are subroutines that are available to all programs on the computer (assuming that the operating system has been loaded into memory).

Service routines are not reference by their names because of two important reasons. One is that the operating system usually resides in the very low addresses of memory (starting at x0000), so the start of the subroutine may be too far away from the program. A more important reason is that if you could access a service routine by its name, e.g. IN, then the assembler would have to have IN in its symbol table in order to calculate the offset for JSR; however, the code that is labeled IN is not part of your program (it's part of the operating system), so the assembler wouldn't know about it. This would also prohibit your program from having an instruction labeled IN. So in order to address this issue, LC-3 (and most real microarchitectures) let programmers access service routines by number, instead of by name. For instance, in LC-3 the service routine to read in a single character from the keyboard is trap x23. Then how does it figure out where to jump to? The starting address of each service routine is stored in a trap vector table that resides in the very low memory addresses. There can be up to 256 service routines, and their starting addresses are stored in memory at addresses x0000 through x00ff (that's 256 in total). For instance, when your program wants to read in a single character from the keyboard, it can use the instruction TRAP x23, which works as follows: the current value of the PC is written to R7 (same as JSR) the trap number (or trap vector) is sign-extended to 16 bits; in this case it would be x0023 that address is read (note that it is an address in the trap vector table) to find the starting address of the service routine; in the LC-3 operating system that you can use for your homework, that starting address is x0234 the PC is set to the starting address of the service routine (in this example, x0234) and the program continues Of course, when the service routine ends, it must call RET in order to go back. The opcode for the TRAP instruction is 1111. The next four bits are unused, and the last eight bits represent the trap vector.