Based on slides © McGraw-HillAdditional material © 2004/2005/2006 Lewis/MartinChapter 7AssemblyLanguage7-2CSE 240Revisited: Counting Characters (From Ch 5 & 6) Count the occurrences of a character in a fileRemember this?Count = 0(R2 = 0)Ptr = 1st file character(R3 = M[x3012])Input charfrom keybd(TRAP x23)Done?(R1 ?= EOT)Load char from file(R1 = M[R3])Match?(R1 ?= R0)Incr Count(R2 = R2 + 1)Load next char from file(R3 = R3 + 1, R1 = M[R3])Convert count toASCII character(R0 = x30, R0 = R2 + R0)Print count(TRAP x21)HALT(TRAP x25)NONOYESYES7-3CSE 240Revisited: Counting Characters (From Ch 5 & 6)Count = 0(R2 = 0)Ptr = 1st file character(R3 = M[x3012])Input charfrom keybd(TRAP x23)Done?(R1 ?= EOT)Load char from file(R1 = M[R3])Match?(R1 ?= R0)Incr Count(R2 = R2 + 1)Load next char from file(R3 = R3 + 1, R1 = M[R3])Convert count toASCII character(R0 = x30, R0 = R2 + R0)Print count(TRAP x21)HALT(TRAP x25)NONOYESYES R2 ! 0 (Count) R3 ! M[x3012] (Ptr) Input to R0 (TRAP x23) R1 ! M[R3] R4 ! R1 – 4 (EOT) BRz x???? R1 ! NOT R1 R1 ! R1 + 1 R1 ! R1 + R0 BRnp x???? R2 ! R2 + 1 R3 ! R3 + 1 R1 ! M[R3] BRnzp x???? R0 ! M[x3013] R0 ! R0 + R2 Print R0 (TRAP x21) HALT (TRAP x25) x3012: x4000 x3013: x00307-4CSE 240Assembly Language: Opcode + Operands R2 ! 0 (Count) R3 ! M[x3012] (Ptr) Input to R0 (TRAP x23) R1 ! M[R3] R4 ! R1 – 4 (EOT) BRz x???? R1 ! NOT R1 R1 ! R1 + 1 R1 ! R1 + R0 BRnp x???? R2 ! R2 + 1 R3 ! R3 + 1 R1 ! M[R3] BRnzp x???? R0 ! M[x3013] R0 ! R0 + R2 Print R0 (TRAP x21) HALT (TRAP x25) x3012: x4000 x3013: x0030.ORIG x3000AND R2,R2,#0LD R3,???TRAP x23LDR R1,R3,#0ADD R4,R1,#-4BRz ????NOT R1,R1ADD R1,R1,#1ADD R1,R1,R0BRnp ???ADD R2,R2,#1ADD R3,R3,#1LDR R1,R3,#0BRnzp ???LD R0,???ADD R0,R0,R2TRAP x21TRAP x25.FILL x4000.FILL x0030.END7-5CSE 240Introducing Labels for PC-Relative Locations.ORIG x3000AND R2,R2,#0LD R3,???TRAP x23LDR R1,R3,#0ADD R4,R1,#-4BRz ???NOT R1,R1ADD R1,R1,#1ADD R1,R1,R0BRnp ???ADD R2,R2,#1ADD R3,R3,#1LDR R1,R3,#0BRnzp ???LD R0,???ADD R0,R0,R2TRAP x21TRAP x25.FILL x4000.FILL x0030.END .ORIG x3000 AND R2,R2,#0 LD R3,PTR TRAP x23 LDR R1,R3,#0 ADD R4,R1,#-4TEST BRz OUTPUT NOT R1,R1 ADD R1,R1,#1 ADD R1,R1,R0 BRnp GETCHAR ADD R2,R2,#1GETCHAR ADD R3,R3,#1 LDR R1,R3,#0 BRnzp TESTOUTPUT LD R0,ASCII ADD R0,R0,R2 TRAP x21 TRAP x25PTR .FILL x4000ASCII .FILL x0030 .END7-6CSE 240Assembly: Human-Readable Machine Language Computers like ones and zeros… Humans like mnemonics … Assembler• A program that turns mnemonics into machine instructions• ISA-specific• Mnemonics for opcodes• One assembly instruction translates to one machine instruction• Labels for memory locations• Additional operations for allocating storage and initializing dataADD R6, R2, R6 ; increment index reg.Opcode Dest Src1 Src2 Comment00011100100001107-7CSE 240An 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 1 SIX: .FILL x0006 ; .END7-8CSE 240LC-3 Assembly Language Syntax Each line of a program is one of the following:• An instruction• An assembler directive (or pseudo-op)• A comment Whitespace (between symbols) and case are ignored Comments (beginning with “;”) are also ignored Labels for instructions can be followed by “:” An instruction has the following format:LABEL: OPCODE OPERANDS ; COMMENTSoptional mandatory7-9CSE 240Opcodes and Operands Opcodes• Reserved symbols that correspond to LC-3 instructions• Listed in Appendix A!ex: ADD, AND, LD, LDR, … Operands• Registers -- specified by R0, R1, …, R7• Numbers -- indicated by # (decimal) or x (hex) or b (binary)!Examples: “#10” is “xA” is “b1010”• Label -- symbolic name of memory location• Separated by comma• Number, order, and type correspond to instruction format!ex:ADD R1,R1,R3ADD R1,R1,#3LD R6,NUMBERBRz LOOP7-10CSE 240Labels and Comments Label• Placed at the beginning of the line• Assigns a symbolic name to the address corresponding to line!ex:LOOP: ADD R1,R1,#-1BRp LOOP Comment• Anything after a semicolon is a comment• 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 program7-11CSE 240Assembler Directives Pseudo-operations• Do not refer to operations executed by program• Used by assembler• Look like instruction, but “opcode” starts with dotallocate multiple words of storage,value unspecifiednumber.BLKWMeaningOperandOpcodeallocate n+1 locations,initialize w/characters and nullterminatorn-characterstring.STRINGZallocate one word, initialize withvaluevalue.FILLend of program.ENDstarting address of programaddress.ORIG7-12CSE 240Muti-Word Assembler Directives .BLKW #4 shorthand for:• .FILL x0• .FILL x0• .FILL x0• .FILL x0 .STRINGZ “Hello” shorthand for:• .FILL x48 ; ‘H’• .FILL x65 ; ‘e’• .FILL x6C ; ‘l’• .FILL x6C ; ‘l’• .FILL x6F ; ‘o’• .FILL x0 ; NULL terminator7-13CSE 240Trap Codes LC-3 assembler provides “pseudo-instructions” foreach trap code, so you don’t have to remember themDescriptionEquivalentCodeWrite null-terminated string to console.Address of string is in R0.TRAP x22PUTSRead one character from keyboard.Character stored in R0[7:0].TRAP x20GETCWrite one character (in R0[7:0]) to console.TRAP x21OUTPrint prompt on console,read (and echo) one character from keybd.Character stored in R0[7:0].TRAP x23INHalt execution and print message toconsole.TRAP x25HALT7-14CSE 240Style Guidelines Improve the readability of your programs• Formatting: start labels, opcode, operands in same column• Use comments to explain what each register does• Give explanatory comment for most instructions• Use meaningful symbolic names• Provide comments between program sections• Each line must fit on the page -- no
View Full Document