CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... ·...

Preview:

Citation preview

CSCI2510 Computer Organization

Tutorial 09:Associative mapping

in MASM

Yuhong LIANG

yhliang@cse.cuhk.edu.hk

Outline

• LRU Algorithm

• First-In-First-Out Algorithm

CSCI2510 Tut09: Associative mapping implementation 2

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 3

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 4

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 5

7 7 7 7 7 3 3 3 3 3 3 3 3 3 3 3 3 7 7 7

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 4 4 4 4 4 4 1 1 1 1 1 1 1

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

States

of

cache

1 1 1 1 1 6 6 6 6 10 10 12 12 12 12 12 12 18 18 18

2 2 2 5 5 7 7 7 7 11 11 11 11 11 16 16 16 19 19

3 3 3 3 3 8 8 8 8 8 8 14 14 14 17 17 17 20

4 4 4 4 4 9 9 9 9 13 13 15 15 15 15 15 15

States

of

count

The cache block has not been used for the longest period of time!

LRU Algorithm: Replace the CB that has not been used for the longest

period of time (in the past).

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 6

Input cachesize or the command or address

The input number is equel to -1

Check the cache

Cache hit?

Update the time tableReplacement using LRU

Algorithm(update cache and time table)

Exityes

yes

no

no

print the cache and time table status

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 7

.data:cacheBlocks dd 32 dup(-1); hold the address

time dd 32 dup(-1); hold the count

cacheSize dd 32, 0; the size of cache

CPUAccess dd "%d", 0; the address that cpu access

count dd 0, 0; the current time

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 8

Input cachesize or the command or address

The input number is equel to -1

Check the cache

Cache hit?

Update the time table

Replacement using LRU Algorithm(update cache and time table)

Exityes

yes

no

no

print the cache and time table status

Part 1: check the cachesize

Start:

mov ECX, cacheSize

cmp ECX, -1

je exitprogram

jmp input

Part 2: check the CPUAccess

Input:

mov ECX, CPUAccess

cmp ECX, -1

je exitprogram

mov EAX, 0

jmp check

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 9

Input cachesize or the command or address

The input number is equel to -1

Check the cache

Cache hit?

Update the time tableReplacement using LRU

Algorithm(update cache and time table)

Exityes

yes

no

no

print the cache and time table status

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 10

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 2)

EBP + EAX*4

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 11

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 2)

EAX

ECX != 1

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 12

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 2)

EAX

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 13

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 2)

EAX

EAX!=4

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 14

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 2)

EAX

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 15

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 2)

EAX

ECX = 2

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 16

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 2)

EAX

Jump to hit

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 17

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 5)

EAX

ECX != 4

If CPU access(ECX) 5

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 18

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 5)EAX

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 19

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 5)EAX

EAX = 4

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 20

4

3

2

1

Input:

mov EAX, 0

check:

cmp ECX, [EBP + EAX*4]

je hit

add EAX,1

cmp EAX, cacheSize

je replace

jmp check

cacheBlocks(cachesize = 4,cpu access(ECX) 5)EAX

Jump to replace

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 21

Input cachesize or the command or address

The input number is equel to -1

Check the cache

Cache hit?

Update the time tableReplacement using LRU

Algorithm(update cache and time table)

Exityes

yes

no

no

print the cache and time table status

hit:

mov EBX ,count

mov [ESI + EAX*4], EBX

jmp printcachestatus

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 22

Input cachesize or the command or address

The input number is equel to -1

Check the cache

Cache hit?

Update the time tableReplacement using LRU

Algorithm(update cache and time table)

Exityes

yes

no

no

print the cache and time table status

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 23

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

4

3

2

1

Time table, count = 64

ESI + EAX*4 ESI + EDI*4

EBP + EDI*4EBP + EAX*4

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 24

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX EDI

EBX = 63

4

3

2

1

Time table, count = 64

EAX EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 25

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EDI

EBX = 63

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 26

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EDI

4

3

2

1

Time table, count = 64

EDI

EBX = 63

EBX > 3

EAX

EAX

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 27

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EDI

4

3

2

1

Time table, count = 64

EDI

Jump to record LRU

EAX

EAX

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 28

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX EDI

4

3

2

1

Time table, count = 64

EAX EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 29

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX EDI

