introduction to computing systems from bits & gates to c & beyond chapter 7 lc-2 assembly...

11
Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 Chapter 7 LC-2 Assembly Language

Upload: leonel-balderson

Post on 14-Dec-2015

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

Introduction to Computing Systemsfrom bits & gates to C & beyond

Chapter 7Chapter 7

LC-2 Assembly Language

Page 2: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 2

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

ExampleExample01 ;02 ; Program to multiply a number by six03 ;04 .ORIG x305005 LD R1, SIX06 LD R2, NUMBER07 AND R3, R3, #0 ; clear R308 ;09 ; The inner loop0A ;0B AGAIN ADD R3, R3, R20C ADD R1, R1, #-1 ; keep track of iterations0D BRp AGAIN0E ;0F HALT10 ;11 NUMBER .BLKW 112 SIX .FILL x000613 ;14 .END

Page 3: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 3

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

Assembly Language InstructionsAssembly Language Instructions

Formats LABEL OPCODE OPERANDS ; COMMENTS LABEL PSEUDO-OPS ; COMMENTS

Label Label is a symbolic name that refers to a memory location. It is used to:

indicate the target of a branch instruction, e.g. AGAIN in location 0B store a value in a memory location, e.g. NUMBER and SIX

Comments intended for humans only: explanation of code, visual display

Page 4: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 4

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

Pseudo-OpsPseudo-Ops Are directives to the assembler

Assembler: the program that will translate human-readable assembly

language into machine instructions.

LC-2 Pseudo-Ops:

.ORIG address Tells assembler where to locate the program in

memory (starting address).

.END Marks the end of the source program.

.BLKW n Set aside a block of n words in memory.

.FILL value Store value in the next location in memory

.STRINGZ string Store the string, one character per word, in

memory. Add a word of x0000 after the string.

.EXTERNAL The label so indicated is allocated in another module.

Page 5: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 5

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

ExamplesExamples

.ORIG x3000

AND R1, R1, #0

ADD R1, R1, #10

LD R2, Twenty

LD R3, Ess

Twenty FILL x0014

Ess .FILL “S”

.BLKW 2

.STRINGZ “Hi”

.BLKW 3

.END

x3000: AND R1, R1, #0x3001: ADD R1, R1, #10x3002: LD R2, #4x3003: LD R3, #5x3004: x0014 ; 0000 0000 0001 0100x3005: x0053 ; 0000 0000 0101 0011x3006:x3007:x3008: x0048 ; Hx3009: x0069 ; ix300A: x0000 ; null terminated stringx300B:x300C:x300D:

Page 6: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 6

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

The Assembly ProcessThe Assembly ProcessObjective

Translate the AL (Assembly Language) program into ML (Machine Language).

Each AL instruction yields one ML instruction word.

Interpret pseudo-ops correctly.

ProblemAn instruction may reference a label.

If the label hasn’t been encountered yet, the assembler can't form the instruction word

SolutionTwo-pass assembly

Page 7: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 7

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

Two-Pass Assembly - 1Two-Pass Assembly - 1

First PassScan 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 find .END

Page 8: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 8

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

Two-Pass Assembly - 2Two-Pass Assembly - 2

Second PassScan each line again

Translate each AL instruction into ML

Look up any symbols in the symbol table

Determine operand field for the instruction

Check for errors if reference crosses page boundaries

A reference to an undefined symbol is an error

Fill memory locations as directed by pseudo-ops

Stop when find .END

Page 9: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 9

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

Symbol Table Symbol Table

From the previous example:

Symbol AddressAgain x3053Number x3057Six x3058

;; Program to multiply a number by six;

.ORIG x3050x3050 LD R1, SIXx3051 LD R2, NUMBERx3052 AND R3, R3, #0

;; The inner loop;

x3053 AGAIN ADD R3, R3, R2x3054 ADD R1, R1, #-1 x3055 BRp AGAIN

;x3056 HALT

;x3057 NUMBER .BLKW 1x3058 SIX .FILL x0006

;.END

Page 10: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 10

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

Object FileObject File

Each source file is translated into an object file: the executable image.

A complete program may include several source and/or object files: Source files written by the programmer Library files provided by the system (OS or other)

The object files must be linked Need to provide a copy of the symbol table Include it in the header of the object file

Page 11: Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

7 - 11

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

Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside

Example - ParityExample - ParityParity is a function that returns a 1 when the number of 1s in a

word is odd and 0 when it is even.

.ORIG x30003000 AND R2, R2, x0 ; clear R23001 LDI R1, Input ; load word into R13002 Count BRz Report ; if 0, done

counting3003 BRp Shift ; if >0, skip ADD3004 ADD R2, R2, x1 ; increment count3005 Shift ADD R1, R1, R1 ; shift left 1 bit 3006 BRnzp Count ; go back up3007 Report AND R3, R2, x1 ; LSB 1 or 0?3008 STI R3, Output ; store results3009 TRAP x25 ; halt program300A Input .FILL x3200 ; address of input300B Output .FILL x3201 ; address of output

Symbol Address

Count x3002

Shift x3005

Report x3007

Input x300A

Output x300B