90 likes | 285 Views
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.
E N D
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
An Assembly Language Program ; ; Program to multiply a number by the constant 6 ; .ORIG x3050 LD R1, SIX LD R2, NUMBER AND R3, R3, #0 ; Clear R3. It will ; contain the product. ; The inner loop ; AGAIN ADD R3, R3, R2 ADD R1, R1, #-1 ; R1 keeps track of BRp AGAIN ; the iteration. ; HALT ; NUMBER .BLKW 2 SIX .FILL x0006 ; .END Symbol Table: Symbol Address AGAIN x3053 NUMBER x3057 SIX x3059
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 Addresses Start x3000 Number x300A Data ? Value ? • The “Linker/Loader” would generate another “global table to resolve • Externals & Exports at Load time
Trap Codes • LC-3 assembler provides “pseudo-instructions” foreach trap code, so you don’t have to remember them.
An Assembly Language Program ; ; Program to multiply a number by the constant 3 ; .ORIG x3050 LD R1, SIX LD R2, NUMBER AND R3, R3, #0 ; Clear R3. It will ; contain the product ; ; Multiply by adding ; AGAIN ADD R3, R3, R2 ADD R1, R1, #-1 ; R1 keeps track of BRp AGAIN ; the iteration ; HALT ; ; Data ; NUMBER .BLKW 1 SIX .FILL x0003 ; .END
Program to add two integers .ORIG x3000 ; begin at x3000 ; input two numbers TRAP x23 ;input an integer character LD R3, HEXN30 ;subtract x30 to get integer ADD R0, R0, R3 ADD R1, R0, x0 ;move the first integer to register 1 TRAP x23 ;input another integer ADD R0, R0, R3 ;subtract x30 to get 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 TRAP x22 ;"PUTS" outputs a string ADD R0, R2, x0 ;move the sum to R0, to be output LD R3, HEX30 ;add 30 to integer to get integer character ADD R0, R0, R3 TRAP x21 ;display the sum ; stop HALT ; data MESG .STRINGZ "The sum of those two numbers is: “ HEXN30 .FILL xFFD0 HEX30 .FILL x0030 .END