addition - add up all 1 bits in x86 assembly -


i don't have clue of i'm doing... goal add 1's , if , if odd c/c++ section gives variables passed asm

#include <stdint.h> #include <stdio.h> #include <iostream> using namespace std;  extern "c" {     bool    isbitcounteven(int32_t); }  static int32_t testdata[] = {     0x0,     0x1,     0x2,     0x4,     0x8,     0x40000000,     0x80000000,     0x00000002,     0xe,     0xff770001,     0xffeeeeff,     0xdeadbeef,     0xbaddf00d,     0xd00fefac,     0xfaceecaf,     0xffffffff,     0xaaaa5555 }; #define num_test_cases  (sizeof (testdata) / sizeof (*testdata))   int main() {     printf(" ice#10 \n\n");      (int = 0; < num_test_cases; i++) {         printf("isbitcounteven(0x%8x) yields: %s\n", testdata[i], isbitcounteven(testdata[i]) ? "true" : "false");     }     system("pause");     return 0; } 

and asm. doesn't output anything. prologue , epilogue things ive copped , pasted other work worked disregard those, know may garbage

here asm

        .386p           ; use 80386 instruction set         .model flat,c     ; use flat memory model          printf proto c, :vararg          .data           ; declare initialzed data here          .stack          ; use default 1k stack space          .code           ; contains our code       ;-------------------part 1------------------------------------------------------------     ;if (the number of 1 bits in binary representation of val even)     ;               return true;     ;           else             ;               return false;     ;       }     ;     ;--------------------------------------------------------------------------------------     isbitcounteven proc public         push    ebp             ; save caller base pointer         mov     ebp, esp        ; set our base pointer         sub     esp, (1 * 4)    ; allocate uint32_t local vars         push    edi         push    esi         ; end prologue          mov     al, [ebp + 8]     ;now walk down variable , count number of 1      shift:         shl al, 1         jnc skip         inc ebx     skip:         loop shift      finale:             ;see if adds ebx     test al, al     jp false     test ebx, 1      jz true    true:     mov     eax,1     pop     esi                 ; start epilogue     pop     edi     mov     esp, ebp            ; deallocate locals     pop     ebp                 ; restore caller base pointer     ret false:     mov     eax, 0     pop     esi                 ; start epilogue     pop     edi     mov     esp, ebp            ; deallocate locals     pop     ebp                 ; restore caller base pointer     ret isbitcounteven endp ; end procedure end isbitcounteven 

what should lost , horrible @ asm...

it can simplified quite bit, getting rid of stack frame business (it's not necessary, there no dynamic stack allocation) , using setcc instead of branching.

you can count bits of int32_t popcnt, test whether count even,

isbitcounteven proc public     mov eax, dword ptr [esp + 4]     popcnt eax, eax     test al, 1     setz al    ; if lowest bit of count zero,     ret isbitcounteven endp 

an other simple way use parity flag, checks parity of low byte takes bit of reduction first,

isbitcounteven proc public     mov eax, dword ptr [esp + 4]     mov edx, eax     shr eax, 16     xor eax, edx     xor al, ah ; 4 bytes xored together, parity flag reflects total parity     setpe al   ; set al 1 if parity even, 0 if odd     ret isbitcounteven endp 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -