LC-3 Assembly Language



Similar documents
Chapter 7 Assembly Language

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Notes on Assembly Language

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Instruction Set Architecture (ISA)

ASSEMBLY LANGUAGE PROGRAMMING (6800) (R. Horvath, Introduction to Microprocessors, Chapter 6)

PROBLEMS (Cap. 4 - Istruzioni macchina)

1 Classical Universal Computer 3

Chapter 2: Elements of Java

Instruction Set Design

Chapter 2 Assemblers

CPU Organization and Assembly Language

1 Description of The Simpletron

The Little Man Computer

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

MACHINE ARCHITECTURE & LANGUAGE

Informatica e Sistemi in Tempo Reale

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.

The programming language C. sws1 1

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

MICROPROCESSOR AND MICROCOMPUTER BASICS

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

EE361: Digital Computer Organization Course Syllabus

2) Write in detail the issues in the design of code generator.

Summary of the MARIE Assembly Language

CSC 2405: Computer Systems II

In this Chapter you ll learn:

UNIVERSITY OF CALIFORNIA, DAVIS Department of Electrical and Computer Engineering. EEC180B Lab 7: MISP Processor Design Spring 1995

High level code and machine code

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

BASIC COMPUTER ORGANIZATION AND DESIGN

OAMulator. Online One Address Machine emulator and OAMPL compiler.

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

PCSpim Tutorial. Nathan Goulding-Hotta v0.1

Z80 Instruction Set. Z80 Assembly Language

Figure 1: Graphical example of a mergesort 1.

Instruction Set Architecture

Assembly Language Programming

Syscall 5. Erik Jonsson School of Engineering and Computer Science. The University of Texas at Dallas

Chapter 7D The Java Virtual Machine

(Refer Slide Time: 00:01:16 min)

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Number Representation

Central Processing Unit (CPU)

Chapter 5 Instructor's Manual

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Introduction. What is an Operating System?

Floating Point C Compiler: Tips and Tricks Part I

Positional Numbering System

Base Conversion written by Cathy Saxton

CSE 141L Computer Architecture Lab Fall Lecture 2

Lecture 3 Addressing Modes, Instruction Samples, Machine Code, Instruction Execution Cycle

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

Keil C51 Cross Compiler

CSC4510 AUTOMATA 2.1 Finite Automata: Examples and D efinitions Definitions

Sources: On the Web: Slides will be available on:

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

Traditional IBM Mainframe Operating Principles

An Introduction to MPLAB Integrated Development Environment

Lecture 27 C and Assembly

Introduction to Python

PART B QUESTIONS AND ANSWERS UNIT I

CHAPTER 7: The CPU and Memory

C++ INTERVIEW QUESTIONS

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

Using C to Access Data Stored in Program Memory on the TMS320C54x DSP

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

Numbering Systems. InThisAppendix...

MIPS Assembly Code Layout

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

The C Programming Language course syllabus associate level

Here is a diagram of a simple computer system: (this diagram will be the one needed for exams) CPU. cache

An Introduction to the ARM 7 Architecture

İSTANBUL AYDIN UNIVERSITY

Administrative Issues

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Java Interview Questions and Answers

Microprocessor & Assembly Language

USB Card Reader Configuration Utility. User Manual. Draft!

Computer Science 281 Binary and Hexadecimal Review

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

2011, The McGraw-Hill Companies, Inc. Chapter 3

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

CPU Organisation and Operation

The 104 Duke_ACC Machine

Lab Experience 17. Programming Language Translation

IA-64 Application Developer s Architecture Guide

Embedded Software development Process and Tools: Lesson-4 Linking and Locating Software

1 Abstract Data Types Information Hiding

Java Basics: Data Types, Variables, and Loops

arrays C Programming Language - Arrays

MACHINE INSTRUCTIONS AND PROGRAMS

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 OUTCOME 3 PART 1

Chapter 6: Programming Languages

Transcription:

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

Elements of Assembly Language Instructions (we have seen most of them) Comments Labels Declarations Assembler directives and trap codes Ignored White space (between symbols) CMPE12 Summer 2008 Slides by ADB 3 Assembly Instructions One instruction or declaration per line LABEL OPCODE OPERANDS ; COMMENTS optional mandatory CMPE12 Summer 2008 Slides by ADB 4 2

Opcodes and Operands Opcodes Reserved symbols that correspond to LC-3 instructions Listed in Appendix A (ex: ADD, AND, ) Operands Can be registers: Rn, where n is the register number Can be immediate numbers Prefix: # (decimal), x (hex), or b (binary) Can be labels Symbolic names of memory locations Separated by spaces, tabs, or commas Number, order, and type correspond to the instruction format CMPE12 Summer 2008 Slides by ADB 5 Data Types LC-3 has two basic data types Integer Character Both are 16 bits wide (a word) Though a character is only 8 bits in size How does that work?? CMPE12 Summer 2008 Slides by ADB 6 3

