Chinaunix首页 | 论坛 | 博客
  • 博客访问: 692655
  • 博文数量: 33
  • 博客积分: 10032
  • 博客等级: 上将
  • 技术积分: 1383
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-22 18:53
文章分类

全部博文(33)

文章存档

2011年(4)

2010年(10)

2009年(14)

2008年(5)

我的朋友

分类:

2011-03-27 22:21:23

最近搜到一份文档,SPARC Assembly Language Reference Manual,上面有个例子,在sun4v上反编译了一下,和例子上的结果不太一样,估计例子上是SPARC V8的结果,所以重新注释了一下。注释完后感觉RISC比CISC要好理解一些,x86寄存器的命名太复杂,倒不如SPARC简洁。

/* a simple program computing the first n Fibonacci numbers */

extern unsigned * fibonacci();

#define MAX_FIB_REPRESENTABLE 49

/* compute the first n Fibonacci numbers */
unsigned * fibonacci(n)
int n;
{
    static unsigned fib_array[MAX_FIB_REPRESENTABLE] = {0,1};

    /* [%fp - 0x14] */
    unsigned prev_number = 0;

    /* [%fp - 0x18] */
    unsigned curr_number = 1;

    /* [%fp - 0x1c] */
    int i;

    if (n >= MAX_FIB_REPRESENTABLE) {
        printf("Fibonacci(%d) cannot be represented in a 32 bit word\n", n);
        exit(1);
    }

    for (i = 2; i < n; i++) {
        fib_array[i] = prev_number + curr_number;
        prev_number = curr_number;
        curr_number = fib_array[i];
    }

    return(fib_array);
}

main()
{
    int n, i;
    unsigned * result;
    printf("Fibonacci(n):, please enter n:\n");
    scanf("%d", &n);
    result = fibonacci(n);

    for (i = 1; i <= n; i++)
        printf("Fibonacci (%d) is %u\n", i, *result++);
}

mdb: logging to "cc.txt"
> main::dis
main:                           save      %sp, -0x80, %sp
main+4:                         sethi     %hi(0x10c00), %g1
main+8:                         or        %g1, 0xf0, %o0
main+0xc:                       call      +0x10250     
main+0x10:                      nop
main+0x14:                      add       %fp, -0x14, %o5
main+0x18:                      sethi     %hi(0x10c00), %g1
main+0x1c:                      or        %g1, 0x110, %o0
main+0x20:                      mov       %o5, %o1
main+0x24:                      call      +0x10244     
main+0x28:                      nop
main+0x2c:                      ld        [%fp - 0x14], %o0
main+0x30:                      call      -0xf8        
main+0x34:                      nop
main+0x38:                      mov       %o0, %g1
main+0x3c:                      st        %g1, [%fp - 0x1c]
main+0x40:                      mov       0x1, %g1
main+0x44:                      st        %g1, [%fp - 0x18]
main+0x48:                      ld        [%fp - 0x18], %o5
main+0x4c:                      ld        [%fp - 0x14], %g1
main+0x50:                      cmp       %o5, %g1
main+0x54:                      bg        +0x48        
main+0x58:                      nop
main+0x5c:                      add       %fp, -0x1c, %o5
main+0x60:                      ld        [%o5], %g1
main+0x64:                      ld        [%g1], %o4
main+0x68:                      add       %g1, 0x4, %g1
main+0x6c:                      st        %g1, [%o5]
main+0x70:                      sethi     %hi(0x10c00), %g1
main+0x74:                      or        %g1, 0x118, %o0
main+0x78:                      ld        [%fp - 0x18], %o1
main+0x7c:                      mov       %o4, %o2
main+0x80:                      call      +0x101dc     
main+0x84:                      nop
main+0x88:                      ld        [%fp - 0x18], %g1
main+0x8c:                      add       %g1, 0x1, %g1
main+0x90:                      st        %g1, [%fp - 0x18]
main+0x94:                      ba        -0x4c        
main+0x98:                      nop
main+0x9c:                      mov       %g1, %i0
main+0xa0:                      ret
main+0xa4:                      restore

    %g1 (r01) temporary value

> fibonacci::dis
fibonacci:                      save      %sp, -0x80, %sp

    [%fp + 0x44] = %i0 = n

fibonacci+4:                    st        %i0, [%fp + 0x44]

    [%fp - 0x14] = prev_number = 0

fibonacci+8:                    clr       [%fp - 0x14]

    [%fp - 0x18] = curr_number = 1

