DOC PREVIEW
Penn CIS 240 - Assembly Language

This preview shows page 1-2 out of 7 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 7 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Based on slides © McGraw-HillAdditional material © 2004/2005 Lewis/MartinChapter 7AssemblyLanguage7-2CSE 240 There are 10 kinds of people in the world……those that know binary,and those that don’t.7-3CSE 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-4CSE 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[x3013] (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[x3012] R0 ← R0 + R2 Print R0 (TRAP x21) HALT (TRAP x25) x3012: x0030 x3013: x400027-5CSE 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,R0NOT R1,R1BRnp ???ADD R2,R2,#1ADD R3,R3,#1LDR R1,R3,#0BRnzp ???LD R0,???ADD R0,R0,R2TRAP x21TRAP x25.FILL x0030.FILL x4000.END7-6CSE 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,R0NOT R1,R1BRnp ???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,R0 NOT R1,R1 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 x25ASCII .FILL x0030PTR .FILL x4000 .END7-7CSE 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-8CSE 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 ; .END37-9CSE 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 An instruction has the following format:LABEL OPCODE OPERANDS ; COMMENTSoptional mandatory7-10CSE 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 “b1100”• 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-11CSE 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-12CSE 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.ORIG47-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 wraparound or truncationsLong statements split in aesthetically pleasing manner Use structured programming constructs• From chapter 6• Don’t be overly clever (may make it harder to change later) High-level


View Full Document

Penn CIS 240 - Assembly Language

Download Assembly Language
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Assembly Language and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Assembly Language 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?