flat assembler
Message board for the users of flat assembler.

Index > Main > Show binary representation of a byte variable

Author
Thread Post new topic Reply to topic
majidkamali1370



Joined: 31 Oct 2010
Posts: 50
Location: Iran
majidkamali1370 20 Nov 2011, 16:45
Hi.
I created a macro that shows binary representation of a byte variable.
Here's my code:
Code:
macro BinaryPrint1 number
{
        local ..mask
        pusha

   ..mask db 1
 shl [..mask], 7
     mov al, number

  mov si, 7
   forLoop:
                test al, ..mask
             jz Zeroed
           jnz NotZeroed
               Zeroed:
                 PrintChar '0'
                     jmp @f
              NotZeroed:
                      PrintChar '1'
                     jmp @f
              @@:
                     shr [..mask], 1
                     dec si
                      cmp si, 0
                   ja forLoop

      popa
}    

and PrintChar is as follows:
Code:
macro PrintChar ch
{
       pusha
       mov dl, ch
  mov ah, 2h
  int 21h
     popa
}    


but when I use this macro in a program bellow, it generates "value out of range" error
Code:
org 100h
jmp start

include '0Header.inc'

num db 35

start:
    BinaryPrint1 num

int 20;    


what's the problem?
Thanks Very Happy
Post 20 Nov 2011, 16:45
View user's profile Send private message Send e-mail Yahoo Messenger ICQ Number Reply with quote
DJ Mauretto



Joined: 14 Mar 2007
Posts: 464
Location: Rome,Italy
DJ Mauretto 20 Nov 2011, 18:16
Hello,
Try This:
Code:
macro BinaryPrint1 number
{


        pusha
  mov bl,80h
        mov al, number
        mov si, 8

        forLoop:
                test al, bl
                jnz NotZeroed

                PrintChar '0'
                jmp @f

                NotZeroed:

                        PrintChar '1'
 
                @@:
                        shr  bl, 1
                        dec  si
                        test si,si
                        jnz  forLoop

        popa
}

macro PrintChar ch
{
        pusha
        mov dl, ch
        mov ah, 2h
        int 21h
        popa
}

org 100h

        BinaryPrint1 35
       int 20h;    

_________________
Nil Volentibus Arduum Razz
Post 20 Nov 2011, 18:16
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 786
Location: Massachusetts, USA
bitshifter 20 Nov 2011, 18:41
Making macro for fun is ok, but in real world this would assemble and run slow.
Besides being huge from inline expansion problem.
I suggest to use procedure, then reuse only cost sizeof param and call.

Here is my super simple 17 byte routine Smile
Code:
use16
org 0x0100

number_of_the_beast = 666

  mov bx,number_of_the_beast
  call display_bin16
  xor ah,ah
  int 0x16
  ret

display_bin16:
; bx = value
  mov ah,0x02
  mov cx,16     ;number of bits in value
.das_loop:
  xor dx,dx
  shl bx,1
  adc dl,'0'
  int 0x21
  loop .das_loop
  ret
    
Post 20 Nov 2011, 18:41
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1308
Location: Paradise Falls
Picnic 21 Nov 2011, 09:45
I can save one more byte Smile

Code:
adc dl,'0'    ; 3 bytes
adc al,'0'    ; 2 bytes    


Code:
display_bin16:                  
        mov cx, 16
        mov ah, 0Eh
.das_loop:
        xor al, al
        shl bx, 1
        adc al, '0'
        int 10h
        loop .das_loop
        ret  
    
Post 21 Nov 2011, 09:45
View user's profile Send private message Reply with quote
majidkamali1370



Joined: 31 Oct 2010
Posts: 50
Location: Iran
majidkamali1370 21 Nov 2011, 12:05
Can I create one proc for handling 1,2 and 4 byte data?
Post 21 Nov 2011, 12:05
View user's profile Send private message Send e-mail Yahoo Messenger ICQ Number Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 786
Location: Massachusetts, USA
bitshifter 21 Nov 2011, 19:01
Sorry Picnic, but some older BIOS'es use BX register for parameters.
http://www.ctyme.com/intr/rb-0106.htm
Post 21 Nov 2011, 19:01
View user's profile Send private message Reply with quote
majidkamali1370



Joined: 31 Oct 2010
Posts: 50
Location: Iran
majidkamali1370 21 Nov 2011, 19:07
I don't understand this line of your code
adc al, '0'
actually I don't understand where, CF become one.
Post 21 Nov 2011, 19:07
View user's profile Send private message Send e-mail Yahoo Messenger ICQ Number Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1499
Location: Toronto, Canada
AsmGuru62 21 Nov 2011, 19:42
SHL BX,1

It will push bit #15 out into CF.
Post 21 Nov 2011, 19:42
View user's profile Send private message Send e-mail Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1308
Location: Paradise Falls
Picnic 22 Nov 2011, 10:38
bitshifter wrote:
Sorry Picnic, but some older BIOS'es use BX register for parameters.
http://www.ctyme.com/intr/rb-0106.htm


Small but important detail. I missed that Mad
Post 22 Nov 2011, 10:38
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.