fibonacci+0xc:                  mov       0x1, %g1
fibonacci+0x10:                 st        %g1, [%fp - 0x18]

    if (n >= MAX_FIB_REPRESENTABLE) {

fibonacci+0x14:                 ld        [%fp + 0x44], %g1
fibonacci+0x18:                 cmp       %g1, 0x30

    please note that: 0x30 = 48 = 49 - 1
    or "cmp %g1, 0x31, bl +0x28"

    branch on less or equal

fibonacci+0x1c:                 ble       +0x28        
fibonacci+0x20:                 nop

    printf("Fibonacci(%d) cannot be represented in a 32 bit word\n", n)
{

fibonacci+0x24:                 sethi     %hi(0x10c00), %g1
fibonacci+0x28:                 or        %g1, 0xb8, %o0

    [%fp + 0x44] = %g1 = n

fibonacci+0x2c:                 ld        [%fp + 0x44], %o1
fibonacci+0x30:                 call      +0x102f4     
fibonacci+0x34:                 nop
}

    exit(1)

fibonacci+0x38:                 mov       0x1, %o0
fibonacci+0x3c:                 call      +0x102ac     
fibonacci+0x40:                 nop

    [%fp - 0x1c] = i = 2

fibonacci+0x44:                 mov       0x2, %g1
fibonacci+0x48:                 st        %g1, [%fp - 0x1c]

    The start of the for statement:
    [%fp - 0x1c ] = %o5 = i

fibonacci+0x4c:                 ld        [%fp - 0x1c], %o5

    [%fp + 0x44 ] = %g1 = n

fibonacci+0x50:                 ld        [%fp + 0x44], %g1

    i < n

fibonacci+0x54:                 cmp       %o5, %g1

    branch on greater or equal

fibonacci+0x58:                 bge       +0x5c        
fibonacci+0x5c:                 nop

    %o3 = fib_array;

fibonacci+0x60:                 sethi     %hi(0x20c00), %g1
fibonacci+0x64:                 or        %g1, 0x340, %o3

    [%fp - 0x1c] = %g1 = i

fibonacci+0x68:                 ld        [%fp - 0x1c], %g1

    %o4 = %g1 * 2

    shift left logic

fibonacci+0x6c:                 sll       %g1, 0x2, %o4

    [%fp - 0x14 ] = %o5 = prev_number

fibonacci+0x70:                 ld        [%fp - 0x14], %o5

    [%fp - 0x18] = %g1 = curr_number

fibonacci+0x74:                 ld        [%fp - 0x18], %g1

    fib_array[i] = prev_number + curr_number
    curr_number(%g1) = prev_number(%o5) + curr_number(%g1)

fibonacci+0x78:                 add       %o5, %g1, %g1

    fib_array[i] = curr_number(%g1)

fibonacci+0x7c:                 st        %g1, [%o3 + %o4]

    prev_number = curr_number
{
    [%fp - 0x18] = curr_number

fibonacci+0x80:                 ld        [%fp - 0x18], %g1

    [%fp - 0x14] = prev_number

fibonacci+0x84:                 st        %g1, [%fp - 0x14]
}

fibonacci+0x88:                 sethi     %hi(0x20c00), %g1
fibonacci+0x8c:                 or        %g1, 0x340, %o5

    g1 = i

fibonacci+0x90:                 ld        [%fp - 0x1c], %g1

    %o4 = %g1 * 2

    shift left logic

fibonacci+0x94:                 sll       %g1, 0x2, %g1

    curr_number = fib_array[i];
{
    %o5 = fib_array;
    fib_array[i] = %g1

fibonacci+0x98:                 ld        [%o5 + %g1], %g1

    [%fp - 0x18] = %g1 = curr_number

fibonacci+0x9c:                 st        %g1, [%fp - 0x18]
}

    [%fp - 0x1c] = %g1 = i

fibonacci+0xa0:                 ld        [%fp - 0x1c], %g1

    i ++

fibonacci+0xa4:                 add       %g1, 0x1, %g1
fibonacci+0xa8:                 st        %g1, [%fp - 0x1c]

    branch always

fibonacci+0xac:                 ba        -0x60        
fibonacci+0xb0:                 nop

    return(fib_array);

fibonacci+0xb4:                 sethi     %hi(0x20c00), %g1
fibonacci+0xb8:                 or        %g1, 0x340, %g1
fibonacci+0xbc:                 mov       %g1, %i0
fibonacci+0xc0:                 ret
fibonacci+0xc4:                 restore
> $q

References:

[1]
[2] ~psm/sparcstack.html
阅读(2878) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~