uP:Newton
Z Wiki Rafał (ert16) Trójniak
Spis treści |
Temat zadania
Napisać program testujacy (w C) oraz funkcję (w ASM) wyznaczającą wartość symbolu Newtona (n k) dla n i k. Deklaracja funkcji:
long binomial_coefficient(long n, long k);
Kody źródłowe
main.c
#include <stdlib.h> long binomial_coefficient(long n, long k); int main() { long test, n,k; n=0;k=0; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); n=1;k=0; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); n=1;k=1; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); n=2;k=0; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); n=2;k=1; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); n=2;k=2; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); n=3;k=0; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); n=3;k=1; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); n=3;k=2; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); n=3;k=3; test=binomial_coefficient( n,k); printf("test(%ld, %ld) = %ld\n",n,k,test); return 0; }
asm.s
.data argDesc: .asciz"%d %d \n" .text .type binomial_coefficient , @function #long binomial_coefficient(long n, long k); .global binomial_coefficient binomial_coefficient: pushl %ebp mov %esp, %ebp movl 8(%ebp), %ecx movl 12(%ebp), %ebx # n=ebx k=ecx cmp $0,%ebx je one cmp %ebx, %ecx je one nop # n-1, k decl %ecx pushl %ebx pushl %ecx call binomial_coefficient popl %ecx popl %ebx # n-1, k-1 decl %ebx pushl %eax pushl %ebx pushl %ecx call binomial_coefficient popl %ecx popl %ebx popl %ebx # ostateczna suma add %ebx, %eax jmp finish one : xor %eax,%eax inc %eax finish : mov %ebp, %esp popl %ebp ret
Odpowiedź programu
test(0, 0) = 1 test(1, 0) = 1 test(1, 1) = 1 test(2, 0) = 1 test(2, 1) = 2 test(2, 2) = 1 test(3, 0) = 1 test(3, 1) = 3 test(3, 2) = 3 test(3, 3) = 1