SP Chapter 3

Download Report

Transcript SP Chapter 3

SYSTEMS PROGRAMMING
CHAPTER 3
THE LC-3 ASSEMBLY
LANGUAGE
Er. Bharadwaj Choudhury
SYNTAX OF LC-3 INSTRUCTION
Each LC-3 instruction appears on line of its own and
can have up to four parts
 These parts in order are the label, the opcode, the
operand(s), and the comment
 Each instruction can start with a label, which makes
it easier to reference a data variable
 The second part of an instruction is the opcode. This
indicates to the assembler what kind of instruction it
will be.
 Operands are required by most instructions. These
operands indicate what data the instruction will be
manipulating. The operands are usually registers,
labels, or immediate values.

LC-3 MEMORY
LC-3 memory consists of 216 locations, each being 16
bits wide
 Each location is identified with an address, a positive
integer in the range 0 through 216-1
 More often we use 4-digit hexadecimal numbers for
the addresses. Hence, addresses range from x0000 to
xFFFF

LC-3 EXAMPLE INSTRUCTIONS
Op
Format
ADD
ADD R1, R2, #val
AND
AND R1,R2,R3
JMP
JMP R1
JSR
JSR LABEL
RET
RET
Description
Adds the values in R2 and the
immediate value and sets R1 to
the result
Performs a bitwise AND on the
values in R2 and R3
and sets R1 to the result
Unconditionally jump to the
instruction based upon the
address in R1
Put the address of the next
instruction after the JSR
instruction into R7 and jump to
the subroutine indicated by
LABEL
Return from a subroutine using
the value in R7 as the base
address
LC-3 EXAMPLE INSTRUCTIONS
Op
Format
Description
JSRR
JSSR R1
Similar to JSR except the
address stored in R1 is used
instead of using a LABEL
LD
LD R1, LABEL
Load the value indicated by
LABEL into the R1 register
LDI
LDI R1, LABEL
Load the value indicated by the
address at LABEL’s memory
location into the R1 register
LDR
LDR R1, R2, offset
Load the value from the memory
location found by adding the
value of R2 to offset into R1
LEA
LEA R1, LABEL
Load the address of LABEL into
R1
TRAP INSTRUCTION
TRAP
Trap
vector
trapvector
Equivalent
Assembly
Instruction
Performs the trap service
specified by trapvector8. Each
trapvector8 service has its own
assembly instruction that can
replace the trap instruction
Description
x20
GETC
Read one input character from the keyboard
and store it into R0 without echoing the
character to the console
x21
OUT
Output character in R0 to the console
x23
IN
Read one input character from the keyboard
and store it into R0 and echo it to the console
x25
HALT
Ends a user’s program
PSEUDO OPERATIONS
Pseudo-op Format Description:
.ORIG # tells the LC-3 simulator where it should
place the segment of code starting at address #
 .FILL #
Allocate one word, initialize with value
#
 .BLKW # Reserve # memory locations for data at
that line of code
 .STRINGZ “<String>” Place a null terminating
string <String> starting at that location.
 .END tells the LC-3 assembler to stop assembling
your code

ALU OPERATIONS
The numbers X and Y are found at locations x3100 and
x3101, respectively. Write LC-3 assembly language
instructions that does the following:
 Compute the sum X +Y and place it at location x3102
 Compute X AND Y and place it at location x3103
 Compute X OR Y and place it at location x3104
 Compute NOT(X) and place it at location x3105
 Compute NOT(Y) and place it at location x3106
 Compute X +3 and place it at location x3107
 Compute Y −3 and place it at location x3108
COMPUTE THE SUM OF 12 INTEGERS


Program begins at location x3000
Integers begin at location x3100
R1  x3100
R3  0 (Sum)
R2  12(count)
R2=0?
NO
R4
R3
R1
R2




M[R1]
R3+R4
R1+1
R2-1
YES
R1: “Array” index pointer (Begin with location x3100)
stop
R3: Accumulator for the sum of integers
R2: Loop counter (Count down from 12)
R4: Temporary register to store next integer
COMPUTE THE SUM OF 12 INTEGERS
Address
Instruction
Comments
x3000
1 1 1 0 0 0 1 0 1 1 1 1 1 1 1 1
R1  x3100
x3001
0 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0
R3  0
x3002
0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0
R2  0
x3003
0 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0
R2  12
x3004
0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1
If Z, goto x300A
x3005
0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 Load next value to R4
x3006
0 0 0 1 0 1 1 0 1 1 0 0 0 1 0 0
x3007
0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 Increment R1 (pointer)
X3008
0 0 0 1 0 1 0 0 1 0 1 1 1 1 1 1
Decrement R2
(counter)
x3009
0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0
Goto x3004
Add to R3
R1: “Array” index pointer (Begin with location 3100)
R3: Accumulator for the sum of integers
R2: Loop counter (Count down from 12)
R4: Temporary register to store next integer
COMPUTE THE SUM OF 12 INTEGERS
.ORIG x3000
; Add 12 integers
;
R1: Pointer to integer
;
R2: Loop counter
;
R3: Accumulator
;
R4: Temporary register
LD
AND
AND
ADD
R1
R3
R2
R2
DATAADDR ; Load pointer to integers
R3 #0
; Accumulator = 0
R2 #0
R2 #12
; Counter = 12
; Add integers
LOOP
BRNZ
LDR
ADD
ADD
ADD
BRP
STOP
R4 R1
R3 R3
R1 R1
R2 R2
LOOP
#0
R4
#1
#-1
; Stop when done
; Add next integer
; Inc pointer
; Dec counter
STOP HALT
; Stop
DATAADDR .FILL x3100
.END
Note: Used DATAADDR to hold address of DATA. Why?
COMPUTE THE SUM OF 12 INTEGERS
.ORIG x3100
; Data section
DATA
.END
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
.FILL
x0001
x0002
x0004
x0008
xFFFF
xFFFE
xFFFC
xFFF8
x0007
x0004
x0002
x0003
; 12 integers
 THANK YOU 