Skip to content

Instantly share code, notes, and snippets.

@ehzawad
Forked from SohanChy/binary-tricks.asm
Created April 18, 2016 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehzawad/f444acbaa685887a79f23c4646d2651c to your computer and use it in GitHub Desktop.
Save ehzawad/f444acbaa685887a79f23c4646d2651c to your computer and use it in GitHub Desktop.
binary tricks on numbers
.model small
.stack 100h
.data
str1 db 'Ones :$'
str2 db ' zero :$'
ones db '0'
zeros db '0'
.code
main proc
mov ax,@data
mov ds,ax
mov bl,00001000b
mov dl,00000100b
mov cx,8
reversetodh1:
rol bl,1
rcr dh,1
loop reversetodh1
mov bl,dh
mov dh,0 ;set zero to be safe
mov cx,8
reversetodh2:
rol dl,1
rcr dh,1
loop reversetodh2
mov dl,dh
;now move to desired registers
mov ch,bl
mov bh,dl
add bh,ch
mov cx,8
countstuff:
rol bh,1
jc one
jnc zero
one:
inc ones
jmp ex
Zero:
inc zeros
ex:
loop countstuff
mov ah,9
lea dx,str1
int 21h
mov ah,2
mov dl,ones
int 21h
mov ah,9
lea dx,str2
int 21h
mov ah,2
mov dl,zeros
int 21h
main endp
end main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment