FUCK THE POLICE
911 EVERY DAY
Code:
segment .text
global main
extern malloc, printf, scanf
main:
.n equ 0 ; degree of the polynomial
.a equ 8
.b equ 16
.p equ 24 ; polynomial
.pp equ 32 ; p' = derivative
.P equ 40 ; Integral
.i equ 48 ; loop counter
push rbp
mov rbp, rsp
sub rsp, 64
; prompt for and read n
segment .data
.n_prompt:
db "Enter the degree of the polynomial: ",0
segment .text
lea rdi, [.n_prompt]
xor eax, eax
call printf
segment .data
.n_scanf:
db "%ld",0
segment .text
lea rdi, [.n_scanf] ;scanf("%ld", &n)
lea rsi, [rsp+.n]
xor eax, eax
call scanf
; allocate pp, p and P
mov rdi, [rsp+.n] ;double pp[n]
call create
mov [rsp+.pp], rax
mov rdi, [rsp+.n] ;double p[n]
inc rdi
call create
mov [rsp+.p], rax
mov rdi, [rsp+.n] ;double P[n]
add rdi, 2
call create
mov [rsp+.P], rax
; prompt for and read p
segment .data
.p_prompt:
db "Enter %ld numbers for p: ",0
segment .text
lea rdi, [.p_prompt]
mov rsi, [rsp+.n]
inc rsi
xor eax, eax
call printf ;printf("Enter %ld numbers for p: ", n++)
; for ( i = 0; i <= n; i++ ) {
xor ecx, ecx ; i = 0
mov [rsp+.i], rcx
.for mov rdx, [rsp+.n]
cmp rcx, rdx ; i <= n ?
jg .end_for
; scanf ( "%lf", p+i );
segment .data
.p_scanf: db "%lf",0
segment .text
lea rdi, [.p_scanf]
mov rsi, [rsp+.p]
lea rsi, [rsi+8*rcx]
xor eax, eax
call scanf
mov rcx, [rsp+.i]
inc rcx
mov [rsp+.i], rcx
jmp .for
; }
.end_for:
; prompt for and read a and b
segment .data
.a_prompt:
db "Enter a: ", 0
segment .text
lea rdi, [.a_prompt]
xor eax, eax
call printf
segment .data
.a_scanf:
db "%lf",0
segment .text
lea rdi, [.a_scanf] ;scanf("%ld", &a)
lea rsi, [rsp+.a]
xor eax, eax
call scanf
segment .data
.b_prompt:
db "Enter b: ", 0
segment .text
lea rdi, [.b_prompt]
xor eax, eax
call printf
segment .data
.b_scanf:
db "%lf",0
segment .text
lea rdi, [.b_scanf] ;scanf("%ld", &b)
lea rsi, [rsp+.b]
xor eax, eax
call scanf
; compute p prime from p
;pp[] = derivative(n, &p[], &P[])
;for(i = 1; i<=n; i++)
;{
xor ecx, ecx ; i = 1
inc rcx
mov [rsp+.i], rcx
.for2
mov rdx, [rsp+.n]
cmp rcx, rdx ; i <= n
jg .end_for2
; double element = p[i];
mov rax, [rsp+.p]
mov r8, [rsp+.pp]
movsd xmm0, [rax+8*rcx]
; int i2 = i;
mov rbx, rcx
; i2--;
dec rbx
; pp[i2] = element*i;
cvtsi2sd xmm1, rcx
mulsd xmm1, xmm0
movsd [r8+8*rbx], xmm1
mov rcx, [rsp+.i]
inc rcx
mov [rsp+.i], rcx
jmp .for2
.end_for2
; compute P from p
;for(i = 0; i<=n; i++)
;{
xor ecx, ecx ; i = 0
mov [rsp+.i], rcx
.for3
mov rdx, [rsp+.n]
cmp rcx, rdx ; i <= n ?
jg .end_for3
; double element = p[i];
mov rax, [rsp+.p]
mov r8, [rsp+.P]
movsd xmm0, [rax+8*rcx]
; P[i] = element/i;
cvtsi2sd [r8+8*rcx], xmm1
mov rcx, [rsp+.i]
inc rcx
mov [rsp+.i], rcx
jmp .for3
;}
.end_for3
; compute and print p(a) and p(b)
for(i<
; compute and print pp(a) and pp(b)
; compute and print p(b)-p(a)
leave
ret
create:
push rbp
mov rbp, rsp
shl rdi, 3
call malloc
leave
ret