Comments In Code What is a comment? Anything on a line after a semicolon is a comment Comments are ignored by the assembler Used by humans to document and understand programs Some tips for useful comments Avoid restating the obvious Bad: Decrement R1 Provide additional insight Good: Accumulate the product Use comments to separate pieces of program CMPE12 Summer 2008 Slides by ADB 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 CMPE12 Summer 2008 Slides by ADB 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 CMPE12 Summer 2008 Slides by ADB 9 Assembler Directives:.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 Example.orig x3000 Typical address for LC-3 is x3000 CMPE12 Summer 2008 Slides by ADB 10 5

Assembler Directives:.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 CMPE12 Summer 2008 Slides by ADB 11 Assembler Directives:.FILL In C type varname; Where type is one of these int (integer) char (character) float (floating-point) In LC-3 varname.fill value Where value is required type is 16-bit integer CMPE12 Summer 2008 Slides by ADB 12 6

Assembler Directives:.BLKW ;set aside 3 unnamed spaces.blkw 3 ;set aside 1 named word Bob.BLKW 1 ; set aside 7 labeled words, initialize them all to 4 Num.BLKW 7 #4 CMPE12 Summer 2008 Slides by ADB 13 Assembler Directives:.STRINGZ Declare a string of characters Stored contiguously in memory Automatically terminated with x0000 Null-terminated Example: hello.stringz Hello World! CMPE12 Summer 2008 Slides by ADB 14 7

Assembler Directives:.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! CMPE12 Summer 2008 Slides by ADB 15 System Calls: TRAP A trap is an exception that interrupts normal processing to perform a system-level task Certain traps are pre-defined in a trap vector To call a trap Use the TRAP instruction Specifying the trap vector Very tedious and dangerous for a programmer to deal with I/O CMPE12 Summer 2008 Slides by ADB 16 8

Trap Service Routines Trap Vector x20 x21 x22 x23 x24 x25 Pseudo- Instruction 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. CMPE12 Summer 2008 Slides by ADB 17 Trap Examples To print a character ; the char must be in R0 TRAP x21 or OUT To read in a character ; will go into R0[7:0], ; no echo. TRAP x20 or GETC To end the program TRAP HALT x25 or CMPE12 Summer 2008 Slides by ADB 18 9

Simple LC-3 program.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 What does this program do? What is in Res at the end? CMPE12 Summer 2008 Slides by ADB 19 The Assembly Process Convert assembly language file (.asm) into an executable file (.obj) for the LC-3 simulator The executable file is the pure binary machine code LC-3 uses a two-pass assembler Status messages are shown when assembling E.g., Starting Pass 1... Pass 1-0 error(s) Starting Pass 2... Pass 2-0 error(s) CMPE12 Summer 2008 Slides by ADB 20 10

The Assembly Process The assembly process is Assembling Pass 1 Pass 2 Linking Loading Running CMPE12 Summer 2008 Slides by ADB 21 The Assembly Process: Assembling First Pass Scan program file Find all labels and calculate the corresponding addresses Generate the symbol table Second Pass Convert instructions to machine language, using information from symbol table CMPE12 Summer 2008 Slides by ADB 22 11

First Pass: The Symbol Table Find the.orig statement Tells the address of the first instruction Initialize the location counter (LC), which keeps track of the current instruction For each non-empty line in the program If a line contains a label, add label plus LC to symbol table Increment LC For a.blkw or.stringz, increment LC by the amount of space allocated Stop when.end statement is reached A line with only a comment is considered an empty line CMPE12 Summer 2008 Slides by ADB 23 Example: Generating a Symbol Table Symbol Address.ORIG x3000 x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 x300a x300b x300c LD LD LD R2, Zero R0, M0 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 CMPE12 Summer 2008 Slides by ADB 24 12

Second Pass: Generating Machine Language For each executable assembly language statement, generate the corresponding machine language instruction If the operand is a label, look up the address from the symbol table Potential problems Improper number of type of arguments E.g.: NOT R1,#7 ADD R1,R2 Immediate argument too large E.g.: ADD R1,R2,#1023 Address (associated with label) more than 256 from instruction Then, can t use PC-relative addressing mode CMPE12 Summer 2008 Slides by ADB 25 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, you 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 It is possible to load overlapping object files CMPE12 Summer 2008 Slides by ADB 26 13

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 CMPE12 Summer 2008 Slides by ADB 27 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 re-adjust branch targets and load/store addresses CMPE12 Summer 2008 Slides by ADB 28 14

And Finally: Running The loader makes the CPU jump to the first instruction Specified by.orig The program executes When execution completes, control returns to the OS or simulator CMPE12 Summer 2008 Slides by ADB 29 The LC-3 Assembler The LC-3 assembler generates several different output files CMPE12 Summer 2008 Slides by ADB 30 15

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) CMPE12 Summer 2008 Slides by ADB 31 16