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

An Assembly Language Program ; ; Program to multiply a number by the constant 6 ; . ORIG x 3050 LD R 1, SIX LD R 2, NUMBER AND R 3, #0 ; Clear R 3. It will ; contain the product. ; The inner loop ; AGAIN ADD R 3, R 2 ADD R 1, #-1 ; R 1 keeps track of BRp AGAIN ; the iteration. ; HALT ; NUMBER. BLKW 2 SIX. FILL x 0006 ; . END Symbol Table: Symbol Address AGAIN x 3053 NUMBER x 3057 SIX x 3059

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?

More than One Object (Load) File • Symbol Table Symbols Externals Exports Start Number Data Value • Addresses x 3000 x 300 A ? ? The “Linker/Loader” would generate another “global table to resolve Externals & Exports at Load time

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.

An Assembly Language Program ; ; Program to multiply a number by the constant 3 ; . ORIG x 3050 LD R 1, SIX LD R 2, NUMBER AND R 3, #0 ; Clear R 3. It will ; contain the product ; ; Multiply by adding ; AGAIN ADD R 3, R 2 ADD R 1, #-1 ; R 1 keeps track of BRp AGAIN ; the iteration ; HALT ; ; Data ; NUMBER. BLKW 1 SIX. FILL x 0003 ; . END

Program to add two integers. ORIG x 3000 ; input two numbers TRAP x 23 LD R 3, HEXN 30 ADD R 0, R 3 ADD R 1, R 0, x 0 TRAP x 23 ADD R 0, R 3 ; add the numbers ADD R 2, R 0, R 1 ; print the results LEA R 0, MESG TRAP x 22 ADD R 0, R 2, x 0 LD R 3, HEX 30 ADD R 0, R 3 TRAP x 21 ; stop HALT ; data MESG HEXN 30 HEX 30 . STRINGZ. FILL. END ; begin at x 3000 ; input an integer character ; subtract x 30 to get integer ; move the first integer to register 1 ; input another integer ; subtract x 30 to get integer ; add the two integers ; load the address of the message string ; "PUTS" outputs a string ; move the sum to R 0, to be output ; add 30 to integer to get integer character ; display the sum "The sum of those two numbers is: “ x. FFD 0 x 0030
- Slides: 9