Chapter 7 Introduction to LC-3 Assembly Language - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Chapter 7 Introduction to LC-3 Assembly Language

Description:

... #0 ; Clear R3 ; The product. ; The multiply loop AGAIN ADD R3, R3, R2 ; Accumulate product ADD R1, R1, #-1 ; Dec counter BRp ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 20
Provided by: Larr155
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 Introduction to LC-3 Assembly Language


1
Chapter 7Introduction to LC-3Assembly Language
  • Assembly Language
  • Assembly Process
  • Using the Editor Simulator for Assembly
    Language Programming

2
LC-3 Assembly Language Syntax
  • Each line of a program is one of the following
  • an instruction
  • an assember directive (or pseudo-op)
  • a comment
  • Whitespace (between symbols) and case are
    ignored.
  • Comments (beginning with ) are also ignored.
  • An instruction has the following format

LABEL OPCODE OPERANDS COMMENTS
optional
mandatory
Example Loop1 ADD R3, R3, -1 Decrement
R3
3
Assembler Directives
  • Pseudo-operations
  • do not refer to operations executed by program
  • used by assembler
  • look like instruction, but opcode starts with
    dot

Opcode Operand Meaning
.ORIG address starting address of program
.END end of assembly program
.BLKW n allocate n words of storage
.FILL n allocate one word, initialize with value n
.STRINGZ n-character string allocate n1 locations, initialize w/characters and null terminator
4
Compute the Sum of 12 Integers
  • Program begins at location x3000.
  • Integers begin at location x3100.

R1 ? x3100R3 ? 0 (Sum)R2 ? 12(count)
R20?
R4 ? MR1 R3 ? R3R4R1 ? R11 R2 ? R2-1
NO
YES
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
5
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 Add to R3
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
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
6
Compute the Sum of 12 Integers - Program
  • .ORIG x3000
  • Add 12 integers
  • R1 Pointer to integer
  • R2 Loop counter
  • R3 Accumulator
  • R4 Temporary register
  • LD R1 DATAADDR Load pointer
  • to integers
  • AND R3, R3, 0 Accumulator 0
  • AND R2, R2, 0 Counter 12
  • ADD R2, R2, 12
  • Add integers
  • LOOP BRZ STOP Stop when done
  • LDR R4, R1, 0 Add next integer
  • ADD R3, R3, R4

R1 ? x3100R3 ? 0 (Sum)R2 ? 12(count)
R20?
R4 ? MR1 R3 ? R3R4R1 ? R11 R2 ? R2-1
NO
YES
7
Compute the Sum of 12 Integers - Data
  • .ORIG x3100
  • Data section
  • DATA .FILL x0001 12 integers
  • .FILL x0002
  • .FILL x0004
  • .FILL x0008
  • .FILL xFFFF
  • .FILL xFFFE
  • .FILL xFFFC
  • .FILL xFFF8
  • .FILL x0007
  • .FILL x0004
  • .FILL x0002
  • .FILL x0003
  • .END

8
Compute the Sum of 12 Integers
  • Use the LC3 Editor to enter the program and data
    and store them as
  • add1.asm
  • data1.asm
  • Use the LC3 Editor to assemble them
  • add1.asm ? add1.obj
  • data1.asm ? data1.obj
  • Then use the LC3 Simulator to test them
  • load add1.obj and data1.obj
  • Set the PC, appropriate breakpoints, and execute
    the program (single step or run)

9
Sum of 12 Integers in One Package
  • .ORIG x3000
  • Add 12 integers
  • R1 Pointer to integer
  • R2 Loop counter
  • R3 Accumulator
  • R4 Temporary register
  • LEA R1, DATA Load pointer to
    integers
  • AND R3, R3, 0 Accumulator 0
  • AND R2, R2, 0 Counter 12
  • ADD R2, R2, 12
  • LOOP BRZ STOP Stop when done
  • LDR R4, R1, 0 Add next integer
  • ADD R3, R3, R4
  • ADD R1, R1, 1 Inc pointer
  • ADD R2, R2, -1 Dec counter
  • BRNZP LOOP
  • STOP BRNZP STOP Stop

10
One Pass vs Two Pass Assemblers
  • What does the assembler need to do?
  • Check for syntax errors
  • Build a symbol table
  • Assemble statements
  • Use pseudo-op instructions
  • Resolve Addresses
  • Create a load module
  • Two Pass Checks for syntax errors and builds
    the Symbol Table during first pass, resolves
    operand addresses during second pass.
  • One Pass Checks for syntax errors, builds the
    Symbol Table, and resolves operand addresses
    during the first pass. So why have a two pass?

