Skip to content

Instantly share code, notes, and snippets.

@SohanChy
Last active April 18, 2016 19:42
Show Gist options
  • Save SohanChy/e15676f1a9755eb49be0ecb4116f2335 to your computer and use it in GitHub Desktop.
Save SohanChy/e15676f1a9755eb49be0ecb4116f2335 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
mov ah,2
countstuff:
rol bh,1
jc one
jnc zero
one:
inc ones
mov dl,'1'
int 21h
jmp ex
Zero:
inc zeros
mov dl,'0'
int 21h
ex:
loop countstuff
mov dl,0ah
int 21h
mov dl,0dh
int 21h
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