2010-05-27 40 views
2

我目前正在使用Sparc處理器系列的一些彙編程序代碼,並且我遇到了一些代碼問題。我認爲代碼和輸出解釋更多,但總之,這是我的問題:Sparc程序集調用破壞數據

當我打電話給函數println()我已寫入%fp - 8內存位置的變量被破壞。下面是我試圖運行彙編代碼:

!PROCEDURE main 
    .section ".text" 
    .global main 
    .align 4 
    main: 
    save %sp, -96, %sp 

L1: 
    set 96, %l0 
    mov %l0, %o0 
    call initObject ; nop 
    mov %o0, %l0 
    mov %l0, %o0 
    call Test$go ; nop 
    mov %o0, %l0 
    mov %l0, %o0 
    call println ; nop 
L0: 
    ret 
    restore 
!END main 

!PROCEDURE Test$go 
    .section ".text" 
    .global Test$go 
    .align 4 
Test$go: 
    save %sp, -96, %sp 

L3: 
    mov %i0, %l0 
    set 0, %l0 
    set -8, %l1 
    add %fp,%l1, %l1 
    st %l0, [%l1] 
    set 1, %l0 
    mov %l0, %o0 
    call println ; nop 
    set -8, %l0 
    add %fp,%l0, %l0 
    ld [%l0], %l0 
    mov %l0, %o0 
    call println ; nop 
    set 1, %l0 
    mov %l0, %i0 
L2: 
    ret 
    restore 

!END Test$go 

這裏是println的代碼

.global println 
    .type println,#function 
println: 
    save %sp,-96,%sp 

    ! block 1 
    .L193: 

    ! File runtime.c: 
    ! 42 } 
    ! 43 
    ! 45 /** 
    ! 46 Prints an integer to the standard output stream. 
    ! 47 
    ! 48 @param i The integer to be printed. 
    ! 49 */ 
    ! 50 void println(int i) { 
    ! 51  printf("%d\n", i); 

    sethi %hi(.L195),%o0 
    or %o0,%lo(.L195),%o0 
    call printf 
    mov %i0,%o1 
    jmp %i7+8 
    restore 

這彙編代碼是出把我得到當我運行這段彙編代碼

1 

67584 

1 

正如你所看到的,位於%fp - 8的數據已被銷燬。請所有反饋意見都是贊成的。

回答

2

由於調用println肯定不是一個NOP,這是一個奇怪的評論:

call println ; nop 
set -8, %l0 
add %fp, %l0, %l0 

我對SPARC彙編不是專家,但在看這個,我想知道,如果call/jmp有所謂的「延遲插槽「,因此分支後面的指令在分支生效之前執行。和他們做:

http://moss.csc.ncsu.edu/~mueller/codeopt/codeopt00/notes/delaybra.html

那你有沒有註釋掉實際上是有目的的NOP操作,因爲他們試圖填補延遲槽?

call println 
nop 
set -8, %l0 
add %fp, %l0, %l0 
+0

Thx的提示,我試了一下,但我仍然得到同樣的錯誤..我已經計算出%fp - 4是好的,但之後的一切都被破壞.. – Sigge 2010-05-27 17:14:37

1

我注意到,我已經forgoten到incease保存從96到104的大小,然後它的工作就像一個魅力:

save %sp, -104, %sp 

,而不是在去功能96 ..