程序集x86:如何计算32位长整数中的设置位数

时间:2016-09-20 21:31:52

标签: assembly x86 masm masm32 att

我是一个新手,我正在编写代码来计算32位整数中的设置位数。

我写了这个,但是我收到了很多错误:

.586
.model flat,stdcall
.stack 4096

; Windows libraries

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

.DATA

data:   .LONG 0x0F0F0101
count:  .BYTE 0x00

.TEXT

_main:  NOP
        MOVB $0x00, %CL
        MOVL data, %EAX
comp:   CMPL $0x00, %EAX
        JE end
        SHRL %EAX
        ADCB $0x0, %CL
        JMP comp
end:    MOVB %CL, count
        RET

我有以下错误:

C:\Users\RaiN\Desktop\Test\test1.asm(20) : error A2034: must be in segment block
C:\Users\RaiN\Desktop\Test\test1.asm(25) : error A2108: use of register assumed to ERROR
C:\Users\RaiN\Desktop\Test\test1.asm(25) : error A2008: syntax error : .
C:\Users\RaiN\Desktop\Test\test1.asm(26) : error A2108: use of register assumed to ERROR
C:\Users\RaiN\Desktop\Test\test1.asm(26) : error A2008: syntax error : .
C:\Users\RaiN\Desktop\Test\test1.asm(28) : error A2008: syntax error : .
C:\Users\RaiN\Desktop\Test\test1.asm(29) : error A2108: use of register assumed to ERROR
C:\Users\RaiN\Desktop\Test\test1.asm(30) : error A2008: syntax error : $0x00
C:\Users\RaiN\Desktop\Test\test1.asm(31) : error A2008: syntax error : dato
C:\Users\RaiN\Desktop\Test\test1.asm(32) : error A2108: use of register assumed to ERROR
C:\Users\RaiN\Desktop\Test\test1.asm(32) : error A2008: syntax error : CMPL
C:\Users\RaiN\Desktop\Test\test1.asm(34) : error A2008: syntax error : !%
C:\Users\RaiN\Desktop\Test\test1.asm(35) : error A2008: syntax error : $0x0
C:\Users\RaiN\Desktop\Test\test1.asm(37) : error A2108: use of register assumed to ERROR
C:\Users\RaiN\Desktop\Test\test1.asm(37) : error A2008: syntax error : MOVB
C:\Users\RaiN\Desktop\Test\test1.asm(38) : error A2088: END directive required at end of file
C:\Users\RaiN\Desktop\Test\test1.asm(33) : error A2107: cannot have implicit far jump or call to near label
C:\Users\RaiN\Desktop\Test\test1.asm(36) : error A2107: cannot have implicit far jump or call to near label

我该如何解决这个问题?

非常感谢!!!

2 个答案:

答案 0 :(得分:0)

新代码是:

.text
.global main
main:
    movq %rsp, %rbp #for correct debugging
    # write your code here
.DATA
stringa:    .ASCIZ "Questa è la stringa di caratteri ASCII che usiamo come esempio"

lettera:     .BYTE 'e'
conteggio:   .BYTE 0x00

.TEXT
_main:        NOP
              MOV $0x00, %CL
              LEA stringa, %ESI
              MOV lettera, %AL
comp:         CMPB $0x00, (%ESI)
              JE fine
              CMP (%ESI), %AL
              JNE poi
              INC %CL
poi:          INC %ESI
              JMP comp
fine:         MOV %Cl, conteggio

              INT $0x27              
              RET

答案 1 :(得分:0)

您要计算的内容称为Hamming Weight 您可以使用Logical shifts和Bit Masking

来实现此目的

这是一个简单的程序。我假设您正在使用x86程序集。

.386
.model flat,stdcall
option casemap:none

include kernel32.inc
includelib kernel32.lib

.code
start:
    mov eax,1h             ;your number in eax 100 is sample here
    mov ecx,0h             ;is the counter register
    xor edx,edx            ;is done to make edx 0, you can also do mov edx,0
notDoneWithNumber:
    cmp eax,0
    je done
    mov edx,eax            ;edx is here a compare register, not nice, but it works
    shr eax,1              ;we push all the bits one place to the right, bits at position 1 will be "pushed out of the byteword"
    and edx,1h             ;make sure we only get, wether the last bit is set or not(thats called bitmaking) 
    cmp edx,0h
    jz notDoneWithNumber   ;if the found value is a zero we can skip the inc of the count register
    inc ecx
    jmp notDoneWithNumber
done:                      ;register ecx will now hold your hamming weight
end start