LC 3 Assembly Language (Textbook Chapter 7)" CMPE12 – Fall 2011 – Joel Ferguson" 6 2" Assembly and assembler" • Machine language binary" • Assembly language symbolic" • Assembler is a program that tur[.]
LC-3 Assembly Language (Textbook Chapter 7)" Assembly and assembler" • Machine language - binary" 0001110010000110 • Assembly language - symbolic" ADD R6, R2, R6 ; increment index reg • Assembler is a program that turns symbols into machine instructions." – ISA-specific: close correspondence between symbols and instruction set" • mnemonics for opcodes" • labels for memory locations" CMPE12 – Fall 2011 – Joel Ferguson" - 2" Syntax of LC-3 assembly: Language elements" • • • • • Instructions (we have seen most of them)! Comments" Labels" Declarations" Assembler directives and trap codes" Case is ignored." CMPE12 – Fall 2011 – Joel Ferguson" - 3" Instructions" • One instruction or declaration per line" LABEL OPCODE OPERANDS ; COMMENTS! optional! CMPE12 – Fall 2011 – Joel Ferguson" mandatory! - 4" Opcodes and Operands" • Opcodes" – reserved symbols that correspond to LC-3 instructions" – listed in Appendix A (ex: ADD, AND, …)" • Operands" – Registers: Rn, where n is the register number" – Immediate numbers: # (decimal), x (hex), or b (binary)" – Labels: symbolic names of memory locations" – Operands are separated by spaces, tabs, or commas" – Their number, order, and type correspond to the instruction format" CMPE12 – Fall 2011 – Joel Ferguson" - 5" Data types" LC-3 has basic data types" • Integer" • Character" Both are 16 bits wide (a word), though a character is only bits in size." CMPE12 – Fall 2011 – Joel Ferguson" - 6" Comments" – Anything on a line after a semicolon is a comment" – Comments are ignored by assembler" – Used by humans to document/understand programs" – Tips for useful comments:" • avoid restating the obvious, as “decrement R1”" • provide additional insight, as in “accumulate product in R6”" • use comments to separate pieces of program" CMPE12 – Fall 2011 – Joel Ferguson" - 7" Labels" • Placed at beginning of line" • Assign a symbolic name to their line (its address)" • Symbolic names used to identify memory locations Two kinds:" – Location of target of a branch or jump" – Location of a variable for loading and storing" • Can be 1-20 characters in size" CMPE12 – Fall 2011 – Joel Ferguson" - 8" Assembler directives" • Directives or psuedo-ops give information to the assembler." • Not executed by the program" • All directives start with a period ʻ.ʼ" Directive! Description! ORIG Where to start in placing things in memory" FILL Declare a memory location (variable)" BLKW Declare a group of memory locations (array)" STRINGZ Declare a group of characters in memory (string)" END Tells assembly where your program source ends" CMPE12 – Fall 2011 – Joel Ferguson" - 9" .ORIG • Tells simulator where to put your code in memory (starting location)" • Only one ORIG allowed per program module" • PC is set to this address at start up" • Similar to the main() function in C" • Example: the standard convention is" • orig CMPE12 – Fall 2011 – Joel Ferguson" x3000" - 10" .END • Tells the assembler where your program ends" • Only one END allowed in your program module" • Thatʼs where the assembler stops assembling, NOT where the execution stops!" CMPE12 – Fall 2011 – Joel Ferguson" - 15" TRAP (System Calls)" Very tedious and dangerous for a programmer to deal with I/O " This is why we like to have an OS " Need an instruction to get its attention." Use the TRAP instruction and a trap vector." CMPE12 – Fall 2011 – Joel Ferguson" - 16" Trap Service Routines The LC-3 assembler provides “pseudo-instructions” for each trap code, so you donʼt have to remember them." Trap Vector! Assembler Name! x20 GETC x21 OUT x22 PUTS x23 Read a character from console into R0, not echoed " Write the character in R0[7:0] to console." Write string of characters to console Start with character at address contained in R0 Stops when 0x0000 is encountered." IN Print a prompt to console and read in a single character into R0 Character is echoed." PUTSP Write a string of characters to console, characters per address location Start with characters at address in R0 First [7:0] and then [15:0] Stops when 0x0000 is encountered." x24 x25 Usage & Result! HALT CMPE12 – Fall 2011 – Joel Ferguson" Halt execution and print message to console." - 17" To print a character! Trap Examples" ; the char must be in R0[7:0]" TRAP!x21 !! or" OUT! To end the program To read in a character! TRAP!x25 " ; will go into R0[7:0], ! ; no echo.! TRAP!x20! " " HALT! "or" " "" or" GETC! CMPE12 – Fall 2011 – Joel Ferguson" - 18" Loop Done Res Zero M0 M1 ORIG LD LD LD BRz ADD ADD BR ST HALT FILL FILL FILL FILL END x3000 R2, Zero R0, M0 R1, M1 Done R2, R2, R0 R1, R1, -1 Loop R2, Res Simple LC-3 program" x0000 x0000 x0007 x0003 CMPE12 – Fall 2011 – Joel Ferguson" - 19" The assembly process" • Convert assembly language file (.asm) into an executable file (.obj) for the LC-3 simulator." • First Pass:" – scan program file" – find all labels and calculate the corresponding addresses - the symbol table! • Second Pass:" – convert instructions to machine language, using information from symbol table" CMPE12 – Fall 2011 – Joel Ferguson" - 20"