Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Base Conversion

Status
Not open for further replies.

bpAgent86

Programmer
Aug 21, 2002
2
0
0
US
Is there an easy way to convert an integer number (base-10)to another base? (like base-5)

 
yes it is easy
you divide the 10 base number out of 5 or any base you want then take the remainder as the left digit redo with the dividend
like the code below


mov eax, (the wished number)
mov cx, 14
a:
xor edx, edx (or extend the sign by cxd instruction i think)
div word ptr 5
shl edx,12
shld memory, edx, 4
mov edx, eax
loop a:
this is example the result is the memory value clear it before use
mov cx, 14 14 if base 5 it must be changed if not 5
 
sorry i gave you a false code example, because i usually did; dividing by ten's power (to convert from hex to decimal and display a bounded num) and put remainder in memory from left to right.
yesterday my brain was off; i mixed the both ideas
ok, never mind
in your case you must divide by the base then you put the remainder in memory from right to left.
the right code is:
mov eax, (wished number)
a:
xor edx, edx
div word ptr 5 (base)
mov memory, dl
inc memory(memory=num after converting)
cmp eax, 0
jz b:
mov eax, edx
jmp a:
b:
nb1: number of 3 digit in decimal base gives number of 7 digit at least in binary base; converting is giving a new representation to any number so you should give memory enough size
nb2: num in base 5 is represented by 0 1 2 3 4 ; you can leave the remainder in nibble you don't need a byte for each digit however if the base is > 8 you should use bytes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top