-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADTF_Kernel_C_Windows.s
151 lines (114 loc) · 2.38 KB
/
ADTF_Kernel_C_Windows.s
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
.include "ADTF_Macros.s"
.file "ADTF_Kernel_C_Windows.s"
#I thought the following might be platform specific.
#I searched around for how to use win32 interupts, but they are platform specific too.
#I was adviced to use the API (e.g. through calls to _system)
#
#If you want to port this game, you should re-implement the below functions
#I tried to keep things simple
.text
.global print_string
.global print_number
.global scan_number
.global exit
.global pause
.global clear_screen
.global save_bytes
.global load_bytes
.global print_error
print_string:
frame
#arg0 = string ptr
call_1 _printf, 8(%ebp)
leave
ret
print_number:
frame
#arg0 = number
call_2 _printf, $number_format, 8(%ebp)
leave
ret
scan_number:
frame
#arg0 = buffer ptr
call_2 _scanf, $number_format, 8(%ebp)
leave
ret
exit:
frame
#arg0 = exit code
call_1 _exit, 8(%ebp)
leave
ret
pause:
frame
call_1 _system, $pause_string
leave
ret
clear_screen:
frame
call_1 _system, $cls
leave
ret
save_bytes:
frame
#arg0 = source pointer
#arg1 = source length (in bytes)
#arg2 = destination file name
#return = if succesful
call_2 _fopen, 16(%ebp), $write_txt
cmpl $0, %eax
je LK1_fail
pushl %eax #save 1 - file pointer
call_4 _fwrite 8(%ebp), $1, 12(%ebp), %eax
pushl %eax #save 2 - bytes written
movl 4(%esp), %eax
call_1 _fclose, %eax
popl %eax #restore 2
cmpl 12(%ebp), %eax
jne LK1_fail
movl $1, %eax
jmp LK1_end
LK1_fail:
call print_error
movl $0, %eax
LK1_end:
leave
ret
load_bytes:
frame
#arg0 = destination pointer
#arg1 = destination length (in bytes)
#arg2 = source file name
#return = if succesful
call_2 _fopen, 16(%ebp), $read_txt
cmpl $0, %eax
je LK2_fail
pushl %eax #save 1 - file pointer
call_4 _fread 8(%ebp), $1, 12(%ebp), %eax
pushl %eax #save 2 - bytes written
movl 4(%esp), %eax
call_1 _fclose, %eax
popl %eax #restore 2
cmpl 12(%ebp), %eax
jne LK2_fail
movl $1, %eax
jmp LK2_end
LK2_fail:
call print_error
movl $0, %eax
LK2_end:
leave
ret
print_error:
frame
#arg0 = text to prepend
call_1 _perror, 8(%ebp)
leave
ret
.data
write_txt: .string "w"
read_txt: .string "r"
cls: .string "cls"
number_format: .string "%d"
pause_string: .string "pause"