Skip to content

習題3 : 請用 C 語言硬塞的方式,直接將 power(a,n) 的機器碼放進去執行 (加分題) #4

Description

@ccckmit

參考

  1. 06-c4/02-asmFib
  2. 06-c4/02b-asmFib2
  3. fib.c
++ gcc -w c4.c -o c4
++ ./c4 -s test/fib.c
1: #include <stdio.h>
2:
3: int f(int n) {
4:   if (n<=0) return 0;
    ENT  0
    LLA  2
    LI
    PSH
    IMM  0
    LE
    BZ   0
    IMM  0
    LEV
5:   if (n==1) return 1;
    LLA  2
    LI
    PSH
    IMM  1
    EQ
    BZ   0
    IMM  1
    LEV
6:   return f(n-1) + f(n-2);
    LLA  2
    LI
    PSH
    IMM  1
    SUB
    PSH
    JSR  -1283591064
    ADJ  1
    PSH
    LLA  2
    LI
    PSH
    IMM  2
    SUB
    PSH
    JSR  -1283591064
    ADJ  1
    ADD
    LEV
7: }
    LEV
8:
9: int main() {
10:   printf("f(7)=%d\n", f(7));
    ENT  0
    IMM  -1283328912
    PSH
    IMM  7
    PSH
    JSR  -1283591064
    ADJ  1
    PSH
    PRTF
    ADJ  2
11: }
    LEV

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions