first effort on recursion - not anywhere done

This commit is contained in:
Perry Kivolowitz 2022-06-09 20:20:06 -05:00
parent 9c8ca24a6c
commit 51611ddf3d
4 changed files with 178 additions and 0 deletions

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.a.out</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

22
section_1/recursion/fib.c Normal file
View file

@ -0,0 +1,22 @@
#include <stdio.h>
extern unsigned int Fib(unsigned int nthNumber);
unsigned int FibonacciInC(unsigned int nthNumber) {
if (nthNumber <= 1) {
return nthNumber;
}
return FibonacciInC(nthNumber - 1) + FibonacciInC(nthNumber - 2);
}
int main() {
printf("In C:\n");
for (unsigned int n = 0; n < 10; n++) {
printf("Fibonacci number %2d is: %d\n", n, FibonacciInC(n));
}
printf("In assembly language:\n");
for (unsigned int n = 0; n < 10; n++) {
printf("Fibonacci number %2d is: %d\n", n, Fib(n));
}
return 0;
}

115
section_1/recursion/fib.s Normal file
View file

@ -0,0 +1,115 @@
.arch armv8-a
.file "fib.c"
.text
.align 2
.global FibonacciInC
.type FibonacciInC, %function
FibonacciInC:
.LFB0:
.cfi_startproc
stp x29, x30, [sp, -48]!
.cfi_def_cfa_offset 48
.cfi_offset 29, -48
.cfi_offset 30, -40
mov x29, sp
str x19, [sp, 16]
.cfi_offset 19, -32
str w0, [sp, 44]
ldr w0, [sp, 44]
cmp w0, 1
bhi .L2
ldr w0, [sp, 44]
b .L3
.L2:
ldr w0, [sp, 44]
sub w0, w0, #1
bl FibonacciInC
mov w19, w0
ldr w0, [sp, 44]
sub w0, w0, #2
bl FibonacciInC
add w0, w19, w0
.L3:
ldr x19, [sp, 16]
ldp x29, x30, [sp], 48
.cfi_restore 30
.cfi_restore 29
.cfi_restore 19
.cfi_def_cfa_offset 0
ret
.cfi_endproc
.LFE0:
.size FibonacciInC, .-FibonacciInC
.section .rodata
.align 3
.LC0:
.string "In C:"
.align 3
.LC1:
.string "Fibonacci number %2d is: %d\n"
.align 3
.LC2:
.string "In assembly language:"
.text
.align 2
.global main
.type main, %function
main:
.LFB1:
.cfi_startproc
stp x29, x30, [sp, -32]!
.cfi_def_cfa_offset 32
.cfi_offset 29, -32
.cfi_offset 30, -24
mov x29, sp
adrp x0, .LC0
add x0, x0, :lo12:.LC0
bl puts
str wzr, [sp, 24]
b .L5
.L6:
ldr w0, [sp, 24]
bl FibonacciInC
mov w2, w0
ldr w1, [sp, 24]
adrp x0, .LC1
add x0, x0, :lo12:.LC1
bl printf
ldr w0, [sp, 24]
add w0, w0, 1
str w0, [sp, 24]
.L5:
ldr w0, [sp, 24]
cmp w0, 9
bls .L6
adrp x0, .LC2
add x0, x0, :lo12:.LC2
bl puts
str wzr, [sp, 28]
b .L7
.L8:
ldr w0, [sp, 28]
bl Fib
mov w2, w0
ldr w1, [sp, 28]
adrp x0, .LC1
add x0, x0, :lo12:.LC1
bl printf
ldr w0, [sp, 28]
add w0, w0, 1
str w0, [sp, 28]
.L7:
ldr w0, [sp, 28]
cmp w0, 9
bls .L8
mov w0, 0
ldp x29, x30, [sp], 32
.cfi_restore 30
.cfi_restore 29
.cfi_def_cfa_offset 0
ret
.cfi_endproc
.LFE1:
.size main, .-main
.ident "GCC: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0"
.section .note.GNU-stack,"",@progbits

View file

@ -0,0 +1,21 @@
.global Fib
.text
.align 4
Fib: str x30, [sp, -16]!
cmp w0, 1
// If w0 is 0 or 1, take the branch to return w0.
bls 99f
// If we get here, we need to do the recursion.
// Save the parameter on the stack so we can
// get it back between recursions.
str w0, [sp, -16]!
sub w0, w0, 1
bl Fib
ldr w0, [sp], 16 // temporary
99: ldr x30, [sp], 16
ret