Syntax of LC-3 assembly: Language elements. Instructions

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

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

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

Computing Layers. Chapter 7 Assembly Language

Chapter 7 Assembly Language

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

CS 135: Computer Architecture I

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

Chapter 5 - ISA. 1.The Von Neumann Model. The Stored Program Computer. Von Neumann Model. Memory

LC-3 Assembly Language

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

Assembly Language. 7.1 Assembly Language Programming Moving Up o Level. chapter

CENG3420 Lab 2-1: LC-3b Simulator

Midterm 2 Review Chapters 4-16 LC-3

LC-3 Instruction Set Architecture. Textbook Chapter 5

Chapter 6 Programming the LC-3

Instruction Set Architecture

Assembly Language. 7.1 Assembly Language Programming Moving Up o Level. chapter

ECE 206, Fall 2001: Lab 3

appendix a The LC-3 ISA A.1 Overview

LC-3 Instruction Set Architecture

Assembly: Review. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 14

LC-2 Programmer s Reference and User Guide

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

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

Writing ARM Assembly. Steven R. Bagley

Chapter 9 TRAP Routines and Subroutines

System Calls. Chapter 9 TRAP Routines and Subroutines

COSC121: Computer Systems: Review

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

COSC121: Computer Systems: Review

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

CS 2461: Computer Architecture I

Chapter 9 TRAP Routines and Subroutines

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

The Assembly Language of the Boz 5

Compiler, Assembler, and Linker

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

Chapter 9 TRAP Routines and Subroutines

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

UNIVERSITY OF WISCONSIN MADISON

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

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

Virtual machines. Virtual machines. Abstractions for computers. Abstractions for computers. Virtual machines

Lecture 4: Instruction Set Architecture

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

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

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

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

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

2.2 THE MARIE Instruction Set Architecture

EEL 5722C Field-Programmable Gate Array Design

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

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

1 Little Man Computer

Address Modes effective address

Textbook chapter 10. Abstract data structures are. In this section, we will talk about. The array The stack Arithmetic using a stack

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

Extra-credit QUIZ Pipelining -due next time-

Implementing Functions at the Machine Level

Final Project: LC-3 Simulator

Assembly Language Manual for the STACK Computer

Copyright 2000 N. AYDIN. All rights reserved. 1

Programming Using C Homework 5

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

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

COSC 6385 Computer Architecture. Instruction Set Architectures

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

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

Chapter 2: Basic Elements of C++

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Computer Software: Introduction

Practical Malware Analysis

Low-level software. Components Circuits Gates Transistors

The Software Stack: From Assembly Language to Machine Code

LC-3 TRAP Routines. Textbook Chapter 9

Computer Architecture I Midterm I

Notes: The Marie Simulator

Computer Organization CS 206 T Lec# 2: Instruction Sets

Chapter 11 Introduction to Programming in C

Chapter 3. Z80 Instructions & Assembly Language. Von Neumann Architecture. Memory. instructions. program. data

Machine Code and Assemblers November 6

18. Machine Language. Computer Systems. COMP1917: Computing 1. Machine Language Programming. History of Computer Technology

Lec-memMapIOtrapJSR SYS_BUS

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

IT 374 C# and Applications/ IT695 C# Data Structures

We will study the MIPS assembly language as an exemplar of the concept.

Guide to Using the Unix version of the LC-2 Simulator

ELEG3924 Microprocessor

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

Instruction Sets: Characteristics and Functions Addressing Modes

ELEG3923 Microprocessor Ch.2 Assembly Language Programming

MIPS (SPIM) Assembler Syntax

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

QUIZ. Name all the 4 parts of the fetch-execute cycle.

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?

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

Transcription:

LC-3 Assembly Language (Textbook Chapter 7) Assembly and assembler Machine language - binary 0001110010000110 Assembly language - symbolic ADD R6, R2, R6 ; increment index reg. Assembler is a program that turns symbols into machine instructions. ISA-specific: close correspondence between symbols and instruction set mnemonics for opcodes labels for memory locations 2 1

Syntax of LC-3 assembly: Language elements Instructions (we have seen most of them) Comments Labels Declarations Assembler directives and trap codes Whitespaces (between symbols) and case are ignored. 3 Instructions One instruction or declaration per line LABEL OPCODE OPERANDS ; COMMENTS optional mandatory 4 2

Opcodes and Operands Opcodes reserved symbols that correspond to LC-3 instructions listed in Appendix A (ex: ADD, AND, ) Operands Registers: Rn, where n is the register number Immediate numbers: # (decimal), x (hex), or b (binary) Labels: symbolic names of memory locations Operands are separated by spaces, tabs, or commas Their number, order, and type correspond to the instruction format 5 Data types LC-3 has 2 basic data types Integer Character Both are 16 bits wide (a word), though a character is only 8 bits in size. 6 3

Comments Anything on a line after a semicolon is a comment Comments are ignored by assembler Used by humans to document/understand programs Tips for useful comments: avoid restating the obvious, as decrement R1 provide additional insight, as in accumulate product in R6 use comments to separate pieces of program 7 Labels Placed at beginning of line Assign a symbolic name to their line (its address) Symbolic names used to identify memory locations. Two kinds: Location of target of a branch or jump Location of a variable for loading and storing Can be 1-20 characters in size 8 4

