-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstDemo.hla
93 lines (74 loc) · 2.69 KB
/
ConstDemo.hla
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
program ConstDemo;
#include ( "stdlib.hhf" );
const
MemToAllocate := 4_000_000;
NumDWords := MemToAllocate div 4;
MisalignBy := 62;
MainRepetitions := 100000;
DataRepetitions := 999_900;
CacheLineSize := 16;
begin ConstDemo;
// console.cls();
stdout.put (
"Memory Alignment Exercise", nl, nl,
"Using a watch (preferably a stopwatch), time the execution of", nl
"the following code to determine how many seconds it takes to", nl
"execute.", nl
nl
"Press Enter to begin timing the code:"
);
// Allocate enough dynamic memory to ensure that it does not
// all fit inside the cache. Note: the machine had better have
// at least four megabytes free or virtual memory will kick in
// and invalidate the timing.
malloc ( MemToAllocate );
// Zero out the memory (this loop really exists just to
// ensure that all memory is mapped in by the OS).
mov ( NumDWords, ecx );
repeat
dec ( ecx );
mov ( 0, (type dword [eax+ecx*4]));
until ( !ecx ); // Repeat until ECX = 0.
// Okay, wait for the user to press the Enter key.
stdin.readLn();
// Note: as processors get faster and faster you may
// want to increase the size of the following constant.
// Execution time for this loop should be approximately
// 10 - 30 seconds.
mov ( MainRepetitions, edx );
add ( MisalignBy, eax ); // Force misalignment of data.
repeat
mov ( DataRepetitions, ecx );
align ( CacheLineSize );
repeat
sub ( 4, ecx );
mov ( [eax+ecx*4], ebx );
mov ( [eax+ecx*4], ebx );
mov ( [eax+ecx*4], ebx );
mov ( [eax+ecx*4], ebx );
until ( !ecx );
dec ( edx );
until ( !edx ); // Repeat until EAX is zero.
stdout.put ( stdio.bell, "Stop timing and record time spent", nl, nl );
// Okay, time the aligned access.
stdout.put ( "Press Enter again to begin timing access to aligned variable:" );
stdin.readLn();
// Note: if you change the constant above, be sure to change
// this one, too!
mov ( MainRepetitions, edx );
sub ( MisalignBy, eax ); // Realign the data
repeat
mov ( DataRepetitions, ecx );
align ( CacheLineSize );
repeat
sub ( 4, ecx );
mov ( [eax+ecx*4], ebx );
mov ( [eax+ecx*4], ebx );
mov ( [eax+ecx*4], ebx );
mov ( [eax+ecx*4], ebx );
until ( !ecx );
dec ( edx );
until ( !edx ); // Repeat until EAX is zero.
stdout.put ( stdio.bell, "Stop timing and record time spent", nl, nl );
free ( eax );
end ConstDemo;