Сalculate the weight of each byte of number x

56 Views Asked by At

While learning C++, i decided to get some knowledge of assembly. So i have to count weight of each byte of some number.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;


int main()
{
    int x=0;
    int count = 0;
    cout << "Enter number\n";
    cin >> x;
    int z = 8;
    _asm
    {
        mov ebx, z;
        mov eax, x;
        xor ecx, ecx;
    l:
        cmp ebx, 0;
        jne k;
        jmp m;
    k:
        test eax, 11111111;
        shr eax, 1;
        adc ecx, 0;
        dec ebx;
        jmp l;
    m:
        mov count, ecx;
    }
    
    cout << "Counted " << count << " bits";
    return 0;

}

Now it works corret only for 1 byte numbers, how to empower this program for 2 bytes and more?

.......................................

0

There are 0 best solutions below