I need help converting numbers...

I'm supposed to make a program that takes an input, which can be binary, hex, decimal or octal, then convert said input to the remaining bases.

It's supposed to have a menu, where I can pick the base of the input.

I found a code on the internet for converting decimal to binary:

radix 10

;org 100h

; load binary value: 
; (hex: 5h)

mov al, 2

; print result in binary: 
mov bl, al
mov cx, 8
print: mov ah, 2   ; print function. 
       mov dl, '0'
       test bl, 10000000b  ; test first bit. 
       jz zero
       mov dl, '1'
zero:  int 21h
       shl bl, 1
loop print
; print binary suffix: 
mov dl, 'b'
int 21h


; wait for any key press: 
mov ah, 0
int 16h

ret

My problem here is that we havent been taught some of the other commands there, specifically RADIX, TEST and SHL. Also, I can't make this code accept any form of input. Tried mov ah,01h then int 21h but it didnt come out right.

Please, I need help ASAP..

RADIX ? Haven't heard of that!

TEST is a Read-Only AND. Does the same AND but the result is not stored, but the flags are still set!

A  B    A ^ B
0  0       0
0  1       0
1  0       0
1  1       1

For SHL MSB is shifted into Carry and zero is shifed into LSB.

10100101         A5h
Carry  <--- MSB             LSB <-- 0

Carry      Bits
   X   10100101

   1   01001010    <-- 0
   0   10010100
   1   00101000
   0   01010000
   0   10100000
   1   01000000
   0   10000000
   1   00000000

I'm modified your code to be branchless. You should examine how it functions! You should avoid LOOP unless you are on an ancient 8086.

; print result in binary: 
       mov ch, al          ; Get 8-bit byte
       mov cl, 8

print: mov dl,'0'
       shl ch,1            ; shift  MSB into carry
       adc dl,0            ; '0' or '1'    dl = dl + 0 + carry
      mov ah,2
       int 21h

       dec cl
       jnz print

      ; print binary suffix: 
   mov dl, 'B'  ;
   int 21h      ; DOS interrupt

Thanks, but I need to convert my numbers to 16-bit binary..how do I go about that?

EDIT:
I also need a way for the numbers to accept input..also this is only for the binary portion....the real assignment is to convert one base to all the other bases (i.e. decimal to hex, binary and octal, binary to hex, dec, and oct and so on)

This prints 16-bit boolean values

; print result in binary: 
       mov bx, ax          ; Get 16-bit halfword
       mov cl, 16          ; 16 bits

print: mov dl,'0'
       shl bx,1            ; shift  MSB into carry
       adc dl,0            ; '0' or '1'    dl = dl + 0 + carry
      mov ah,2
       int 21h

       dec cl
       jnz print

      ; print binary suffix: 
   mov dl, 'B'  ;
   int 21h      ; DOS interrupt

Wow, it works! Thank you so much!

Now all I need is a mechanism to accept an input so that the value doesn't have to be predefined in-program, then I'll be all set to try the next few sets.

Hmm ok, now I've got something.

Now I need to convert a string input into decimal so that it ties in with the convert code above...any ideas?

Come on, that's really easy! You need to think these things out instead of asking to be spoon fed. You need to write the code first then I'll review!

char ary[] = "1234"

n = 0
v = 0
while ary[n]
     i = ary[n] - 0x30
     v = (v * 10) + i
     n++
end while

v is number

Well I'm sorry for being sick with the flu for a week when we took up this lesson.

And...isn't that a C++ code?

EDIT:
Oh, I see.

This is what I've got. I've got a byte/word combination error, and I still have no idea on how to convert the DECIMAL string into a real decimal string.

~^
#ERROR messages will be removed if you leave these first two lines in     @@@@#
JMP MAIN

PROMPT1 	DB      'INPUT DECIMAL: $'

DNAME		LABEL	BYTE
DMAXL		DB	5
DLEN		DB	?
DECIMAL		DB	5 DUP ('$')



MAIN:

INPUT_DECIMAL:
	MOV 	AH,09H
        LEA 	DX,PROMPT1
        INT 	21H

        MOV 	AH,0AH
        LEA 	DX,DNAME
        INT 	21H

	CALL 	NEWLINE

	mov 	ax,decimal
~             ^
#ERROR 13: Byte/Word Combination Not Allowed                              @@@@#


       ; print result in binary: 
	mov 	bx, ax          ; Get 16-bit halfword
	mov 	cl, 16          ; 16 bits

print: 
	mov 	dl,'0'
       	shl 	bx,1            ; shift  MSB into carry
       	adc 	dl,0            ; '0' or '1'    dl = dl + 0 + carry
      	mov 	ah,2
       	int 	21h

       	dec 	cl
       	jnz 	print

	mov 	dl, 'B'  ;
	int 	21h      ; DOS interrupt

; wait for any key press: 
	mov 	ah, 0
	int 	16h	

	ret



;DISPLAY:
;	CALL	NEWLINE
;	MOV 	AH,09H
;       LEA 	DX,DECIMAL
;       INT 	21H

;EXIT:
;	INT	20H

NEWLINE:

        MOV 	AH,02H
        LEA 	DL,0AH
        INT 	21H
        MOV 	DL,0DH
        INT 	21H
        RET

I use pseudo code or C (NEVER C++) to proof an logic flow before writing it in assembly code!

You need something like...

DECIMAL		DB 32         ; BIGGER BUFFER  N + 3 
              	                DB 0
                                DB 0
                               DB 32 DUP (' ')

	lea 	bx,DECIMAL       ; <-- Get the case correct!
                call           GetInteger
                      ; ax,dx contain decimal number

...same as you did for printing the strings! Note, as usual I'm doing this in my head so it may not assemble or work.

GetInteger:
   mov cl,[bx+2]     ; Get # of characters read into buffer
   dec cl    ; remove carriage return from length?
   xor dx,dx           ; val = 0
$num:
   mov ax,dx
   shl  dx,3     ; x 8
   add dx,ax  ; x 9
   add dx,ax  ; x 10
   
    mov ah,0
    mov al,[bx+3]    ; get a character
    inc  bx

    sub al,'0'
    add dx,ax           ;   val = (val * 10) + digit

    dec cl
    jnz  $num

    mov  ax,dx
     ret

guys... can you pls..help me...we have a project in assembly a86 w/c is the 4 basic mathematical operation...you should input value...and my problem is on the division, it always divideover flow...what should i do? pls guide me...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.