Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language.

Similar presentations


Presentation on theme: "Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language."— Presentation transcript:

1 Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language

2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside Example 01; 02; Program to multiply a number by six 03; 04.ORIGx LDR1, SIX 06 LDR2, NUMBER 07 ANDR3, R3, #0; clear R3 08; 09; The inner loop 0A; 0BAGAIN ADDR3, R3, R2 0C ADDR1, R1, #-1 ; keep track of iterations 0D BRpAGAIN 0E; 0F HALT 10; 11NUMBER.BLKW1 12SIX.FILLx ; 14.END

3 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside Assembly Language Instructions Formats  LABEL OPCODEOPERANDS; COMMENTS  LABEL PSEUDO-OPS; COMMENTS Label  Label is a symbolic name that refers to a memory location.  It is used to:  indicate the target of a branch instruction, e.g. AGAIN in location 0B  store a value in a memory location, e.g. NUMBER and SIX Comments  intended for humans only: explanation of code, visual display

4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside Pseudo-Ops  Are directives to the assembler  Assembler: the program that will translate human-readable assembly language into machine instructions.  LC-2 Pseudo-Ops: . ORIG address Tells assembler where to locate the program in memory (starting address). .ENDMarks the end of the source program. .BLKW n Set aside a block of n words in memory. .FILL value Store value in the next location in memory .STRINGZ stringStore the string, one character per word, in memory. Add a word of x0000 after the string. .EXTERNALThe label so indicated is allocated in another module.

5 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside Examples.ORIG x3000 AND R1, R1, #0 ADD R1, R1, #10 LD R2, Twenty LD R3, Ess TwentyFILLx0014 Ess.FILL“S”.BLKW2.STRINGZ “Hi”.BLKW 3.END x3000:AND R1, R1, #0 x3001:ADD R1, R1, #10 x3002:LDR2, #4 x3003:LDR3, #5 x3004:x0014 ; x3005:x0053 ; x3006: x3007: x3008:x0048 ; H x3009:x0069 ; i x300A:x0000 ; null terminated string x300B: x300C: x300D:

6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside The Assembly Process Objective  Translate the AL (Assembly Language) program into ML (Machine Language).  Each AL instruction yields one ML instruction word.  Interpret pseudo-ops correctly. Problem  An instruction may reference a label.  If the label hasn’t been encountered yet, the assembler can't form the instruction word Solution  Two-pass assembly

7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside Two-Pass Assembly - 1 First Pass  Scan each line  Keep track of current address  Increment by 1 for each instruction  Adjust as required for any pseudo-ops (e.g..FILL or.STRINGZ, etc.)  For each label  Enter it into the symbol table  Allocate to it the current address  Stop when find.END

8 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside Two-Pass Assembly - 2 Second Pass  Scan each line again  Translate each AL instruction into ML  Look up any symbols in the symbol table  Determine operand field for the instruction  Check for errors if reference crosses page boundaries  A reference to an undefined symbol is an error  Fill memory locations as directed by pseudo-ops  Stop when find.END

9 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside Symbol Table  From the previous example: ; ; Program to multiply a number by six ;.ORIGx3050 x3050LDR1, SIX x3051LDR2, NUMBER x3052ANDR3, R3, #0 ; ; The inner loop ; x3053AGAINADDR3, R3, R2 x3054ADDR1, R1, #-1 x3055BRpAGAIN ; x3056HALT ; x3057NUMBER.BLKW1 x3058SIX.FILLx0006 ;.END

10 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside Object File Each source file is translated into an object file: the executable image. A complete program may include several source and/or object files:  Source files written by the programmer  Library files provided by the system (OS or other) The object files must be linked  Need to provide a copy of the symbol table  Include it in the header of the object file

11 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Slides prepared by Walid A. Najjar & Brian J. Linard, University of California, Riverside Example - Parity Parity is a function that returns a 1 when the number of 1s in a word is odd and 0 when it is even..ORIGx AND R2, R2, x0 ; clear R2 3001LDI R1, Input ; load word into R1 3002CountBRz Report ; if 0, done counting 3003BRp Shift ; if >0, skip ADD 3004ADD R2, R2, x1 ; increment count 3005ShiftADD R1, R1, R1 ; shift left 1 bit 3006BRnzp Count ; go back up 3007ReportAND R3, R2, x1 ; LSB 1 or 0? 3008STI R3, Output ; store results 3009TRAP x25 ; halt program 300AInput.FILL x3200 ; address of input 300BOutput.FILL x3201 ; address of output


Download ppt "Introduction to Computing Systems from bits & gates to C & beyond Chapter 7 LC-2 Assembly Language."

Similar presentations


Ads by Google