-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_out.asm
181 lines (159 loc) · 2.85 KB
/
in_out.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
; Read a signed number from keybord and return in rax and write it to consol as a string
; using syscall
%include "sys-equal.asm"
;----------------------------------------------------
newLine:
push rax
mov rax, NL
call putc
pop rax
ret
;---------------------------------------------------------
putc:
push rcx
push rdx
push rsi
push rdi
push r11
push ax
mov rsi, rsp ; points to our char
mov rdx, 1 ; how many characters to print
mov rax, sys_write
mov rdi, stdout
syscall
pop ax
pop r11
pop rdi
pop rsi
pop rdx
pop rcx
ret
;---------------------------------------------------------
writeNum:
push rax
push rbx
push rcx
push rdx
sub rdx, rdx
mov rbx, 10
sub rcx, rcx
cmp rax, 0
jge wAgain
push rax
mov al, '-'
call putc
pop rax
neg rax
wAgain:
cmp rax, 9
jle cEnd
div rbx
push rdx
inc rcx
sub rdx, rdx
jmp wAgain
cEnd:
add al, 0x30
call putc
dec rcx
jl wEnd
pop rax
jmp cEnd
wEnd:
pop rdx
pop rcx
pop rbx
pop rax
ret
;---------------------------------------------------------
getc:
push rcx
push rdx
push rsi
push rdi
push r11
sub rsp, 1
mov rsi, rsp
mov rdx, 1
mov rax, sys_read
mov rdi, stdin
syscall
mov al, [rsi]
add rsp, 1
pop r11
pop rdi
pop rsi
pop rdx
pop rcx
ret
;---------------------------------------------------------
readNum:
push rcx
push rbx
push rdx
mov bl,0
mov rdx, 0
rAgain:
xor rax, rax
call getc
cmp al, '-'
jne sAgain
mov bl,1
jmp rAgain
sAgain:
cmp al, NL
je rEnd
cmp al, ' ' ;Space
je rEnd
sub rax, 0x30
imul rdx, 10
add rdx, rax
xor rax, rax
call getc
jmp sAgain
rEnd:
mov rax, rdx
cmp bl, 0
je sEnd
neg rax
sEnd:
pop rdx
pop rbx
pop rcx
ret
;-------------------------------------------
printString:
push rax
push rcx
push rsi
push rdx
push rdi
mov rdi, rsi
call GetStrlen
mov rax, sys_write
mov rdi, stdout
syscall
pop rdi
pop rdx
pop rsi
pop rcx
pop rax
ret
;-------------------------------------------
; rsi : zero terminated string start
GetStrlen:
push rbx
push rcx
push rax
xor rcx, rcx
not rcx
xor rax, rax
cld
repne scasb
not rcx
lea rdx, [rcx -1] ; length in rdx
pop rax
pop rcx
pop rbx
ret
;-------------------------------------------