使用汇编计算二进制数中最长的1s链

时间:2015-10-29 18:39:39

标签: c assembly x86

我必须用数字的二进制表示来计算最长的1s链,就像标题所说的那样。测试我的数字

<p>Admin site | client activate and deactivate page:</p>
{!! Form::open(array('url' => 'admin/action')) !!}

<table width="800px">
    <tr>
        <td width="10%">Select</td>
        <td width="20%">Client id</td>
        <td width="20%">Client status</td>
        <td width="50%">Client name</td>
    </tr>

    @foreach ($client as $clientOutput)
        <tr>
            <td>{!! Form::checkbox('status[]', $clientOutput->id, false, ['id' => $clientOutput->id]) !!}</td>
            <td>{{$clientOutput->id}}</td>
            <td>{{$clientOutput->status}}</td>
            <td>{{$clientOutput->client}}</td>
        </tr>
    @endforeach

</table>

{!! Form::submit('Activate', ['name' => 'activate']) !!}
{!! Form::submit('Deactivate', ['name' => 'Deactivate']) !!}
{!! Form::close() !!}

哪个二进制represantation是

0x38F9

这意味着我的程序应该将0011 1000 1111 1001 写为输出,表示此数字中最长的1s链为5。

我用C inline编写了这个程序集,但它输出y=5,而不是y=15,这显然是错误的:

y=5

我无法弄清楚出了什么问题。正如我所说,它输出 #include <stdio.h> int main(void){ short int x, y; x = 0x38F9; //x=(0011,1000,1111,1001) y = 0; asm volatile ( ".intel_syntax noprefix;" "mov ax,%1;" "mov cx,16;" //counter of a loop "xor dx,dx;" //stores the highest number of 1s in one chain "xor bx,bx;" // counter of 1s "loop: " "shl ax;" "jz reset;" "inc bx;" //increment 1s counter "cmp bx,dx;" "jg add;" "jmp jump;" "reset: " "xor bx,bx;" "jmp jump;" "add: " "mov dx, bx;" "jump: " "dec cx;" //decrement loop counter "jnz loop;" "mov %0,dx;" ".att_syntax prefix;" : "=r" (y) : "r" (x) : "ax","bx","cx","dx" ); printf("y = %hd",y); } ,这绝对不是真的。你能帮我解决这个问题吗?

0 个答案:

没有答案