DOC PREVIEW
UW-Madison CS/ECE 252 - Assembly Language and Subroutines

This preview shows page 1-2-3-18-19-36-37-38 out of 38 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 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 38 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 38 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 38 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 38 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 38 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 38 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 38 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to Computer EngineeringChapter 7 & 9.2 Assembly Language and SubroutinesHuman-Readable Machine LanguageAn Assembly Language ProgramLC-3 Assembly Language SyntaxOpcodes and OperandsLabels and CommentsAssembler DirectivesTrap CodesStyle GuidelinesSample ProgramChar Count in Assembly Language (1 of 3)Char Count in Assembly Language (2 of 3)Char Count in Assembly Language (3 of 3)Assembly ProcessFirst Pass: Constructing the Symbol TablePracticeSecond Pass: Generating Machine LanguageSlide 19LC-3 AssemblerObject File FormatMultiple Object FilesLinking and LoadingSkipping Ahead to Chapter 9JSR InstructionJSRJSRR InstructionJSRRReturning from a SubroutineExample: Negate the value in R0Passing Information to/from SubroutinesUsing SubroutinesSaving and Restore RegistersExampleCountChar Algorithm (using FirstChar)CountChar ImplementationFirstChar AlgorithmFirstChar ImplementationIntroduction to Computer EngineeringCS/ECE 252, Spring 2007Prof. Mark D. HillComputer Sciences DepartmentUniversity of Wisconsin – MadisonChapter 7 & 9.2Assembly Language and Subroutines7-3Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Human-Readable Machine LanguageComputers like ones and zeros…Humans like symbols…Assembler is a program that turns symbols intomachine instructions.•ISA-specific:close correspondence between symbols and instruction setmnemonics for opcodeslabels for memory locations•additional operations for allocating storage and initializing dataADD R6,R2,R6 ; increment index reg.00011100100001107-4Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.An Assembly Language Program;; Program to multiply a number by the constant 6;.ORIG x3050LD R1, SIXLD R2, NUMBERAND R3, R3, #0 ; Clear R3. It will; contain the product.; The inner loop;AGAIN ADD R3, R3, R2ADD R1, R1, #-1 ; R1 keeps track ofBRp AGAIN ; the iteration.;HALT;NUMBER .BLKW 1SIX .FILL x0006;.END7-5Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.LC-3 Assembly Language SyntaxEach line of a program is one of the following:•an instruction•an assember directive (or pseudo-op)•a commentWhitespace (between symbols) and case are ignored.Comments (beginning with “;”) are also ignored.An instruction has the following format:LABEL OPCODE OPERANDS ; COMMENTSoptional mandatory7-6Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Opcodes and OperandsOpcodes•reserved symbols that correspond to LC-3 instructions•listed in Appendix Aex: ADD, AND, LD, LDR, …Operands•registers -- specified by Rn, where n is the register number•numbers -- indicated by # (decimal) or x (hex)•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-7Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Labels and CommentsLabel•placed at the beginning of the line•assigns a symbolic name to the address corresponding to lineex:LOOP ADD R1,R1,#-1BRp LOOPComment•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-8Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Assembler DirectivesPseudo-operations•do not refer to operations executed by program•used by assembler•look like instruction, but “opcode” starts with dotOpcode Operand Meaning.ORIGaddress starting address of program.ENDend of program.BLKWn allocate n words of storage.FILLn allocate one word, initialize with value n.STRINGZn-character stringallocate n+1 locations, initialize w/characters and null terminator7-9Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Trap CodesLC-3 assembler provides “pseudo-instructions” foreach trap code, so you don’t have to remember them.Code Equivalent DescriptionHALT TRAP x25Halt execution and print message to console.IN TRAP x23Print prompt on console,read (and echo) one character from keybd.Character stored in R0[7:0].OUT TRAP x21Write one character (in R0[7:0]) to console.GETC TRAP x20Read one character from keyboard.Character stored in R0[7:0].PUTS TRAP x22Write null-terminated string to console.Address of string is in R0.7-10Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Style GuidelinesUse the following style guidelines to improvethe readability and understandability of your programs:1. Provide a program header, with author’s name, date, etc.,and purpose of program. 2. Start labels, opcode, operands, and comments in same columnfor each line. (Unless entire line is a comment.)3. Use comments to explain what each register does.4. Give explanatory comment for most instructions.5. Use meaningful symbolic names.•Mixed upper and lower case for readability.•ASCIItoBinary, InputRoutine, SaveR16. Provide comments between program sections.7. Each line must fit on the page -- no wraparound or truncations.•Long statements split in aesthetically pleasing manner.7-11Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Sample ProgramCount the occurrences of a character in a file.Remember 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-12Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.Char Count in Assembly Language (1 of 3);; Program to count occurrences of a character in a file.; Character to be input from the keyboard.; Result to be displayed on the monitor.; Program only works if no more than 9 occurrences are found.; ;; Initialization;.ORIG x3000AND R2, R2, #0 ; R2 is counter, initially 0LD R3, PTR ; R3 is pointer to charactersGETC ; R0 gets character inputLDR R1, R3, #0 ; R1 gets first character;; Test character for end of file;TEST ADD R4,


View Full Document
Download Assembly Language and Subroutines
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 Subroutines 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 and Subroutines 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?