-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.s
More file actions
156 lines (143 loc) · 2.69 KB
/
Copy pathutils.s
File metadata and controls
156 lines (143 loc) · 2.69 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
.include "sys_call-inl.s"
.global msleep
.global itoa
.global next_rect_pos
.global srand
.global rand
.text
#=======================
#-----------------------
# func msleep(%eax ms)
# ms(%eax): ms to sleep
.bss
.lcomm timespec, 8
.lcomm rem, 8
.text
.type msleep, @function
msleep:
movl $1000000, %ecx
mull %ecx
movl %eax, timespec + 4
nanosleep $timespec, $rem
ret
#-----------------------
# func itoa
# number(%ax, int), buf(%edi, char*) -> count(%ecx)
.data
decimal_map: .ascii "0123456789"
.text
.type itoa, @function
itoa:
xorl %ecx, %ecx
movb $10, %bl
itoa_mod_again:
div %bl
movzbl %ah, %edx
movb decimal_map(, %edx, 1),%bh
movb %bh, (%edi, %ecx, 1)
incl %ecx
xorb %ah, %ah
cmpb $0, %al
jnz itoa_mod_again
# reverse [edi, esi]
movl %edi, %esi
addl %ecx, %esi
decl %esi
itoa_reverse_again:
cmpl %edi, %esi
jbe itoa_exit
movb (%esi), %al
xchgb %al, (%edi)
movb %al, (%esi)
decl %esi
incl %edi
jmp itoa_reverse_again
itoa_exit:
ret
#-----------------------
# func next_rect_pos
# start : row(%si), col(%di)
# width : %cl
# height : %ch
# current : row(%ax), col(%bx)
# -> next : row(%ax), col(%bx)
.type next_rect_pos, @function
next_rect_pos:
cmpw %si, %ax
je next_rect_pos_at_top
cmp %di, %bx
je next_rect_pos_at_left
# %dx as bottom row
mov %si, %dx
pushw %cx
movzbw %ch, %cx
add %cx, %dx
dec %dx
popw %cx
cmp %dx, %si
je next_rect_pos_at_bottom
next_rect_pos_at_right:
inc %ax
cmp %dx, %ax
jbe next_rect_pos_end
# out of range
mov %dx, %ax
dec %bx
jmp next_rect_pos_end
next_rect_pos_at_top:
incw %bx
movzbw %cl, %cx
addw %cx, %di
cmpw %di, %bx
jb next_rect_pos_end
# out of range
incw %ax
movw %di, %bx
decw %bx
jmp next_rect_pos_end
next_rect_pos_at_left:
decw %ax
cmpw %si, %ax
jae next_rect_pos_end
# out of range
movw %si, %ax
movw %di, %bx
incw %bx
jmp next_rect_pos_end
next_rect_pos_at_bottom:
decw %bx
cmpw %di, %bx
jae next_rect_pos_end
# out of range
decw %ax
movw %di, %bx
next_rect_pos_end:
ret
#-----------------------
# func srand
# ax(seed)
.bss
rand_seed: .word 0
.text
.type srand, @function
srand:
movw %ax, rand_seed
ret
#-----------------------
# func rand
# -> ax(randomized number)
# seed = (seed * 217 + 25111) % 31111
.type rand, @function
rand:
movw $217, %ax
# ax * rand_seed = dx:ax
mulw rand_seed
add $25111, %ax
# adc $0, %dx
# movw $31111, %cx
# # dx:ax / cx = ax ... dx
# divw %cx
# mov %dx, rand_seed
# mov %dx, %ax
mov %ax, rand_seed
ret