# Converting Numbers from Decimal to Binary

Page 1 of 1

## 5 Replies - 2403 Views - Last Post: 19 May 2010 - 07:05 AM

### #1 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

# Converting Numbers from Decimal to Binary

Posted 21 September 2006 - 05:37 AM

Description: Currently implements conversion for numbers between -255 and 255 (1 byte numbers). Modify the 0x80 value stored in EDI in the beginning to use it for larger values.Simple Assembly Code Snippet that can convert a Decimal Number into Binary using the AND instruction.
```; Code written by Sanchit Karve
;                   born2c0de
;           printf("I'm a %XR",195936478);
; Target Processor : 16-bit 80x86 Microprocessor or Higher.

; No User Interface code, since this is a code snippet.

; Code can be copied and pasted in your code since
; PUSHA and POPA have been used to prevent changes to
; previous Register Values.

; For use with 32-bit Assemblers, you can also replace
; all registers with 'E' (Eg. SI becomes ESI, AX -> EAX)

; Assumption:
; Value(in decimal) to be converted in Binary should be
; in register SI.
; Move it from Stack or from Variable to SI register
; before using this code.

PUSHA
;  SI Contains the Value to be converted.
MOV BX,80h
MOV AX,BX
MOV DX,SI

loop_begin:
AND DX,AX
TEST DX,AX
XOR DX,DX
INC DX

PUSHA

;  Call Output Function or INT21h, and pass value
;  stored in DX as output.
;  If you're planning to pass it as an argument to a
;  function, use PUSH DX to pass it via stack.
;  Remember to shift the SP (or BP if you're opening
;  stack frame) accordingly up the stack after the
;  number has been displayed by the function.

POPA
MOV DX,SI
MOV AX,BX
SHR AX,1
MOV BX,AX
TEST BX,BX
JNZ loop_begin
POPA
```

Is This A Good Question/Topic? 0

## Replies To: Converting Numbers from Decimal to Binary

### #2 Zerobu

• Black Hatter

Reputation: 13
• Posts: 1,822
• Joined: 14-January 08

## Re: Converting Numbers from Decimal to Binary

Posted 05 April 2009 - 12:12 AM

does this work with floating point values?

### #3 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

## Re: Converting Numbers from Decimal to Binary

Posted 07 April 2009 - 05:46 AM

No, but it can be modified to work with floats.

### #4 verodeg

• New D.I.C Head

Reputation: 0
• Posts: 14
• Joined: 26-December 08

## Re: Converting Numbers from Decimal to Binary

Posted 19 May 2010 - 07:02 AM

i dont understand. how can it run?

### #5 verodeg

• New D.I.C Head

Reputation: 0
• Posts: 14
• Joined: 26-December 08

## Re: Converting Numbers from Decimal to Binary

Posted 19 May 2010 - 07:04 AM

.model small .stack 100h .data parlist label byte maxlen db 20 actlen db ? input db 20 DUP (' ') str1 db "input: \$" str2 db "output: \$" .code mov ax,@data mov ds,ax mov ax,3h int 10h LEA dx,str1 mov ah,9 int 21h mov ah,0ah LEA dx,parlist int 21h LEA dx,actlen mov bx,dx mov cl,[bx] mov ch,0 mov si,0 add si,cx inc si mov dl,13d mov ah,2h int 21h mov dl,10d mov ah,2h int 21h mov dl,13d mov ah,2h int 21h LEA dx,str2 mov ah,9 int 21h mov si,dx pusha mov bx,80h mov ax,bx mov dx,si loop_begin: and dx,ax test dx,ax jz jump_ahead xor dx,dx inc dx jump_ahead: pusha int 21h POPa MOV DX,SI MOV AX,BX SHR AX,1 MOV BX,AX TEST BX,BX JNZ loop_begin POPa mov ah,4ch int 21h end

### #6 verodeg

• New D.I.C Head

Reputation: 0
• Posts: 14
• Joined: 26-December 08

## Re: Converting Numbers from Decimal to Binary

Posted 19 May 2010 - 07:05 AM

how come mine doesn't work?