汇编语言怎么实现以e为底的对数函数?
汇编语言怎么实现以e为底的对数函数? 你先用C语言实现一个,然后编译一下就成汇编语言了我去补了一下自己的数学功底,用C语言写了一个
#include <stdio.h>
#include <math.h>
double ya_log(double x) {
double result = 0;
while(pow(M_E, result) < x) ++result;
double half = 0.5;
for(size_t i = 0; i < 100; ++i) {
double p = pow(M_E, result);
if(p < x) {
result += half;
} else if(p > x) {
result -= half;
} else return result;
half /= 2;
}
return result;
}
int main(void) {
for(size_t i = 0; i < 50; ++i) {
printf("%.20lf\n", log(i));
printf("%.20lf\n", ya_log(i));
printf("\n");
}
return 0;
}
汇编语言版本
$ gcc -S main.c
$ cat main.s
.file "main.c"
.text
.globlya_log
.def ya_log; .scl 2; .type 32; .endef
.seh_proc ya_log
ya_log:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $80, %rsp
.seh_stackalloc 80
.seh_endprologue
movsd %xmm0, 16(%rbp)
pxor %xmm0, %xmm0
movsd %xmm0, -8(%rbp)
jmp .L2
.L3:
movsd -8(%rbp), %xmm1
movsd .LC1(%rip), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -8(%rbp)
.L2:
movsd -8(%rbp), %xmm0
movq .LC2(%rip), %rax
movapd%xmm0, %xmm1
movq %rax, -40(%rbp)
movsd -40(%rbp), %xmm0
call pow
movapd%xmm0, %xmm1
movsd 16(%rbp), %xmm0
ucomisd %xmm1, %xmm0
ja .L3
movsd .LC3(%rip), %xmm0
movsd %xmm0, -16(%rbp)
movq $0, -24(%rbp)
jmp .L4
.L11:
movsd -8(%rbp), %xmm0
movq .LC2(%rip), %rax
movapd%xmm0, %xmm1
movq %rax, -40(%rbp)
movsd -40(%rbp), %xmm0
call pow
movq %xmm0, %rax
movq %rax, -32(%rbp)
movsd 16(%rbp), %xmm0
ucomisd -32(%rbp), %xmm0
jbe .L14
movsd -8(%rbp), %xmm0
addsd -16(%rbp), %xmm0
movsd %xmm0, -8(%rbp)
jmp .L7
.L14:
movsd -32(%rbp), %xmm0
ucomisd 16(%rbp), %xmm0
jbe .L15
movsd -8(%rbp), %xmm0
subsd -16(%rbp), %xmm0
movsd %xmm0, -8(%rbp)
jmp .L7
.L15:
movsd -8(%rbp), %xmm0
jmp .L10
.L7:
movsd -16(%rbp), %xmm0
movsd .LC4(%rip), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -16(%rbp)
addq $1, -24(%rbp)
.L4:
cmpq $99, -24(%rbp)
jbe .L11
movsd -8(%rbp), %xmm0
.L10:
addq $80, %rsp
popq %rbp
ret
.seh_endproc
.def __main; .scl 2; .type 32; .endef
.section .rdata,"dr"
.LC5:
.ascii "%.20lf\12\0"
.text
.globlmain
.def main; .scl 2; .type 32; .endef
.seh_proc main
main:
pushq %rbp
.seh_pushreg %rbp
movq %rsp, %rbp
.seh_setframe %rbp, 0
subq $64, %rsp
.seh_stackalloc 64
.seh_endprologue
call __main
movq $0, -8(%rbp)
jmp .L17
.L22:
movq -8(%rbp), %rax
testq %rax, %rax
js .L18
cvtsi2sdq %rax, %xmm0
jmp .L19
.L18:
movq %rax, %rdx
shrq %rdx
andl $1, %eax
orq %rax, %rdx
cvtsi2sdq %rdx, %xmm0
addsd %xmm0, %xmm0
.L19:
call log
movq %xmm0, %rax
movq %rax, %rdx
movq %rdx, -24(%rbp)
movsd -24(%rbp), %xmm0
movapd%xmm0, %xmm1
movq %rax, %rdx
leaq .LC5(%rip), %rcx
call printf
movq -8(%rbp), %rax
testq %rax, %rax
js .L20
cvtsi2sdq %rax, %xmm0
jmp .L21
.L20:
movq %rax, %rdx
shrq %rdx
andl $1, %eax
orq %rax, %rdx
cvtsi2sdq %rdx, %xmm0
addsd %xmm0, %xmm0
.L21:
call ya_log
movq %xmm0, %rax
movq %rax, %rdx
movq %rdx, -24(%rbp)
movsd -24(%rbp), %xmm0
movapd%xmm0, %xmm1
movq %rax, %rdx
leaq .LC5(%rip), %rcx
call printf
movl $10, %ecx
call putchar
addq $1, -8(%rbp)
.L17:
cmpq $49, -8(%rbp)
jbe .L22
movl $0, %eax
addq $64, %rsp
popq %rbp
ret
.seh_endproc
.section .rdata,"dr"
.align 8
.LC1:
.long 0
.long 1072693248
.align 8
.LC2:
.long 2333366121
.long 1074118410
.align 8
.LC3:
.long 0
.long 1071644672
.align 8
.LC4:
.long 0
.long 1073741824
.ident"GCC: (GNU) 7.4.0"
.def pow; .scl 2; .type 32; .endef
.def log; .scl 2; .type 32; .endef
.def printf; .scl 2; .type 32; .endef
.def putchar; .scl 2; .type 32; .endef
$
页:
[1]