-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgradient.asm
137 lines (109 loc) · 1.8 KB
/
gradient.asm
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# Data section
.data;
# 800 * 600 * 4 (BGRA byte order)
PIXEL_BUFFER:
.zero 1_920_000;
EVENT:
.zero 256;
WINDOW_TITLE:
.stringz "UVM Gradient Example";
MS_STR:
.stringz " milliseconds to render\n";
# Code section
.code;
# Local 0, 1, width and height
push 800;
push 600;
# Create the window
get_local 0;
get_local 1;
push WINDOW_TITLE;
push 0;
syscall window_create;
pop;
# Local 2: Y=0
push 0;
# Local 3: X=0
push 0;
# Start time in ms, local 4
syscall time_current_ms;
# For each row
LOOP_Y:
# For each column
# X = 0
push_i8 0;
set_local 3;
LOOP_X:
# Compute pixel address (u32 color)
get_local 2; # Y
push_u64 800;
mul_u64;
get_local 3;
add_u64; # Y * 800 + X
push_u64 4;
mul_u64; # (Y * 800 + X) * 4
# Compute red color:
# Y * 256 / 600
get_local 2;
push_u64 256;
mul_u64;
push_u64 600;
div_i64;
# Lshift red
push 16;
lshift_u64;
# Compute blue color:
# X * 256 / 800
get_local 3;
push_u64 256;
mul_u64;
push_u64 800;
div_i64;
# R | B
or_u64;
# Store pixel color
store_u32;
# X = X + 1
get_local 3;
push_i8 1;
add_u64;
set_local 3;
# Loop until done writing pixels
get_local 3;
get_local 0;
lt_i64;
jnz LOOP_X;
# Y = Y + 1
get_local 2;
push_i8 1;
add_u64;
set_local 2;
# Loop for each row
get_local 2;
get_local 1;
lt_i64;
jnz LOOP_Y;
# End time in ms
syscall time_current_ms;
# Compute render time in ms
get_local 4;
sub_u64;
syscall print_i64;
push MS_STR;
syscall print_str;
push 0;
push PIXEL_BUFFER;
syscall window_draw_frame;
# Wait until the user closes the window
WAIT_FOR_EVENT:
push EVENT;
syscall window_wait_event;
push EVENT;
load_u16;
# Check for EVENT_QUIT
push_u32 0;
eq_u64;
jz WAIT_FOR_EVENT;
# End program
push 0;
ret;