LC-3 Assembly Language

Similar documents
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

Syntax of LC-3 assembly: Language elements. Instructions

Computing Layers. 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

Chapter 7 Assembly Language

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

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

Introduction Presentation A

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?

Chapter 6 Programming the LC-3

M2 Instruction Set Architecture

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

ECE 206, Fall 2001: Lab 3

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

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

Chris Riesbeck, Fall Introduction to Computer Systems

Introduction to Computer Systems

Compila(on, Disassembly, and Profiling

Compilation, Disassembly, and Profiling (in Linux)

Introduction to Computer Systems

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

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

Instruction Set Architecture

Introduction to Computer Systems

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

CIT 595 Spring System Software: Programming Tools. Assembly Process Example: First Pass. Assembly Process Example: Second Pass.

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

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

COSC121: Computer Systems: Review

LC-3 Instruction Set Architecture

CENG3420 Lab 2-1: LC-3b Simulator

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

LC-3 Instruction Set Architecture. Textbook Chapter 5

1/17/2018. Task: Print a Number in Binary. ECE 220: Computer Systems & Programming. First Step: Break the Task into a Sequence of Two

Week 1 / Lecture 2 8 March 2017 NWEN 241 C Fundamentals. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Great Reality #2: You ve Got to Know Assembly Does not generate random values Arithmetic operations have important mathematical properties

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

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

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

Beginning C Programming for Engineers

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

ADD R3, R4, #5 LDR R3, R4, #5

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

EL2310 Scientific Programming

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

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

Final Labs and Tutors

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

Work relative to other classes

Instruction Set Architectures

Lecture 3: Instruction Set Architecture

Midterm 2 Review Chapters 4-16 LC-3

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

CS 61C: Great Ideas in Computer Architecture Introduction to C

Bits and Bytes. Why bits? Representing information as bits Binary/Hexadecimal Byte representations» numbers» characters and strings» Instructions

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Why Don t Computers Use Base 10? Lecture 2 Bits and Bytes. Binary Representations. Byte-Oriented Memory Organization. Base 10 Number Representation

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) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

