-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment10.asm
210 lines (150 loc) · 2.18 KB
/
assignment10.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
%macro print 1
mov rdi,formatpf
sub rsp,8
movsd xmm0,[%1]
mov rax,1
call printf
add rsp,8
%endmacro
%macro scan 1
mov rdi,formatsf
sub rsp,8
mov rax,0
mov rsi,rsp
call scanf
mov r8,qword[rsp]
mov qword[%1],r8
add rsp,8
%endmacro
%macro printc 2
mov rdi,formatpfc
sub rsp,8
movsd xmm0,[%1]
movsd xmm1,[%2]
mov rax,2
call printf
add rsp,8
%endmacro
%macro scall 4
mov rax,%1
mov rdi,%2
mov rsi,%3
mov rdx,%4
syscall
%endmacro
section .data
formatpf db "%lf",10,0
formatsf db "%lf",0
formatpfc db "%lf + i%lf",10,0
msg1 db "Enter the coefficients",10
len1 equ $-msg1
msg2 db "Root 1:"
len2 equ $-msg2
msg3 db "Root 2:"
len3 equ $-msg3
msg4 db 10
len4 equ $-msg4
section .bss
a resq 1
b resq 1
c resq 1
disc resq 1
bsq resq 1
fac resq 1
four resq 1
two resq 1
root1 resq 4
root2 resq 4
real resq 4
imaginary resq 4
section .text
extern printf
extern scanf
global main
main:
scall 1,1,msg1,len1
; Get the coefficients
scan a
scan b
scan c
finit
fldz
; Calculating b square
fld qword[b]
fmul qword[b]
fstp qword[bsq]
; Calculating 4ac
mov qword[four],4
fild qword[four]
fmul qword[a]
fmul qword[c]
fstp qword[fac]
; Calculating b^2-4ac
fld qword[bsq]
fsub qword[fac]
fstp qword[disc]
; Calculating 2a
mov qword[two],2
fild qword[two]
fmul qword[a]
fstp qword[a]
fldz
; Checking whether disc is less than zero or not
btr qword[disc],63
jc img
realroots:
; real sqrt(b^2-4ac)
fld qword[disc]
fsqrt
fstp qword[disc]
; Root 1
fsub qword[b]
fadd qword[disc]
fdiv qword[a]
fstp qword[root1]
; Root 2
fldz
fsub qword[b]
fsub qword[disc]
fdiv qword[a]
fstp qword[root2]
scall 1,1,msg2,len2
print root1
scall 1,1,msg4,len4
scall 1,1,msg3,len3
print root2
scall 1,1,msg4,len4
jmp exit
img:
fld qword[disc]
fsqrt
fstp qword[disc]
; Root 1 Real Part
fldz
fsub qword[b]
fdiv qword[a]
fstp qword[real]
; Root 1 Imaginary Part
fld qword[disc]
fdiv qword[a]
fstp qword[imaginary]
scall 1,1,msg2,len2
printc real,imaginary
scall 1,1,msg4,len4
; Root 2 Real Part
fldz
fsub qword[b]
fdiv qword[a]
fstp qword[real]
; Root 2 Imaginary Part
fldz
fsub qword[disc]
fdiv qword[a]
fstp qword[imaginary]
scall 1,1,msg3,len3
printc real,imaginary
scall 1,1,msg4,len4
exit:
mov rax,60
mov rsi,0
syscall