0

したがって、整数のビットをカウントする必要があります。これは私のコードですが、なぜ機能しないのかわかりません。c main から 16 進値を送信し、どういうわけかそれをシフトしてマスクする必要があります。私はちょっと迷っています。シフトとマスクの方法がわからないので、間違った答えを得ていると思います。繰り返しますが、ゼロではなく 1 であるビットを 32 までカウントする必要がありますが、間違った答えが得られます。たとえば、6 110 は 2 ビットになります。これは宿題なので、組み込み関数などを使用することはできません。

   .global hi
hi:

save %sp,-1024,%sp

clr %i0
clr %i1
clr %i2

loop:  

       cmp %i2,32
       be,a away
    mov %l7,%i0
    add 1,%i2,%i2

    and %i0,1,%l1


    cmp %l1,1
    be count
       nop
    !srl %i0,%l2,%10
    !mov %l2,%i0


    !and %i0,1,%i0
    srl %i0,1,%i0

    ba loop
    nop
    !mov %l2,%i0

count:
    add 1,%l7,%l7

away:
    ret
    restore

なぜこれがまだ機能していないのですか?私はそのC実装に従いましたが、まだビット数を返していません:/。戻り値は %i0 で、カウンターをインクリメントした後にループに戻る方法がわかりません。

それで、これは何をしているのですか?ba loop と表示されたら、ループに戻るべきではありませんか?

質問が多いかどうかはわかりませんが、この問題を解決する方法について何か考えはありますか? :pよくわからないので、マニュアルを見ていますが、私に役立つものは何もありません:/。

4

1 に答える 1

0

シフトと追加にも欠点があります...

ループ構造は次のとおりです。

int count(int data) {
loop_entry:
        if (loop_counter == 32) goto out;      // this is basically ok
        loop_counter++;            

        if (some_math_expression) goto count;  // the math expression is ok

        temporary_variable>>=1;   // this is not ok -- you should be shifting l2 

        if (something_else) goto loop_entry;   // what actually is the 'something_else'?

count:
        data++;    // this is not ok: you are incrementing i0 instead of l2
                   // this is not ok: fall through instead of jumping back to loop

out:
        return result;

}

Cでの実装に最も近い適切な構造は次のようになります。

int count (int data)
{
     int result=0;
     int temp=0;
     int loop_counter = 0;
     while (loop_counter++ != 32) {  // conditionally jump out of the loop
         temp = data & 1;
         if (temp == 1) {   // Q: how to perform this in assembler? 
            result++;       // A: negate the condition and jump over the result++
         }                  // statement
         data>>=1;
     }                      // (unconditionally? Jump to beginning of loop)
     return result;
}

http://www.cs.northwestern.edu/~agupta/_projects/sparc_simulator/SPARC%20INSTRUCTION%20SET-1.htm

ここでは、「ba」=常に分岐している(私の間違い)-(temp == 1)の比較が「be」または等しい場合は分岐によって行われる場合、その反対
は等しくない場合は分岐、またはbne 。したがって、条件付き統計は次のようになります。

cmp %temp, %one          # I'm using temp & one to "alias" the registers
bne skip                 # that contain these variables or constants
nop    ; // delay slot
add %count, 1, %count
skip:

// there's however a trick to do that without jumping (see the link about 'carry'-bit)
add %data,%data,%data    # "multiply" data by two, which may produce carry
addx %count,%zero,%count # count = count + 0 + carry_produced_by_previous_instruction

キャリービットとオーバーフロービットを使用する理由

于 2012-10-08T05:38:46.843 に答える