News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Anyway toi use 'Print' to output a number in binary ?

Started by Rainstorm, November 22, 2006, 11:55:20 AM

Previous topic - Next topic

Rainstorm

hi

how would i use Print to output a number in binary.

thanks

Rainstorm

Tedd

Print(1);
Print(0);
Print(0);
Print(1);
Print(0);
Print(1);
Print(0);
Print(1);
:bdg


Or you could convert the number to a string of binary digits first, null-terminate it, and then print that :wink
No snowflake in an avalanche feels responsible.

Rainstorm

lol
this is the code i had tried, but that gives it in decimal for the byte in 'al'
how would i output/display the byte in al, in binary
start:
    xor eax, eax
    mov al, 11001b
    And al, al
    movzx eax, al

print ustr$(eax),13,10

inkey
exit

Rainstorm

#3
i tried with byt2bin_ex it assembles proper..but get the windows error msg.

    .data

buff10 db 12 (0)
pbuff dd 0
var10 db 0

    .code

start:

    xor eax, eax          ; clear eax

    mov al, 10001b
    And al, 1             ; some random operation

mov ebx, offset buff10
mov pbuff, ebx
;mov var10, al

invoke byt2bin_ex,al,pbuff

print pbuff,13,10

inkey
exit
end start

thnks..

Tedd

loop..
- get each bit by shifting (or rotating)
- test if a bit is 1, then "setnz al"
- add al,'0'
- al is now the "0" or "1" to print


remember to get the bits 'backwards' (so you print the highest bit first)
No snowflake in an avalanche feels responsible.

Rainstorm

ted,
i can't get how to do it like you said...
after shifting..the bit gets stored in the CF flag right ?

i attempted to do something like in the following code..
but it doesn't seem to be outputing the right results..& i don't know why.

also do you know why my previouss code with the byt2bin_ex function isn't working ?

    .data

buff db 64 dup(0)
pbuff dd 0

    .code

start:

xor bl, bl
xor al, al

mov bl, 00011001b 
sal bl, 1
setnc al
setc al

mov buff,al
mov esi, offset buff
mov pbuff, esi
print pbuff,13,10

inkey
exit

end start


thanks..

Rainstorm

hi,..

here i tried some code on the lines of your post..- it assembles proper.. but doesn't give the
right results,

Also, in your post i don't know what   add al,'0'    does exactly

     .data

buff db 64 dup(0)
pbuff dd 0
msk db 0

    .code
start:

xor bl, bl
xor al, al

mov esi, 0
mov bl, 0
mov al, 10000000b

printt:

mov msk, 00011001b 

test msk, al
setnz cl
movzx esi, bl

mov buff[esi], cl
mov buff[esi+1], 0

mov edi, offset buff

push eax
print edi
pop eax

sar al, 1
inc esi
inc bl
cmp bl, 7
jz xit
jmp printt

xit:
print " ",10
inkey
exit

end start

zooba

Quote from: Rainstorm on November 22, 2006, 11:27:26 PM
do you know why my previouss code with the byt2bin_ex function isn't working ?

Because you passed the value 'buff' ( = 0) instead of the address. Try this:

invoke byt2bin_ex,al,OFFSET pbuff

Cheers,

Zooba :U

Rainstorm

zooba,

QuoteBecause you passed the value 'buff' ( = 0) instead of the address. Try this:..

not understanding how i passed the value as 0..

mov ebx, offset buff10
mov pbuff, ebx                 ; that moves the address of the buffer into pbuff...right ?
invoke byt2bin_ex,al,pbuff


thanks..

zooba

You're right, sorry. I didn't read the code properly  :red

hutch--

Rainstorm,

Give this a blast.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .data?
      byte_buffer db 32 dup (?)

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    LOCAL pbuf  :DWORD
    LOCAL buffer[32]:BYTE

    push ebx

  ; ----------------
  ; use LOCAL memory
  ; ----------------
    xor ebx, ebx                    ; set EBX to zero
    mov pbuf, ptr$(buffer)

  @@:
    invoke byt2bin_ex,bl,pbuf       ; use BL as the BYTE value
    print pbuf,13,10                ; display the result
    add ebx, 1                      ; increment EBX for next BYTE value
    cmp ebx, 256                    ; exit if > 255
    jne @B

    inkey                           ; wait for a keypress

  ; ---------------------------------------
  ; use GLOBAL memory in the .DATA? section
  ; ---------------------------------------
    xor ebx, ebx                    ; set EBX to zero
    mov pbuf, OFFSET byte_buffer    ; load the OFFSET into a pointer

  @@:
    invoke byt2bin_ex,bl,pbuf       ; use BL as the BYTE value
    print pbuf,13,10                ; display the result
    add ebx, 1                      ; increment EBX for next BYTE value
    cmp ebx, 256                    ; exit if > 255
    jne @B

    pop ebx

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Rainstorm


Rainstorm

don't know the reason, my old code using byt2bin_ex didn't work though.
was trying to compare it with yours..

-

dsouza123

#13
Another way.

.data
    Src dd 87654321h, 0              ; could be used instead of array of bytes to show dwords
  szSrc db 21h,43h,65h,87h,0,0,0,0   ; in standard lo byte first format (little endian)
  szCap db "Binary output:",13,10
  szMsg db 256 dup (0)

.code
start:
   xor edi, edi
   mov esi, 4        ; count of bytes
NB:
   mov ecx, 8
   movzx eax, byte ptr [szSrc+esi-1]
@@:
   shl eax, 1
   xor eax, 3000h
   mov byte ptr [szMsg+edi], ah
   inc edi
   and eax, 0FFh
   dec ecx
   jnz @B

   mov byte ptr [szMsg+edi], ' '   ; remove, only in to highlight byte boundaries
   inc edi                         ; remove, only in to highlight byte boundaries
     
   dec esi
   jnz NB
   mov byte ptr [szMsg+edi], 'b'   ; remove, only have trailing b to indicate binary number

   print offset szCap,13,10
   inkey
   exit
end start


Edit added the missing offset needed for the print macro, also added a : in caption string.

PBrennick

Using Hutch's nice example, I notice that using global memory is faster than using local memory. An interesting observation.

Paul
The GeneSys Project is available from:
The Repository or My crappy website