-
Notifications
You must be signed in to change notification settings - Fork 0
/
grey-scale.asm
57 lines (48 loc) · 1.08 KB
/
grey-scale.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
section .text
global grey_scale:
; Arguments:
; rdi - address of source matrix
; rsi - address of results matrix
; rdx - number of columns
; rcx - number of lines
; r8 - address to [3] color variables to determine the weighted average
grey_scale:
; Determine number of iterations = columns * lines.
; Store the result in the rcx.
mov rax, rdx
mul rcx
mov rcx, rax
; Loop rcx times.
compute:
; Red.
mov rax, 0
mov al, BYTE[rdi]
mul BYTE[r8]
mov rdx, rax
inc rdi
; Green.
mov rax, 0
mov al, BYTE[rdi]
mul BYTE[r8+1]
mov r9, rax
inc rdi
; Blue.
mov rax, 0
mov al, BYTE[rdi]
mul BYTE[r8+2]
mov r10, rax
inc rdi
; Add three colors ...
mov rax, rdx
add rax, r9
add rax, r10
; Divide the sum by 256 = 0x100.
; We may shift the value to the right instead.
; 256 = 10 000 000 in binary representation (8 bits).
; Therefore, we should shift 8 bits.
shr rax, 8
; Store the color in the result array.
mov WORD[rsi], ax
inc rsi
loop compute
ret