汇编:获取LSB和MSB的位置

时间:2015-10-29 01:27:44

标签: assembly nasm

我有一个任务,使用NASM程序集找到数字中最不重要和最重要位的位置。但是,我遇到了两个问题,即使得到我要编译的东西:

  1. 将数字除以2不起作用(div 2输出invalid operand错误)
  2. 我的注册用完了!
  3. 这就是我现在所拥有的:

    %include "along32.inc"
    
    section .data
    msg1    db      'Enter a hexadecimal number: ', 0
    msg2    db      'LSB set: ' , 0
    msg3    db      'MSB set: ', 0
    msg4    db      'Total bits set: ', 0
    
    
    section .text
    
    global main
    
    main:
        mov     edx,    msg1
        call    WriteString
        call    ReadHex     ; eax contains hex number
        mov     edx,    eax ; edx contains hex number
    
        mov     si,    0   ; si will contain lsb posiiton
        mov     r9w,    0   ; r9w will contain msb position
        mov     bh,     0   ; bh will contain number of bits set
        mov     bl,     1   ; bl will store if the msb is set
        mov     cx,     32  ; loop through every power of two
        mov     eax,    2147483647 ; eax contains 2^31 - 1
    
    bitchecker:
        mov     ebp,    edx ; ebp contains number
        and     ebp,    eax ; ebp contains eax & ebp
        jz      loopend
        inc     bh
        mov     si,    cx ; put value of cx into si
        cmp     bl,     0  ; if msb is already set, jump to loop end
        je      loopend
    
        mov     r9w,    cx ; put value of cx into r9w
        dec     bl
    
    loopend:
        xor     edx,    edx
        div     2
        loop    bitchecker
    
    continue:
        mov     edx,    msg2
        call    WriteString
    
        mov     eax,    si ; write lsb
        call    WriteInt
    
        call    Crlf
    
        mov     edx,    msg3
        call    WriteString
    
        mov     eax,    r9w ; write msb
        call    WriteInt
    
        call    Crlf
    
        mov     edx,    msg4
        call    WriteString 
    
        xor     eax,    eax
        mov     ax,    bh ; write total bits set
        call    WriteInt
    
        jmp end
    
    end:
    
        call    Crlf
    
        mov     eax,    1
        int     0x80
    

    我甚至不知道逻辑是否首先起作用,但现在这不是我的问题。这只是很难与所有这些寄存器一起工作,这让人感到困惑。我该如何避免这些问题?

    编辑:

    最终解决方案:

    %include "along32.inc"
    
    section .data
    msg1    db      'Enter a hexadecimal number: ', 0
    msg2    db      'LSB set: ' , 0
    msg3    db      'MSB set: ', 0
    msg4    db      'Total bits set: ', 0
    
    section .text
    
    global main
    
    main:
        mov     edx,    msg1
        call    WriteString
        call    ReadHex     ; eax contains hex number
    
        mov     ecx,    0  ; ecx contains loop variable
        mov     ebx,    32  ; ebx contains lsb
        mov     ebp,    0   ; ebp contains msb
        mov     esi,    0   ; esi contains how many bits set
        jmp     algo
    
    algo:
        shr     eax,    1
        jnc     not_set
        mov     ebp,    ecx
        inc     esi
        cmp     ecx,    ebx
        jg      not_set
        mov     ebx,    ecx
    
        ; decrement and check loop condition
    not_set:
        inc     ecx
        cmp     ecx,    32
        je      end
    
        jmp     algo
    
    
    end:
        mov     edx,    msg2
        call    WriteString
    
        mov     eax,    ebx
        call    WriteInt
    
        call    Crlf
    
        mov     edx,    msg3
        call    WriteString
    
        mov     eax,    ebp
        call    WriteInt
    
        call Crlf
    
        mov     edx,    msg4
        call    WriteString
    
        mov     eax,    esi
        call    WriteInt
    
        call    Crlf
    
        mov     eax,    1
        int     0x80
    

1 个答案:

答案 0 :(得分:1)

您可以使用div 2

,而不是无效的shr eax, 1

一个简单的算法是:

lsb = 32;
for(i = 0; i < 32; i++)
{
    if (x & 1)
    {
        if (i < lsb) lsb = i;
        msb = i;
    }
    x >>= 1;
}

这只需要4个寄存器(xilsbmsb)。