(Extract from the slides by Terrance E. Boult

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

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

Lecture 3. More About C

COSC121: Computer Systems: Review

COMPUTER ORGANIZATION & ARCHITECTURE

What s next. Computer Systems A Programmer s Perspective

srl - shift right logical - 0 enters from left, bit drops off right end note: little-endian bit notation msb lsb "b" for bit

Lecture 1: Course Overview

Review Topics. Midterm Exam Review Slides

ISA Instruction Operation

ABSTRACTION ISN T THE ENTIRE STORY

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

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

The MARIE Architecture

appendix a The LC-3 ISA A.1 Overview

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

Computer Organization MIPS ISA

Bits and Bytes January 13, 2005

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

EE251: Tuesday September 5

Programming. Elementary Concepts

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

Computer Organization - Overview

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

EEL 5722C Field-Programmable Gate Array Design

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

CSE 2421: Systems I Low-Level Programming and Computer Organization. Linking. Presentation N. Introduction to Linkers

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

EECE.2160: ECE Application Programming

Computing Layers. Chapter 5 The LC-3

Writing ARM Assembly. Steven R. Bagley

C-string format with scanf/printf

Introduction to Computer Systems: Semester 1 Computer Architecture

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Transcription:

Chapter 7 LC-3 Assembly Language

CS Reality You ve got to know assembly Chances are, you ll never write program in assembly Compilers are much better & more patient than you are Understanding assembly key to machine-level execution model Behavior of programs in presence of bugs High-level language model breaks down Tuning program performance Understanding sources of program inefficiency Implementing system software Compiler has machine code as target Operating systems must manage process state 2

It s hard to write code in 1 s & 0 s! Assembly language makes it possible to write Machine Language code each line of assembly language is translated into a single ML instruction A program called the Assembler does the translation and provides useful tools: use of labels - symbolic names for address locations automatic conversion of binary / hex / decimal pseudo-ops 3

Assembly Language Instructions Formats LABEL OPCODE OPERANDS ; COMMENTS LABEL PSEUDO-OPS ; COMMENTS Opcode Symbolic name for the 4-bit ML opcode Label Symbolic name for a memory location. It is used to: indicate the target of a branch instruction, e.g. AGAIN in location 0B indicate the location of a stored value or array, e.g. NUMBER and SIX Comments intended for humans only: explanation of code, visual display 4

Pseudo-Ops are directives to the assembler they are not translated into ML instructions LC-3 Pseudo-Ops:.ORIG address Tells assembler where to locate the program in memory (starting address)..fill value Store value in the next location in memory..blkw n Set aside a block of n words in memory..stringz string Store the string, one character per word, in memory. Add a word of x0000 after the string..end Marks the end of the source program (not to be confused with the instruction HALT!).EXTERNAL The label so indicated is allocated in another module. 5

A sample program 01 ; 02 ; Program to multiply an integer by the number 6 03 ; 04.ORIG x3050 05 LD R1, SIX 06 LD R2, NUMBER 07 AND R3, R3, #0 ; clear R3 to hold the product 08 ; 09 ; The inner loop 0A ; 0B AGAIN ADD R3, R3, R2 0C ADD R1, R1, #-1 ; keep track of iterations 0D BRp AGAIN 0E ; 0F HALT 10 ; 11 NUMBER.BLKW 1 12 SIX.FILL x0006 13 ; 14.END 6

From Assembly to bits.orig x3000 AND R1, R1, #0 ADD R1, R1, #10 LD LD HALT R2, Twenty R3, Ess Twenty FILL x0014.blkw 2 Ess.FILL S.STRINGZ Hi.BLKW 3.END x3000: AND R1, R1, 0 0000 x3001: ADD R1, R1, 0 1010 x3002: LD R2, 0 0000 0010 x3003: LD R3, 0 0000 0100 x3004: TRAP 0010 0101 x3005: 0000 0000 0001 0100 ; x0014 x3006: x3007: x3008: 0000 0000 0101 0011 ; x0053 = S x3009: 0000 0000 0100 1000 ; x0048 = H x300a: 0000 0000 0110 1001 ; x0069 = i x300b: 0000 0000 0000 0000 ; x0000 = null terminator x300c: x300d: x300e: 7

The Assembly Process Objective Translate the AL (Assembly Language) program into ML (Machine Language). Each AL instruction yields one ML instruction word. Interpret pseudo-ops correctly. Problem An instruction may reference a label. If the label hasn t been encountered yet, the assembler can't form the instruction word Solution Two-pass assembly 8

Two-Pass Assembly - 1 First Pass - generating the symbol table Scan each line Keep track of current address Increment by 1 for each instruction Adjust as required for any pseudo-ops (e.g..fill or.stringz, etc.) For each label Enter it into the symbol table Allocate to it the current address Stop when.end is encountered 9

Symbol Table example Using the earlier example: Symbol Address Again x3053 Number x3057 Six x3058 ; ; Program to multiply a number by six ;.ORIG x3050 x3050 LD R1, SIX x3051 LD R2, NUMBER x3052 AND R3, R3, #0 ; ; The inner loop ; x3053 AGAIN ADD R3, R3, R2 x3054 ADD R1, R1, #-1 x3055 BRp AGAIN ; x3056 HALT ; x3057 NUMBER.BLKW 1 x3058 SIX.FILL x0006 ;.END 10

Two-Pass Assembly - 2 Second Pass - generating the ML program Scan each line again Translate each AL instruction into ML Look up symbols in the symbol table instruction Ensure that labels are no more than +256 / -255 lines from instruction Determine operand field for the instruction Fill memory locations as directed by pseudo-ops Stop when.end is encountered 11

Assembled code Using the earlier example: Symbol Address Again x3053 Number x3057 Six x3058 x3050 0010 001 0 0000 0111 ; LD R1, SIX x3051 0010 010 0 0000 0101 ; LD R2, NUMBER x3052 0101 011 011 1 00000 ; AND R3, R3, #0 x3053 0001 011 011 0 00 010 ; ADD R3, R3, R2 x3054 0001 001 001 1 11111 ; ADD R1, R1, #-1 x3055 0000 001 1 1111 1101 ; BRp AGAIN x3056 1111 0000 0010 0101 ; HALT X3057 ;.BLKW 1 x3058 0000 0000 0000 0110 ;.FILL x0006 12

Another example.orig x3000 3000 AND R2, R2, x0 ; clear R2 3001 LDI R1, Input ; load word into R1 3002 Count BRz Report ; if 0, done counting 3003 BRp Shift ; if >0, skip ADD 3004 ADD R2, R2, x1 ; increment count 3005 Shift ADD R1, R1, R1 ; shift left 1 bit 3006 BRnzp Count ; go back up 3007 Report AND R3, R2, x1 ; LSB 1 or 0? 3008 STI R3, Output ; store results 3009 TRAP x25 ; halt program 300A Input.FILL x3200 ; address of input 300B Output.FILL x3201 ; address of output Symbol Count Shift Report Input Output Address x3002 x3005 x3007 x300a x300b 13

From C to executable Hello.c Pre- Compiler Assembler Linker Hello.i Hello.s Hello.o Processor (cc1) (as) (ld) Hello (cpp) Preprocessing phase: Modifies original program according to preprocessor directives (these start with the # character). The result is another C program text file (typically with the.i suffix) #include <stdio.h> #define FALSE 0 Compilation phase: text file converted from high-level language to assembly. Regardless of the original high-level language, all programs handled identically from this point onward One assembly corresponds one-to-one with machine instructions 14

From C to executable Hello.c Pre- Compiler Assembler Linker Hello.i Hello.s Hello.o Processor (cc1) (as) (ld) Hello (cpp) Assembly phase: assembly text file converted into machine language binary file and packaged into a relocatable object program. Linker phase: multiple object programs are merged to result in an executable object file. For example: a standard library function such as printf might reside in a separate precompiled object file (like printf.o) that exists elsewhere in the system. 15

Object File Each source file is translated into an object file a list of ML instructions including the symbol table. A complete program may include several source and/or object files: Source files written in Assembly by the programmer Library files provided by the system (OS or other) Compiled HLL libraries The object files must be linked One object file will be the main All cross-referenced labels in symbol tables will be resolved 16

The end result is the executable image (.exe file) this is a file ( image ) of the finalized list of ML instructions, with all symbolic references resolved it is loaded by copying the list into memory, starting at the address specified in the.orig directive it is run by copying the starting address to the PC 17

Representing Instructions int sum(int x, int y) { return x+y; } For this example, Alpha & Sun use two 4-byte instructions Use differing numbers of instructions in other cases PC uses 7 instructions with lengths 1, 2, and 3 bytes Same for NT and for Linux NT / Linux not fully binary compatible Alpha sum 00 00 30 42 01 80 FA 6B Sun sum 81 C3 E0 08 90 02 00 09 Different machines use totally different instructions and encodings PC sum 55 89 E5 8B 45 0C 03 45 08 89 EC 5D C3 18

Problems 7.1, 7.2 (tricky), 7.4, 7.5, 7.6 19