Transcript Chapter 7

Introduction to Computing Systems
from bits & gates to C & beyond
Chapter 7
LC-2 Assembly Language
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
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
11
12
13
14
7-2
Example
;
; Program to multiply a number by six
;
.ORIG x3050
LD
R1, SIX
LD
R2, NUMBER
AND
R3, R3, #0
; clear R3
;
; The inner loop
;
AGAIN
ADD
R3, R3, R2
ADD
R1, R1, #-1 ; keep track of iterations
BRp
AGAIN
;
HALT
;
NUMBER .BLKW 1
SIX
.FILL x0006
;
.END
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 Instructions
 Formats
 LABEL
 LABEL
OPCODE OPERANDS
PSEUDO-OPS
; COMMENTS
; 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
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
Pseudo-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
7-4
The label so indicated is allocated in another module.
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
Examples
Twenty
Ess
7-5
.ORIG x3000
AND R1, R1, #0
ADD R1, R1, #10
LD
R2, Twenty
LD
R3, Ess
FILL x0014
.FILL “S”
.BLKW 2
.STRINGZ “Hi”
.BLKW 3
.END
x3000:
x3001:
x3002:
x3003:
x3004:
x3005:
x3006:
x3007:
x3008:
x3009:
x300A:
x300B:
x300C:
x300D:
AND R1, R1, #0
ADD R1, R1, #10
LD
R2, #4
LD
R3, #5
x0014 ; 0000 0000 0001 0100
x0053 ; 0000 0000 0101 0011
x0048 ; H
x0069 ; i
x0000 ; null terminated string
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 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
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
Two-Pass Assembly - 1
 First
Pass
 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 find .END
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 - 2
 Second
Pass
 Scan 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
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
Symbol ; Table
 From the previous
example:
Symbol Address
Again
x3053
Number x3057
Six
x3058
x3050
x3051
x3052
x3053
x3054
x3055
x3056
x3057
x3058
7-9
; Program to multiply a number by six
;
.ORIG x3050
LD
R1, SIX
LD
R2, NUMBER
AND
R3, R3, #0
;
; The inner loop
;
AGAIN
ADD
R3, R3, R2
ADD
R1, R1, #-1
BRp
AGAIN
;
HALT
;
NUMBER .BLKW 1
SIX
.FILL
x0006
;
.END
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 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
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
Example - Parity
Parity is a function that returns a 1 when the number of 1s in a
word is odd and 0 when it is even.
Symbol
Address
Count
x3002
Shift
x3005
Report
Input
x3007
x300A
Output
x300B
7 - 11
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
300A
300B
.ORIG
AND
LDI
Count BRz
BRp
ADD
Shift
ADD
BRnzp
Report AND
STI
TRAP
Input
.FILL
Output .FILL
x3000
R2, R2, x0
R1, Input
Report
Shift
R2, R2, x1
R1, R1, R1
Count
R3, R2, x1
R3, Output
x25
x3200
x3201
; clear R2
; load word into R1
; if 0, done counting
; if >0, skip ADD
; increment count
; shift left 1 bit
; go back up
; LSB 1 or 0?
; store results
; halt program
; address of input
; address of output