52
CSCI2510 Computer Organization Tutorial 09:Associative mapping in MASM Yuhong LIANG [email protected]

CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

CSCI2510 Computer Organization

Tutorial 09:Associative mapping

in MASM

Yuhong LIANG

[email protected]

Page 2: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

Outline

• LRU Algorithm

• First-In-First-Out Algorithm

CSCI2510 Tut09: Associative mapping implementation 2

Page 3: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 3

Page 4: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

LRU Algorithm

CSCI2510 Tut09: Associative mapping in MASM 4

Page 5: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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).

Page 6: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 7: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 8: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 9: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 10: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 11: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 12: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 13: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 14: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 15: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 16: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 17: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 18: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 19: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 20: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 21: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 22: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 23: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 24: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 25: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 26: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 27: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 28: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 29: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 30: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 31: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 32: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 33: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 34: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 35: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 36: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 37: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 38: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 39: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 40: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 41: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 42: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 43: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 44: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 45: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 46: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 47: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

Associative mapping implementation

CSCI2510 Tut09: Associative mapping in MASM 47

Page 48: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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)

Page 49: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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

Page 50: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

First-In-First-Out Algorithm

CSCI2510 Tut09: Associative mapping in MASM 50

Page 51: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

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!!!

Page 52: CSCI2510 Computer Organization Tutorial 09:Associative ...mcyang/csci2510/2018F/Tut09... · CSCI2510 Tut09: Associative mapping in MASM 21 Input cachesize or the command or address

Summary

• LRU Algorithm

• First-In-First-Out Algorithm

52CSCI2510 Tut09: Associative mapping in MASM