Introduction to LC3 Assembly Language LC3 Assembly Language
Introduction to LC-3 Assembly Language
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
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. ORIG address . END Meaning starting address of program end of program . BLKW n allocate n words of storage . FILL n allocate one word, initialize with value n . STRINGZ n-character string allocate n+1 locations, initialize w/characters and null terminator
Compute the Sum of 12 Integers • Program begins at location x 3000. • Integers begin at location x 3100. R 1 x 3100 R 3 0 (Sum) R 2 12(count) R 2=0? NO R 4 R 3 R 1 R 2 M[R 1] R 3+R 4 R 1+1 R 2 -1 YES R 1: “Array” index pointer (Begin with location 3100) R 3: Accumulator for the sum of integers R 2: Loop counter (Count down from 12) R 4: Temporary register to store next integer
Compute the Sum of 12 Integers Address Instruction Comments x 3000 1 1 1 0 0 0 1 1 1 1 1 R 1 x 3100 x 3001 0 1 0 1 1 1 0 0 0 R 3 0 x 3002 0 1 0 1 0 1 0 0 0 R 2 0 x 3003 0 0 0 1 0 1 0 1 1 0 0 R 2 12 x 3004 0 0 0 1 0 0 0 0 1 If Z, goto x 300 A x 3005 0 1 1 0 0 0 0 1 0 0 0 Load next value to R 4 x 3006 0 0 0 1 1 0 0 0 1 0 0 x 3007 0 0 0 1 1 0 0 1 Increment R 1 (pointer) X 3008 0 0 0 1 0 1 1 1 1 Decrement R 2 (counter) x 3009 0 0 1 1 1 1 1 0 Goto x 3004 Add to R 3 R 1: “Array” index pointer (Begin with location 3100) R 3: Accumulator for the sum of integers R 2: Loop counter (Count down from 12) R 4: Temporary register to store next integer
Compute the Sum of 12 Integers. ORIG x 3000 ; Add 12 integers ; R 1: Pointer to integer ; R 2: Loop counter ; R 3: Accumulator ; R 4: Temporary register LD AND ADD R 1 R 3 R 2 DATAADDR ; Load pointer to integers R 3 #0 ; Accumulator = 0 R 2 #0 ; Counter = 12 R 2 #12 ; Add integers LOOP STOP BRZ LDR ADD ADD BRNZP STOP R 4 R 1 R 3 R 1 R 2 LOOP #0 R 4 #1 #-1 BRNZP STOP ; Stop when done ; Add next integer ; Inc pointer ; Dec counter ; Stop DATAADDR. FILL x 3100. END Note: Used DATAADDR to hold address of DATA. Why?
Compute the Sum of 12 Integers. ORIG x 3100 ; Data section DATA . END . FILL x 0001 x 0002 x 0004 x 0008 x. FFFF x. FFFE x. FFFC x. FFF 8 x 0007 x 0004 x 0002 x 0003 ; 12 integers
Compute the Sum of 12 Integers • Use the LC 3 Editor to enter the program and data and store them as add 1. asm data 1. asm • Use the LC 3 Editor to assemble them: add 1. asm add 1. obj data 1. asm data 1. obj • Then use the LC 3 Simulator to test them: load add 1. obj and data 1. obj Set the PC, appropriate breakpoints, and execute the program (single step or run)
Compute the Sum of 12 Integers. ORIG x 3000 ; Add 12 integers ; R 1: Pointer to integer ; R 2: Loop counter ; R 3: Accumulator ; R 4: Temporary register LOOP STOP LEA AND ADD BRZ LDR ADD ADD BRNZP R 1 DATA R 3 #0 R 2 R 2 #12 STOP R 4 R 1 #0 R 3 R 4 R 1 #1 R 2 #-1 LOOP STOP ; Load pointer to integers ; Accumulator = 0 ; Counter = 12 ; Stop when done ; Add next integer ; Inc pointer ; Dec counter ; Stop ; Data section DATA . END . FILL x 0001 x 0002 x 0004 x 0008 x. FFFF x. FFFE x. FFFC x. FFF 8 x 0007 x 0004 x 0002 x 0003 ; 12 integers Note: Used LEA to load pointer to data. Why?
Compute the Sum of 12 Integers • Use the LC 3 Editor to enter the program and data and store it as add 2. asm • Use the LC 3 Editor to assemble it: add 2. asm add 1. obj • Then use the LC 3 Simulator to test it: load add 2. obj Set the PC, appropriate breakpoints, and execute the program (single step or run)
One Pass vs Two Pass Assemblers • 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?
An Assembly Language Program ; ; Program to multiply a number by the constant 6 ; . ORIG x 3050 LD LD AND ; R 1, SIX R 2, NUMBER R 3, #0 AGAIN The multiply loop ADD BRp ; Load the number 6 (counter) ; Load the number ; Clear R 3. It will ; contain the product. R 3, R 2 R 1, #-1 AGAIN ; R 1 (counter) keeps track of ; the iteration. 2 x 0006 ; Value of number is stored here HALT NUMBER. BLKW SIX. FILL. END Symbol Table: Symbol Address AGAIN x 3053 NUMBER x 3057 SIX x 3059
More than One Object (Load) File • Example Symbol Table: Symbols Start Number Externals Data Exports Value Addresses x 3000 x 300 A ? ? • 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.
Trap Codes • LC-3 assembler provides “pseudo-instructions” for each trap code, so you don’t have to remember them. Code Equivalent Description HALT TRAP x 25 Halt execution and print message to console. IN TRAP x 23 Print prompt on console, read (and echo) one character from keybd. Character stored in R 0[7: 0]. OUT TRAP x 21 Write one character (in R 0[7: 0]) to console. GETC TRAP x 20 Read one character from keyboard. Character stored in R 0[7: 0]. PUTS TRAP x 22 Write null-terminated string to console. Address of string is in R 0.
Program to add two single digit integers ; ; . ORIG x 3000 input two numbers IN LD R 3, HEXN 30 ADD R 0, R 3 ADD R 1, R 0, x 0 IN ADD R 0, R 3 add the numbers ADD R 2, R 0, R 1 print the results LEA R 0, MESG PUTS ADD R 0, R 2, x 0 LD R 3, HEX 30 ADD R 0, R 3 OUT stop HALT ; data MESG HEXN 30 HEX 30 . STRINGZ. FILL. END ; begin at x 3000 ; input an integer character (ascii) {TRAP 23} ; subtract x 30 to get integer ; move the first integer to register 1 ; input another integer {TRAP 23} ; convert it to an integer ; add the two integers ; load the address of the message string ; "PUTS" outputs a string {TRAP 22} ; move the sum to R 0, to be output ; add 30 to integer to get integer character ; display the sum {TRAP 21} ; {TRAP 25} "The sum of those two numbers is: “ x. FFD 0 ; -30 HEX x 0030 ; 30 HEX
Program to add two single digit integers • Enter, • assemble, • Execute, • And test the program.
HW, due November 10 1) Write and test an LC-3 assembly language program to calculate and print on the console, the sum of two double digit hexidecimal numbers entered from the console. Show: – A snapshot of your well commented Assembly program in the LC-3 Editor Window with the Assembly response. – A snapshot of the Simulator execution and the Console Display Window with the input and output displayed.
HW, due November 10 (continued) 2) Write and test an LC-3 assembly language program to calculate and print all the numbers in the Fibonacci series that can be stored in words of the LC-3. The program should stop when it determines it has found the largest number. Show: – A snapshot of your well commented Assembly program in the LC-3 Editor Window with the Assembly response. – A snapshot of the Simulator execution and the Console Display Window with the Fibonacci numbers displayed.
Write a program to count the 1’s in register R 0 • • Flow Diagram Assembly code
Write a program to count the 1’s in register R 0 ; Program to count 1's in Register R 0 ; R 3 is a working copy of R 0 ; R 1 contains the count ; R 2 is a loop counter. orig x 3100 ADD R 3, R 0, #0 ; copy R 0 into R 3 AND R 1, #0 ; clear count ADD R 3, #0 BRZP NEXT ADD R 1, #1 ; test highest bit ; count if neg NEXT AND R 2, #0 ADD R 2, #-15 ; check remaining 15 bits ; R 2 = -15 (count) LOOP ADD R 3, R 3 BRZP AGAIN ADD R 1, #1 ADD R 2, #1 BRN LOOP ; shift R 3 left AGAIN HALT. END ; count if neg ; inc count
Program to Check for Overflow Write a program to add the contents of R 0 and R 1, and indicate in R 2 if there was an overflow • Flow Diagram • Assembly Code
Program to Check for overflow ; ; Add R 3=R 0+R 1, R 2=0 indicates no overflow. ORIG x 3000 AND R 2, #0 ADD R 3, R 0, R 1 ; test for overflow ADD R 0, #0 BRN NEG ADD R 1, #0 BRN DONE ADD R 3, #0 BRZP DONE ADD R 2, #1 BRNZP DONE R 0 NEG ADD R 1, #0 BRZP DONE ADD R 3, #0 BRN DONE ADD R 2, #1 DONE HALT. END ; Initially R 2=0 (no Overflow assumed) ; R 3=R 0+R 1 ; test R 0 ; Branch if RO negative ; R 0 pos, test R 1 ; R 0 pos, R 1 neg -> No overflow ; R 0 pos, R 1 pos, maybe, test R 3 ; R 3 also pos -> no overflow ; Set R 2=1 indicating overflow ; R 0 neg, test R 1 ; R 0 neg, R 1 pos -> No overflow ; R 0 neg, R 1 neg, maybe, test R 3 ; R 3 also neg -> no overflow ; Set R 2=1 indicating overflow
Sample Program • Count the occurrences of a character in a file.
Count the occurrences of a character in a file (1 0 f 2). ; ; ; ; ; 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 AND LD GETC LDR x 3000 R 2, #0 ; R 2 is counter, initially 0 R 3, PTR ; R 3 is pointer to character file ; R 0 gets input character R 1, R 3, #0 ; R 1 gets first character from file ; ; Test character for end of file ; TEST ADD R 4, R 1, #-4 ; Test for EOT (ASCII x 04) BRz OUTPUT ; If done, prepare the output ; ; Test character for match. If a match, increment count. ; NOT R 1, R 1 ADD R 1, R 0 ; If match, R 1 = x. FFFF NOT R 1, R 1 ; If match, R 1 = x 0000 BRnp GETCHAR ; If no match, do not increment ADD R 2, #1 ; ; Get next character from file. ; GETCHAR ADD R 3, #1 ; Point to next character. LDR R 1, R 3, #0 ; R 1 gets next char to test BRnzp TEST
Count the occurrences of a character in a file (2 of 2). ; ; Output the count. ; OUTPUT LD R 0, ASCII ; Load the ASCII template ADD R 0, R 2 ; Covert binary count to ASCII OUT ; ASCII code in R 0 is displayed. HALT ; Halt machine ; ; Storage for pointer and ASCII template ; ASCII. FILL x 0030 ; ASCII offset PTR. FILL x 4000 ; PTR to character file. END
- Slides: 25