31
Computer Architecture MT 2011 A3 Computer Architecture Engineering Science 3rd year A3 Lectures Prof David Murray [email protected] www.robots.ox.ac.uk/dwm/Courses/3CO Michaelmas 2000 1/1

A3 Computer Architecture Engineering Science 3rd year A3 Lectures

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Computer Architecture MT 2011

A3 Computer Architecture

Engineering Science

3rd year A3 Lectures

Prof David Murray

[email protected]/∼dwm/Courses/3CO

Michaelmas 2000

1 / 1

Computer Architecture MT 2011

5. Macrolevel support for High LevelLanguages:

Addressing Modes and Compilation

3A3 Michaelmas 2000

2 / 1

Computer Architecture MT 2011

In the last lectures ...

we concentrated effort on the register level, looking both at theraw hardware and how it enables register transfers

we considered the relationship between themacro-level (assembler mnemonics or RTL-like statements suchas AC← 〈x 〉); and themicro-level (where we dealt with properly with the detailedRegister transfers.

3 / 1

Computer Architecture MT 2011

In this lecture ...

we look at the support supplied to the high level programmer bythe macro-level

Assembler used comparatively rarely — most use a high-levellanguage (C, Pascal, Fortran etc)

However, although modern compilers produce very efficientcode, there are always short cuts in assembler ...

In particular we look atmemory addressing modescompilation

4 / 1

Computer Architecture MT 2011

Memory addressing modes

Your knowledge of memory hardware tells you that memoryworks in just one way:

stick the address on the address linesset READ or WRITE lineIf reading set the CS line and clock the MBRIf writing clock the CS line

So what are the different addressing modes?The different modes of memory addressing

don’t refer to hardwaredo refer to different ways of using what you read from the memory.

We will look atimmediate addressingdirect addressingindirect addressingindexed addressing

5 / 1

Computer Architecture MT 2011

Immediate Addressing

Written LDA #x MOVE #x,D0Immediate addressing really doesn’tinvolve further memory addressingafter the instruction fetch.It provides a method of specifying aconstant number in the operand.

loads the accumulator immediatelywith the operand x .

AC ← IR (address)

Lo, there is a direct link from the IR(address) to the AC.

Memory

MAR

ALU

CUAC

Control Lines

IR(opcode)

SPPC

IR(address)

Status

MBR

IR

Inc(PC)

6 / 1

Computer Architecture MT 2011

Other instructions can use Immediate Addressing

For exampleADD #22ADD #22,D0

Indicates that 22 is not anaddress but a constant to beadded immediately to thecontents of the accumulator.In RTL, AC←AC + 22Compare with AC←AC + 〈22 〉Is there are problemimplementing this on the BSA?

Memory

MAR

ALU

CUAC

Control Lines

IR(opcode)

SPPC

IR(address)

Status

MBR

IR

Inc(PC)

7 / 1

Computer Architecture MT 2011

Example 1 of high-level code using ImmediateAddressing

int n;...n=n+10;

Assume that after compilation the memory address of n is man.Compilation might appear like:

LDA man MOVE man,DOADD #10 ADD #10,DOSTA man MOVE DO,man

On more advanced processors than the BSA (que?), it ispossible to request that a number be stored in a register onboard the cpu (if space permits).For example in C the request would be:

register int n;...n=n+10;

Supposing that n gets stored in accumulator D7, the code mightget compiled as

ADD #10,D7

This is obviously faster than before!

8 / 1

Computer Architecture MT 2011

Eg2 of high-level code using Immediate Addressing

Suppose we were usingconditional in C:

int n;...if( n>7 && n<25) {

statements A ...}else {

statements B ...}statements C ...

In assembler this might beMOVE ma_n,D0 from address ma n into D0CMP #8,D0 Sets flags on D0−8.BMI LABB Branch to LABB if N flag set.CMP #25,D0 Set flags on D0-25BPL LABB Branch if positive to LABB

LABA: statements A...

JMP LABCLABB: statements B

...LABC: statements C

Note the “line labels” LABA, LABB, LABC,and remember these become memory ad-dresses.

9 / 1

Computer Architecture MT 2011

Direct Addressing (aka absolute addressing)

Already used direct addressing!The operand is the address of the data you require.The operand is a pointer to the data.

LDA x MOVE x,D0

MAR ← IR (address)MBR ← 〈MAR 〉

AC ← MBR

Again, other commands can use the direct addressing mode, forexample:

ADD x ADD x,D0

MAR ← IR (address)MBR ← 〈MAR 〉

AC ← MBR + AC

10 / 1

Computer Architecture MT 2011

Example of both

1 7

3

5

23

20

1123

22

21

20

19

Given the memory contentswhat does Location 23 containafter

LDA #21STA 22ADD #1ADD 22STA 23

11 / 1

Computer Architecture MT 2011

Indirect addressingLDA (x) MOVE [x],D0

In indirect addressing, theoperand is the address of the address of the data.Ie, at address x we don’t find the data but rather another address.We then have to look at this new address to find the data.Alternatively we could say that the operand is a pointer to apointer to the data.Obvious that we need an extra memory access to useindirection, so it must be slower.

MAR ← IR (address)MBR ← 〈MAR 〉

IR (address) ← MBRMAR ← IR (address)MBR ← 〈MAR 〉

AC ← MBR

12 / 1

Computer Architecture MT 2011

Comparison ...

Comparison of LDA #2, LDA 2 and LDA (2).

0

1

2

3

4

5

6

47

47

47

38

38

Immediate Direct Indirect

2

LOAD #2 LOAD 2 LOAD (2)

4747

3838

13 / 1

Computer Architecture MT 2011

Why is indirect addressing used ...Given that it takes more time?Hill and Peterson (Ch12):

sometimes an advantage to have small programs.Using indirect addressing, a short opcode (eg in ourmodel 8bit) opcode address can access a 16bitaddress.

Other books:eases the use of pointers in a high level language,allowing complicated data structures to be constructed.

Hayes:useful to be able to change the location of x, withoutchanging any of the address fields of instructions (ieoperands) that refer to x.

It is this last point that is the CRUCIAL one...Best understood in the context of compilation of a high-levellanguage.

14 / 1

Computer Architecture MT 2011

Later ... but for now ...let’s look at indirect address accessing array elementsAn array comprises data that occupy contiguous locations inmemory.Suppose that the pointer to the first member of the array is heldin address 60.(That is, if we look in the memory at address 60 we shall see theaddress of the first element, not the first element itself.)Assume that the array starts at address 410, the next element isin 411, and so on.This snippet of code adds up the members

LDA #410 Load AC immediate with 410STA 60 Store this number in address 60LDA #0 Zero the AC

NEXT ADD (60) Add indirect. The contents ofaddress 410 are summed

INC 60 Increment the contents ofaddress 60 => increment thepointer to 411

JMP NEXT Loop back15 / 1

Computer Architecture MT 2011

Register Addressing

The method we just saw required an extra main-memory read toobtain the pointer, and an extra main-memory write to incrementthe pointer, and is therefore potentially slow.

However cpus typically provide a number of registers fortemporary storage which avoid the need to hold and accesspointers in main memory, with obvious savings in time.

This is register addressingmany exotic flavours availableall are playing at indirection in one form.

16 / 1

Computer Architecture MT 2011

Indexed addressing: an example of registeraddressing

An index register holds an offset from a given address

the effective address is the sum of the two.

The index register can be loaded separately, and can beincremented or decremented.

17 / 1

Computer Architecture MT 2011

Two ExamplesSuppose the register is called the X register, and the start of anarray we wish to access is located at 0400 in memory, then thiswill add the elements:

LDX #0 //set the index register to 0LDA #0 //set the ac to 0

NEXT ADD 0400,X //indexed addressingINX //increment the index register XJMP NEXT

Another use is the addition of two arrays to create a third as inc[i] = a[i] + b[i]. Assume a[] is at 400, b[] is at 500, and theresult c[] is to be put at 600:

LDX #0 //set the index register to 0LDA #0

NEXT LDA 0400,XADD 0500,XSTA 0600,XINX //increment the index register XJMP NEXT

18 / 1

Computer Architecture MT 2011

More on Indirection

In the context of a high-level languageIndirect addressing is essential if access is to be madeto memory allocated at run-time rather thancompile-time by a high-level language.

To understand this, we need first to understand a little moreabout what happens during compilation of a high-level program.

High-level→ assembler→ binary code

As hinted at in Lecture 4, the aim is to produce a program whereinstructions are occupy one contiguous section of memoryallocated data memory lies above the instructionsand further unused memory (the heap) lies above that.

19 / 1

Computer Architecture MT 2011

Compilation

Compilers works in two passes:1 reads the file of high level statements, checks the syntax, and

replaces each statement with the relevant sequence ofassembler instructions.Places declared variables, and the names of labels, into asymbol table.Writes instructions and table to a temporary file.

2 Since each instruction is of known length, the compiler canassociate with it provisional memory addresses, relative to somearbitrary offset.After the first pass, the compiler knows the full extent of theprogram — or more strictly the instructions.It can replace locations in the symbol table by proper locations inmemory.

20 / 1

Computer Architecture MT 2011

Compilation Example: First pass

Here is the snippet of code:

int a,b,c;a=1;b=2;c=a+b;if(c!=0) {

a=3;}b=4;

After reading the declaration of variables, the compiler couldstart making the symbol table, where BOD indicates the (as yetunknown) Beginning Of Data.

Name SymTab Actual Location Wordsa var0 BOD 1b var1 BOD+1 1c var2 BOD+2 1

21 / 1

Computer Architecture MT 2011

First pass /page2Let beginning of the programbe located at BOP.

Address InstructionBOP LDA #1BOP+1 STA var0BOP+2 LDA #2BOP+3 STA var1BOP+4 ADD var0BOP+5 STA var2BOP+6 BEQ label0

At this point, the compilerknows that the label will bejust after the closing curlybracket in C, but doesn’t knowwhere that is yet in memory,because it hasn’t worked outthe length of the instructionswith the curly brackets.

So it adds an entry to thesymbol table:Name SymTab Actual Words

Locationa var0 BOD 1b var1 BOD+1 1c var2 BOD+2 1

lab0 ? -

int a,b,c;a=1;b=2;c=a+b;if(c!=0) {

a=3;}b=4;

22 / 1

Computer Architecture MT 2011

First pass /page3

The compiler grinds onwith more statements

Address InstructionBOP LDA #1BOP+1 STA var0BOP+2 LDA #2BOP+3 STA var1BOP+4 ADD var0BOP+5 STA var2BOP+6 BEQ label0BOP+7 LDA #3BOP+8 STA var0BOP+9 LDA #4

After the LDA #4instruction, the compilerknows that lab0 is actualBOP+9, so

Name SymTab Actual Location Wordsa var0 BOD 1b var1 BOD+1 1c var2 BOD+2 1

lab0 BOP+9 -

Then it carries on ...Address InstructionBOP LDA #1BOP+1 STA var0BOP+2 LDA #2BOP+3 STA var1BOP+4 ADD var0BOP+5 STA var2BOP+6 BEQ label0BOP+7 LDA #3BOP+8 STA var0BOP+9 LDA #4BOP+10STA var1BOP+11HALT

23 / 1

Computer Architecture MT 2011

First pass /page4to repeat

Address InstructionBOP LDA #1BOP+1 STA var0BOP+2 LDA #2BOP+3 STA var1BOP+4 ADD var0BOP+5 STA var2BOP+6 BEQ label0BOP+7 LDA #3BOP+8 STA var0BOP+9 LDA #4BOP+10 STA var1BOP+11 HALT

At the end of the program,the compiler knows thatthe beginning of data BOD= BOP+12.

So it can rewrite thesymbol table as:

Name SymTab Actual WordsLocation

a var0 BOP+12 1b var1 BOP+13 1c var2 BOP+14 1

lab0 BOP+9 -

Here endeth the 1st pass.

24 / 1

Computer Architecture MT 2011

Second passThere are now twopossibilities for the secondpass. The compiler caneither

rewrite var0, var1 andvar2 and lab0 leavingBOP as a variable to befilled in at run time; orcan set a value a valuefor BOP.

Choosing the latter withBOP=100 (dec), we endup with:

Address Instruction100 LDA #1101 STA 112102 LDA #2103 STA 113104 ADD 112105 STA 114106 BEQ 109107 LDA #3108 STA 112109 LDA #4110 STA 113111 HALT112 0113 0114 0

Now simply replace theopcodes by the relevantbinary (as we did in aprevious lecture) and weend up with executablemachine code.

25 / 1

Computer Architecture MT 2011

Back to indirect addressing ...

Now we can understand why indirect addressing is essential forrun-time memory allocation.

In the compilation example we allocated memory for three 1Wordinteger quantities at compile time.

int a,b,c;...

This meant that the compile could work out exactly what memoryit should reserve, and how to address it in the instructions.

26 / 1

Computer Architecture MT 2011

Back to indirect addressing ...

Now suppose the high level programmer wishes to reserve somememory for two 1D ar-rays (or vectors) of 1Word objects. The programmer might declare

int array1[26];int array2[26];array1[0] = ’a’; array1[1] = ’b’;etcarray2[0] = ’A’; array2[1] = ’B’;etc

The Compiler reserves space in the data section. After pass 1,compiler knows the size of program and all data it can work outhow to access the arrays.

27 / 1

Computer Architecture MT 2011

Indirect addressing and compilation/ctdint array1[26];int array2[26];array1[0] = ’a’;array1[1] = ’b’;etcarray2[0] = ’A’;array2[1] = ’B’;etc

Suppose in our example that BOPis at address n and the size ofthe program and other fixed-sizedata is q, then the first elementof array1, array1[0] would be refer-enced as

array1[0]↔ 〈n+q 〉array1[k]↔ 〈n+q+k 〉and the first element of array2 by

array2[0]↔ 〈n+q+26 〉 and so on.

a

b

c

z

A

B

Program

Other fixed size data

n

n+q+26

n+q+25

n+q

array1

26 Bytes

array226 Bytes

28 / 1

Computer Architecture MT 2011

But using run-time allocation ...

Suppose however that the sizes of array1 and array2 could notbe determined until the program was running.

Suppose for example that the size of array1 needed to be 28.The compiler could not have referred to array2[0] asarray2[0]↔ 〈n+q+28 〉because the value 28 was not known at compilation time.

What’s the fix?One solution would be to rewrite the program during execution,replacing unknown addresses.

However, this is a recipe for chaos, and not done.

29 / 1

Computer Architecture MT 2011

Use indirection, change data, not program

The solution is to declare at compilation two pieces of storagethat will hold the addresses of the arrays array1 and array2when they become known.

The addresses a and b of these pieces of storage ARE known atcompilation time and are therefore available to the program.

During exection when the memory for the arrays is allocated, theaddresses p and p + 28 are placed in a and b.

Now the array members are accessed indirectly:For example, the i-th element of array1, ie array[i], would beaccessed indirectly as 〈 〈a 〉+i 〉.The i=th element of array2 is accessed as 〈 〈b 〉+i 〉.

By using indirection, the program need no “know” what p is.

30 / 1

Computer Architecture MT 2011

Use indirection, change data, not program

Contents are meaningless

Contents are meaningless

Program

Other fixed size data

Other fixed size data

n

a

b

Program

Other fixed size data

Other fixed size data

n

a

b

p

p+28

array1

array2

p

p+28

before execution

After Compilation After allocation

during execution

7Bytes

28Bytes

31 / 1