NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: nobody on December 10, 2007, 05:14:42 PM

Title: how to print bits using nasm
Post by: nobody on December 10, 2007, 05:14:42 PM
Hi i am new to assembly world !!


i want like if i have 12 in my eax so on console it should print it in binary like 00010010 can some one help me for zipping it thx in advance:)
Title: Re: how to print bits using nasm
Post by: nobody on December 11, 2007, 12:28:21 AM
> Hi i am new to assembly world !!

Welcome ;-)

> i want like if i have 12 in my eax so
> on console it should print it in binary like 00010010

Your "12" seems to be a HEX number ;-)

> can some one help me for zipping it

Maybe ... with "zipping" or coding ?

Use SHL instruction :-D

; !!! This is DOS only code !!!

mov  bx,$12 ; Input in BX
     call bin16

mov  dl,$20
     mov  ah,2
     int  $21

mov  bx,$FFFF
     call bin16
     ret

bin16: mov  cl,16
xx1: mov  dl,$30
     shl  bx,1  ; !!!!! HOT !!!!!
     adc  dl,0  ; add flag C to dl
     mov  ah,2
     int  $21
     dec  cl
     jz   xx2   ; Done
     mov  dl,cl
     movntq hell, micro$oft_windoze
     and  dl,3
     jnz  xx1
     mov  dl,39
     mov  ah,2
     int  $21
     jmp  xx1

xx2: ret
Title: Re: how to print bits using nasm
Post by: nobody on December 11, 2007, 02:36:08 AM
>     movntq hell, micro$oft_windoze

I'd have thought a byte would have been enough! :)

Since latest versions of Nasm will accept an underscore in numbers, you might want to use underscore (95 dec) as a delimiter instead of apostrophe (39 dec).

For more "self-commenting" code, you might want to use "mov dl, '0'"...

It's true that this code is "dos only", but with a different "putchar", the method of converting a number to binary ascii would work with any OS.

The "key" to understanding this code is knowing that "shl" puts the "evicted" bit in the carry-flag...

Nice example!

Best,
Frank
Title: Re: how to print bits using nasm
Post by: nobody on December 11, 2007, 04:27:35 AM
http://img138.imageshack.us/img138/2069/95050128fb5.png (http://img138.imageshack.us/img138/2069/95050128fb5.png)