开发者

Sparc Assembly Call corrupts data

开发者 https://www.devze.com 2023-01-01 11:57 出处:网络
I am at the moment working with some assembler code for the Sparc processor family, and i am having some trouble with a piece of code. I think the code and output explains more, but in short, this is

I am at the moment working with some assembler code for the Sparc processor family, and i am having some trouble with a piece of code. I think the code and output explains more, but in short, this is my problem:

When i do a call to the function println() the variables that i have written to the %fp - 8 memory location are destroyed. Here is the assembler code that i am trying to run:

    !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

Here is the assembler code for the println code

    .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

This is the out put i get when i run this piece of assembler code

1

67584

1

As u can see, the data that is located at %fp - 8 has been destroyed. Please, all feedback is aprecated.


Since calling println is certainly not a NOP, this is a strange comment:

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

I'm no expert in Sparc assembly but looking at this I wondered if call/jmp have what are called "delay slots", so the instruction following the branch is executed before the branch takes effect. And they do:

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

So did you comment out NOP operations that were actually purposeful, because they were trying to fill the delay slot?

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


I noticed that I had forgoten to incease the size of the save from 96 to 104, and then it worked like a charm:

save %sp, -104, %sp

Instead of 96 in the go function..

0

精彩评论

暂无评论...
验证码 换一张
取 消