Assembler directives Directives give information to the assembler. Not executed by the program All directives start with a period. Directive.ORIG.FILL.BLKW.STRINGZ.END Description Where to start in placing things in memory Declare a memory location (variable) Declare a group of memory locations (array) Declare a group of characters in memory (string) Tells assembly where your program source ends 9.ORIG Tells simulator where to put your code in memory (starting location) Only one.orig allowed per program module PC is set to this address at start up Similar to the main() function in C Example: the standard convention is.orig x3000 10 5

.FILL Declaration and initialization of variables One declaration per line Always declaring words Examples: flag.fill x0001 counter.fill x0002 letter.fill x0041 letters.fill x4241 11 In C type varname;.fill Where type is int (integer) char (character) float (floating-point) varname In LC-3.FILL value value is required (initialize) type is only 16-bit integer 12 6

.BLKW Reserves (and initializes) a sequence of contiguous memory locations (arrays) Examples: ;set aside 3 locations.blkw 3 ;set aside 1 location and label it Bob.BLKW 1 ; set aside 7 locations, ; label them, and init them all to 4 Num.BLKW 7 #4 13.STRINGZ Declare a string of characters Automatically terminated with x0000 Example: hello.stringz Hello World! 14 7

.END Tells the assembler where your program ends Only one.end allowed in your program module That s where the assembler stops assembling, NOT where the execution stops! 15 TRAP (System Calls) Very tedious and dangerous for a programmer to deal with I/O. This is why we like to have an OS. Need an instruction to get its attention. Use the TRAP instruction and a trap vector. 16 8

Trap Service Routines The LC-3 assembler provides pseudo-instructions for each trap code, so you don t have to remember them. Trap Vector 0x20 0x21 0x22 0x23 0x24 0x25 Assembler Name GETC OUT PUTS IN PUTSP HALT Usage & Result Read a character from console into R0, not echoed. Write the character in R0[7:0] to console. Write string of characters to console. Start with character at address contained in R0. Stops when 0x0000 is encountered. Print a prompt to console and read in a single character into R0. Character is echoed. Write a string of characters to console, 2 characters per address location. Start with characters at address in R0. First [7:0] and then [15:0]. Stops when 0x0000 is encountered. Halt execution and print message to console. 17 To print a character ; the char must be in R0[7:0] TRAP x21 or OUT Trap Examples To read in a character ; will go into R0[7:0], ; no echo. TRAP x20 or GETC To end the program TRAP x25 or HALT 18 9

.ORIG x3000 LD R2, Zero LD R0, M0 LD R1, M1 Loop BRz Done ADD R2, R2, R0 ADD R1, R1, -1 BR Loop Done ST R2, Res HALT Res.FILL x0000 Zero.FILL x0000 M0.FILL x0007 M1.FILL x0003.end Simple LC-3 program What does this program do? What is in Res at the end? 19 The assembly process Convert assembly language file (.asm) into an executable file (.obj) for the LC-3 simulator. First Pass: scan program file find all labels and calculate the corresponding addresses - the symbol table Second Pass: convert instructions to machine language, using information from symbol table 20 10

The assembly process First Pass: The Symbol Table 1. Find the.orig statement, which tells us the address of the first instruction. Initialize Location Counter (LC), which keeps track of the current instruction. 2. For each non-empty line in the program: a) If line contains a label, add label and LC to symbol table. b) Increment LC. NOTE: If statement is.blkw or.stringz, increment LC by the number of words allocated. 3. Stop when.end statement is reached. NOTE: A line with only a comment is considered an empty line. 21 The assembly process Practice: Symbol Table Build the symbol table for the multiply program: Symbol Address x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 x300a x300b x300c.orig x3000 LD R2, Zero LD R0, M0 LD R1, M1 ; begin multiply Loop BRz Done ADD R2, R2, R0 ADD R1, R1, #-1 BR Loop ; end multiply Done ST R2, Result HALT Result.FILL x0000 Zero.FILL x0000 M0.FILL x0007 M1.FILL x0003.end 22 11

The assembly process 2nd Pass: Generating Machine Language For each executable assembly language statement, generate the corresponding machine language instruction. If operand is a label, look up the address from the symbol table. Potential problems: Improper number or type of arguments ex: NOT R1,#7 ADD R1,R2 Immediate argument too large ex: ADD R1,R2,#1023 Address (associated with label) more than 256 from instruction can t use PC-relative addressing mode 23 The assembly process The LC-3 Assembler Using assemble (Unix) or LC3Edit (Windows), generates several different output files. 24 12

The assembly process Multiple Object Files An object file is not necessarily a complete program. system-provided library routines code blocks written by multiple developers For LC-3 simulator, can load multiple object files into memory, then start executing at a desired address. system routines, such as keyboard input, are loaded automatically loaded into system memory, below x3000 user code should be loaded between x3000 and xfdff each object file includes a starting address be careful not to load overlapping object files 25 The assembly process Linking Linking is the process of resolving symbols between independent object files. Suppose we define a symbol in one module, and want to use it in another The directive.external is used to tell the assembler that a symbol is defined in another module The linker will search symbol tables of other modules to resolve symbols and complete code generation before loading 26 13

The assembly process Loading Loading is the process of copying an executable image into memory. more sophisticated loaders are able to relocate images to fit into available memory must readjust branch targets, load/store addresses 27 Program Execution Running The loader makes the CPU jump to the first instruction ->.ORIG. The program executes When execution completes, control returns to the OS or to the simulator Load again to run again with different data (in LC3 we must assemble again, since data is in program) 28 14

Recommended exercises: Ex 7.1 to 7.11 Especially recommended: 7.12 to 7.16, and 7.18 to 7.25 (yes, all of them except 7.17) 29 15