Presentation is loading. Please wait.

Presentation is loading. Please wait.

CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Assembler Directives and The Symbol Table.

Similar presentations


Presentation on theme: "CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Assembler Directives and The Symbol Table."— Presentation transcript:

1 CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Assembler Directives and The Symbol Table

2 CEG 320/520: Computer Organization and Assembly Language Programming2 Assembler Directives: Introduction Assembler directives give information to the assembler. They can be used to declare variables, create storage space for results, to declare constants, and to label locations in the code to be used as branch destinations. Most assemblers are ‘two-pass assemblers’. –They read through the code once, looking for labels, and use them to create the ‘symbol table’. The symbol table is a mapping between labels and values. –On the second pass, they replace each occurrence of a label with its value from the symbol table. Coding tip: Labels MUST be left justified, not indented. First character must be a letter (not a number). Assembler only looks at first 8 characters.

3 CEG 320/520: Computer Organization and Assembly Language Programming3 Assembler Directives: Origin (ORG) Tells the assembler where to place your code in memory. Operand given is the desired memory location. – ORG $001000 ; sets origin to be $001000 Generally, we’ll put our code at $001000 and our data (declared with DC, DS, etc) at $002000.

4 CEG 320/520: Computer Organization and Assembly Language Programming4 Assembler Directives: Declare Constant (DC) Declares a named variable in memory. The term ‘constant’ is misleading, because the contents of that memory location can be changed. Use DC.B, DC.W, DC.L to declare different sizes of variables. Operand given is initial value of variable. –VAR DC.W $40 ; places $40 in memory location pointed ; to by VAR. Symbol table: the value of a label declared with DC is the memory location of the label.

5 CEG 320/520: Computer Organization and Assembly Language Programming5 Assembler Directives: Declare Storage (DS) Reserves space in memory for the result of a future operation or future variables. Use DS.B, DS.W, DS.L to declare different sizes of empty storage. Operand given is number of spaces to allocate, where the size of a space is determined by the extension (B,W,L) –ARR DS.W 5 ; reserves 5 words in memory for ARR Symbol table: the value of a label declared with DS is the memory location of the label.

6 CEG 320/520: Computer Organization and Assembly Language Programming6 Assembler Directives: equal to (EQU) Declares a constant value. No need for extension here, assembler will treat the number appropriately. Operand given is value to assign to label. –CONST EQU 5 ; sets the value of CONST to 5 Symbol table: the value of a label declared with EQU is the value, NOT THE MEMORY LOCATION. EQU does not cause anything to be stored in memory.

7 CEG 320/520: Computer Organization and Assembly Language Programming7 Assembler Directives: Code labels Labels an instruction in the program so that it can be used as a branch target. All instructions are indented; a code label is added by putting a left justified label on the same line. –LOOP ADD D1,D0 ; LOOP is the code label Symbol table: the value of a code label is the memory location of the instruction on that line.

8 CEG 320/520: Computer Organization and Assembly Language Programming8 Assembler Directives: end of code (END) Tells the assembler to stop looking for more code. This should be the last line in any code file you write.

9 CEG 320/520: Computer Organization and Assembly Language Programming9 The Symbol Table Most assemblers are two-pass assemblers. –On the first pass, any labels found in the program are put into the symbol table along with the corresponding numerical value. Code labels – memory location of instruction Variable labels (DS, DC) – memory location of reserved memory Constants (EQU) – value of constant –On the second pass, all labels in the code are replaced with their values from the symbol table. Why use two passes? –If the program contains a branch to a later location in the code (a forward branch), the assembler won’t know where to find the branch target because it would not have seen it yet.

10 CEG 320/520: Computer Organization and Assembly Language Programming10 Example: An Assembly Language Program ORG$001000 MOVE VAL1, D0 MOVE VAL2, D1 MUL D0, D1 ADD #CONST, D1 END ORG$002000 VAL1 DC3 VAL2 DC12 CONSTEQU5 What does the symbol table look like after the first pass of the assembler? What does the code look like after the second pass of the assembler?

11 CEG 320/520: Computer Organization and Assembly Language Programming11 Example: An Assembly Language Program ORG$001000 STARTCLR.LD0; sum = 0; MOVEA.L#ARRAY,A0; ptr points to the 1st array element MOVE.W#SIZE,D1; counter = SIZE; LOOPADD.W(A0)+,D0; sum += *ptr++; SUBQ.W#1,D1; counter -= 1; BGTLOOP; repeat while counter > 0; MOVE.WD0,SUM; Save the sum in memory MOVE.W#228,D7; Magic exit code TRAP#14; more magic exit code ;--------DATA AND CONSTANTS-------------------------------------------- ORG$002000 SIZEEQU9; the size of the array ARRAYDC.W3,7,-5,12,23,15,-12,82,5; the array to sum SUMDS.W1; the final sum END What does the symbol table look like after the first pass of the assembler? What does the code look like after the second pass of the assembler?


Download ppt "CEG 320/520: Computer Organization and Assembly Language Programming1 Assembly Language Programming Assembler Directives and The Symbol Table."

Similar presentations


Ads by Google