LC-2 Assembly Language: Introduction To Computing Systems
LC-2 Assembly Language: Introduction To Computing Systems
Chapter 7
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7-2 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7-3 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7-4 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7-5 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
A partial assembly sample
.ORIG x3000 x3000: AND R1, R1, b0 0000
AND R1, R1, #0 x3001: ADD R1, R1, b0 1010
ADD R1, R1, #10 x3002: LD R2, b0 0000 0010
LD R2, Twenty x3003: LD R3, b0 0000 0100
x3004: TRAP b0010 0101
LD R3, Ess
x3005: b0000 0000 0001 0100 ; x0014
HALT
x3006:
Twenty FILL x0014 x3007:
.BLKW 2 x3008: b0000 0000 0101 0011 ; x0053
Ess .FILL “S” x3009: b0000 0000 0100 1000 ; x0048 = ‘H’
.STRINGZ “Hi” x300A: b0000 0000 0110 1001 ; x0069 = ‘i’
.BLKW 3 x300B: x0000 ; null terminator
.END x300C:
x300D:
x300E:
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7-6 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
The Assembly Process
Objective
Problem
Solution
Two-pass assembly
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7-7 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Two-Pass Assembly - 1
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7-8 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Symbol Table example
;
; Program to multiply a number by six
;
Using the earlier .ORIG x3050
example: x3050 LD R1, SIX
x3051 LD R2, NUMBER
x3052 AND R3, R3, #0
Symbol Address ;
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7-9 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Another example - Parity checking
Parity is a function that returns a 1 when the number of 1s
in a word is odd and 0 when it is even.
.ORIG x3000
3000 AND R2, R2, x0 ; clear R2
3001 LDI R1, Input ; load word into R1
Symbol Address 3002 Count BRz Report ; if 0, done counting
Count x3002 3003 BRp Shift ; if >0, skip ADD
3004 ADD R2, R2, x1 ; increment count
Shift x3005
3005 Shift ADD R1, R1, R1 ; shift left 1 bit
Report x3007 3006 BRnzp Count ; go back up
Input x300A 3007 Report AND R3, R2, x1 ; LSB 1 or 0?
3008 STI R3, Output ; store results
Output x300B 3009 TRAP x25 ; halt program
300A Input .FILL x3200 ; address of input
300B Output .FILL x3201 ; address of output
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7 - 10 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Two-Pass Assembly - 2
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7 - 11 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Assembled code
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7 - 12 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
Object File
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7 - 13 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside
The end result …
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7 - 14 Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside