Advertisement
Il_Voza

4.3 Count the 1 into a bin number, given its decimal number

May 12th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Write a program that counts the number of bit at 1 in the binary representation of a variable of type byte
  2.  
  3. DIM EQU 8
  4. .MODEL small
  5. .STACK
  6. .DATA
  7. var DB 1
  8. .CODE
  9. .STARTUP
  10. MOV AL,var
  11. MOV CX,DIM
  12. XOR BX,BX
  13. ciclo:  DEC CX
  14.         SHR AL,1  ;Coincides to the division by 2
  15.         JC sum
  16.         ciclo1:
  17.         CMP AL,0
  18.         JNZ ciclo
  19.         JZ ex ;So when I finish to cycle, It doesn't pass through sum cycle
  20.        
  21. sum: INC BX
  22.      JMP ciclo1        
  23. ex:
  24. .exit
  25. END
  26.  
  27. ;My strategy was: I divide my number
  28. ;always by 2, and whenever that I have remainder, that is
  29. ;when the Carry Flag is up to 1, go in sum cycle
  30. ;and increases BX that acts as a counter, then return
  31. ;at ciclo the value 1 (it is optional) so
  32. ;I regain the code where I was remained
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement