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

#1 born2c0de   User is offline

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

Reputation: 187
  • View blog
  • 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
   JZ jump_ahead
   XOR DX,DX
   INC DX

jump_ahead:
   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   User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de   User is offline

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

Reputation: 187
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 verodeg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 verodeg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 verodeg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1