4

3

2

1

Time table, count = 64

EBX = 4

EAX EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 30

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX EDI

4

3

2

1

Time table, count = 64

EBX = 3

EAX EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 31

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX EDI

4

3

2

1

Time table, count = 64

EBX = 3

EAX = 1

EBX != 1

EAX EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 32

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX EDI

4

3

2

1

Time table, count = 64

Jump to findLRU

EAX EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 33

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 3

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 34

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 3

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 35

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 3 THEN EBX < 55

Not jmp

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 36

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 4

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 37

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 3

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 38

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 3 EAX=2 SO

EBX != EAX

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 39

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 3

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 40

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 3

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 41

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 3 THEN EBX < 54

Not jmp

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 42

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 4

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 43

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EBX = 3

EDI

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 44

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

3

63

cacheBlocks, count = 64,cpu access 5

EAX

EDI

4

3

2

1

Time table, count = 64

EAX

EDI

EBX = 3 EAX=3 SO

RET

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 45

replace:

mov EAX, 0

mov EDI, 0

call findLRU

mov EBX, CPUAccess

mov [EBP + EDI*4], EBX

mov EBX ,count

mov [ESI + EDI*4], EBX

jmp printcachestatus

findLRU:

mov EBX, [ESI + EDI*4]

add EAX, 1

cmp EBX, [ESI + EAX*4]

jg recordLRU

mov EBX, cacheSize

sub EBX, 1

cmp EAX, EBX

jne findLRU

ret

recordLRU:

mov EDI, EAX

mov EBX, cacheSize

dec EBX

cmp EAX, EBX

jne findLRU

ret

54

55

64

63

cacheBlocks, count = 64, cpu access 5

EAX

EDI

4

3

5

1

Time table, count = 64

EAX

EDI

EBX = 5

EBX = 64

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 46

Input cachesize or the command or address

The input number is equel to -1

Check the cache

Cache hit?

Update the time tableReplacement using LRU Algorithm(update cache and time table)

Exityes

yes

no

no

print the cache and time table status

printcachestatus:

mov EDI, 0

invoke crt_printf, addr stateFormat

call printstate

invoke crt_printf, addr countFormat

mov EDI, 0

call printcount

invoke crt_printf, addr endFormat

jmp input

printstate:

mov EAX, [EBP + EDI*4]

invoke crt_printf, addr outputFormat, EAX

add EDI, 1

cmp EDI, cacheSize

jne printstate

ret

printcount:

mov EAX, [ESI + EDI*4]

invoke crt_printf, addr outputFormat, EAX

add EDI, 1

cmp EDI, cacheSize

jne printcount

ret

Associative mapping implementation

CSCI2510 Tut09: Associative mapping in MASM 47

First-In-First-Out Algorithm

CSCI2510 Tut09: Associative mapping in MASM 48

7 7 7 7 7 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2

0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4 7 7 7

1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0

2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1

States

of

cache

1 1 1 1 1 6 6 6 6 6 6 6 6 6 15 15 15 15 15 15

2 2 2 2 2 2 8 8 8 8 8 8 8 8 8 8 18 18 18

3 3 3 3 3 3 3 3 11 11 11 11 11 11 11 11 11 11

4 4 4 4 4 4 4 4 4 4 14 14 14 14 14 14 14

States

of

count

The cache block has arrived for the longest period of time!

First-In-First-Out Algorithm: Replace the CB that has arrived for the longest period

of time (in the past)

First-In-First-Out Algorithm

CSCI2510 Tut09: Associative mapping in MASM 49

Input cachesize or the command or address

The input number is equel to -1

Check the cache

Cache hit?

Replacement using FIFO Algorithm(update cache and time table)

Exityes

yes

no

no

print the cache and time table status

First-In-First-Out Algorithm

CSCI2510 Tut09: Associative mapping in MASM 50

Assignment3

CSCI2510 Tut09: Associative mapping in MASM 51

• Implement the FIFO Algorithm

• Fill the five code segments:

check, hit, replace, find first,

recordfirst

• Don’t change the start, input and

Output(printstate/printcachestatus/

printcount) code segments!!!

Summary

• LRU Algorithm

• First-In-First-Out Algorithm

52CSCI2510 Tut09: Associative mapping in MASM

Recommended