11
An Assembly Language Program Example
  • Program to multiply a number by the constant 6
  • .ORIG x3050
  • LD R1, SIX Constant 6
  • LD R2, NUMBER Number
  • AND R3, R3, 0 Clear R3
  • The product.
  • The multiply loop
  • AGAIN ADD R3, R3, R2 Accumulate product
  • ADD R1, R1, -1 Dec counter
  • BRp AGAIN
  • HALT
  • NUMBER .BLKW 3 Value of Number
  • SIX .FILL x0006

Symbol Table Symbol Address
AGAIN x3053
NUMBER x3057
SIX x305A
12
What if there were More than One Object (Load)
File
  • Example Symbol Table
  • Symbols Externals Exports Addresses
  • Start x3000
  • Number x300A
  • Data ?
  • Value x30E0
  • The Linker/Loader would generate another
    global symbol table to resolve Externals
    Exports at Load time. It would only address the
    Externals (Imports) and Exports (Internals).
  • An Export is a value make available to other
    modules
  • An External is a value expected to be defined in
    another module

13
Trap Codes
  • LC-3 assembler provides pseudo-instructions
    foreach trap code, so you dont have to remember
    their TRAP s.

Code Equivalent Description
HALT TRAP x25 Halt execution and print message to console.
IN TRAP x23 Print prompt on console,read (and echo) one character from keyboard.Character stored in R070.
OUT TRAP x21 Write one character (in R070) to console.
GETC TRAP x20 Read one character from keyboard.Character stored in R070.
PUTS TRAP x22 Write null-terminated string to console.Address of string is in R0.
14
Program to add two single digit integers
  • .ORIG x3000 begin at x3000
  • input two numbers
  • IN input an integer character (ascii)
    TRAP 23
  • LD R3, HEXN30 subtract x30 to get integer
  • ADD R0, R0, R3
  • ADD R1, R0, 0 move the first integer to
    register 1
  • IN input another integer TRAP 23
  • ADD R0, R0, R3 convert it to an integer
  • add the numbers
  • ADD R2, R0, R1 add the two integers
  • print the results
  • LEA R0, MESG load the address of the message
    string
  • PUTS "PUTS" outputs a string TRAP 22
  • ADD R0, R2, 0 move the sum to R0, to be
    output
  • LD R3, HEX30 add 30 to integer to get integer
    character
  • ADD R0, R0, R3
  • OUT display the sum TRAP 21
  • stop
  • HALT TRAP 25

15
Program to add two single digit integers
  • Enter,
  • Assemble,
  • Load
  • Execute, and Test the program.

16
Write a program to count the 1s in register R0
  • Program to count 1's in Register R0
  • R3 is a working copy of R0
  • R1 contains the count
  • R2 is a loop counter
  • .orig x3100
  • ADD R3, R0, 0 copy R0 into R3
  • AND R1, R1, 0 clear count
  • ADD R3, R3, 0 test highest bit
  • BRZP NEXT count if neg
  • ADD R1, R1, 1
  • NEXT AND R2, R2, 0 check remaining 15 bits
  • ADD R2, R2, -15 R2 -15 (count)
  • LOOP ADD R3, R3, R3 shift R3 left
  • BRZP AGAIN
  • ADD R1, R1, 1 count if neg
  • AGAIN ADD R2, R2, 1 inc count

17
Program to Check for overflow
  • Add R3R0R1, R20 indicates no overflow
  • .ORIG x3000
  • AND R2, R2, 0 Initially R20 (no Overflow
    assumed)
  • ADD R3, R0, R1 R3R0R1
  • test for overflow
  • ADD R0, R0, 0 test R0
  • BRN NEG Branch if RO negative
  • ADD R1, R1, 0 R0 pos, test R1
  • BRN DONE R0 pos, R1 neg -gt No overflow
  • ADD R3, R3, 0 R0 pos, R1 pos, maybe,
    test R3
  • BRZP DONE R3 also pos -gt no overflow
  • ADD R2, R2, 1 Set R21 indicating overflow
  • BRNZP DONE
  • R0NEG ADD R1, R1, 0 R0 neg, test R1

18
Count the occurrences of a character in a file.

19
  • Program to count occurrences of a character in
    a file.
  • Character to be input from the keyboard.
  • Result to be displayed on the monitor.
  • Program only works if no more than 9
    occurrences are found.
  • Initialization
  • .ORIG x3000
  • AND R2, R2, 0 R2 is counter, initially 0
  • LD R3, PTR R3 is pointer to character file
  • GETC R0 gets input character
  • LDR R1, R3, 0 R1 gets first character from
    file
  • Test character for end of file
  • TEST ADD R4, R1, -4 Test for EOT (ASCII x04)
  • BRz OUTPUT If done, prepare the output
Write a Comment
User Comments (0)
About PowerShow.com