I don't completely understand how to convert for dec to hex. but i need to write some code to do that. Where can I find a good tutorial that would explain it VERY clearly.

Thanks
Posted on 2005-01-10 18:24:14 by ptr_08_2004
Im sure there are many ways of solving this out there, but in the most direct, but propabaly not the most optomal solution, is to break the ASCII number into its fractional components and then brute force addet the fractions together in a loop:

"12345" can be viewed as:

"1*10000" "2*1000" + "3*100" + "4*10" + "5*1"

So a loop that will extract a single ascii character at a time. Convert the single character to binary (easy to do, '3' - 30h = 3h ) and multiply it by its digit position. This will build a new binary number based on the ascii value. If the Ascii number is being analysed from left to right, you can determine the initial digit multiplier by counting the number of ascii character. 5 characters = 10^(5-1) = 10000, 4 characters = 10^(4-1) = 1000, etc. The loop will continue adding up the number until the characters left = 0.

``````
Calchex PROC uses ebx esi lpAscii:DWORD, dLength:DWORD
Local RetrunVal:DWORD

xor ebx, ebx
mov ReturnVal, ebx
inc ebx
mov esi, dLength
.if esi
; Build Digit Multiplier
.while esi > 1
imul ebx, 10
dec esi
.wend

; Loop through ascii number and translate
mov esi, lpAscii
.while dLength
.break .if BYTE PTR [esi] == NULL
xor eax, eax
mov al, [esi]
inc esi
sub eax, 30h
imul eax, ebx
add ReturnVal, eax
idiv ebx, 10
dec dLength
.wend
.endif
mov eax, ReturnVal
ret
Calchex endp``````

I just rattled this off here on the forum. I didnt test it. But it should work. Or at the very least give you the idea...

Regards,
:NaN:

BTW: This type of topic is not very advanced and would be better suited for the Main forum area. I've probably offended this forums users by even posting such ugly source ;) <lol>
Posted on 2005-01-10 21:51:47 by NaN
Im sure there are many ways of solving this out there, but in the most direct, but propabaly not the most optomal solution, is to break the ASCII number into its fractional components and then brute force addet the fractions together in a loop:

"12345" can be viewed as:

"1*10000" "2*1000" + "3*100" + "4*10" + "5*1"

So a loop that will extract a single ascii character at a time. Convert the single character to binary (easy to do, '3' - 30h = 3h ) and multiply it by its digit position. This will build a new binary number based on the ascii value. If the Ascii number is being analysed from left to right, you can determine the initial digit multiplier by counting the number of ascii character. 5 characters = 10^(5-1) = 10000, 4 characters = 10^(4-1) = 1000, etc. The loop will continue adding up the number until the characters left = 0.

``````
Calchex PROC uses ebx esi lpAscii:DWORD, dLength:DWORD
Local RetrunVal:DWORD

xor ebx, ebx
mov ReturnVal, ebx
inc ebx
mov esi, dLength
.if esi
; Build Digit Multiplier
.while esi > 1
imul ebx, 10
dec esi
.wend

; Loop through ascii number and translate
mov esi, lpAscii
.while dLength
xor eax, eax
mov al, [esi]
inc esi
.break .if eax == NULL
sub eax, 30h  ;make ascii char to binary number under 10
imul eax, ebx ;multiply by digit value
add ReturnVal, eax ; add it to running total
idiv ebx, 10 ; move on to next digit value
dec dLength ; Note one less char to process and go again
.wend
.endif
mov eax, ReturnVal
ret
Calchex endp``````

I just rattled this off here on the forum. I didnt test it. But it should work. Or at the very least give you the idea...

Regards,
:NaN:

BTW: This type of topic is not very advanced and would be better suited for the Main forum area. I've probably offended this forums users by even posting such ugly source ;) <lol>
Posted on 2005-01-10 21:56:30 by NaN
I think it is better to scan for null character then work from the back. Easier to code.

Scan for null character and set eax = zero
@@:
Read one character and minus '0' and store in ecx
multiply eax by 10
add eax, ecx
if not the first character jmp @B

Once you got the value stored in eax, converting to hex is easy. just read 4 bits and output a character. You do not need to make use of any divisions...

Sorry for not coding it out, kinda lazy.
Posted on 2005-01-11 06:28:33 by roticv
Well I use Randall Hyde's "High Level Assembler", which has numerous built in conversion and input/output routines. Start herehttp://webster.cs.ucr.edu/AoA/Windows/HTML/AoATOC.html to get information on various programming issues. Error trapping is available as well. Somewhere on this site he has information on how to link in HLA libraries into MASM.

In one program I read a number (checkpoint time) from a file (inpH) into a 32 bit register using:
``        fileio.get (inpH, cktime );``

Somewhere else in the same program used a hand coded a routine (written before I learned file I/O or about various conversion routines) to read a number from the command line. I have minimal error checking. Index eax, final number edx. To multiply by 10 I multiply by 5 then 2. imul or mul affects more registers and may not be necessary. Also this seems more straightforward than scanning to the end of the line first. But there is validity checking

Algorithm:
If next character is a number, then current number**10+next character
Repeat.

``````     xor (edx, edx);
getn1a:
mov ([eax],  bl);
if (bl in '0'..'9') then
and (15, bl);
lea (edx, [edx + edx *4]);
lea (edx, [edx + edx]);
add (ebx, edx);
inc (eax);
jmp getn1a;``````

Then once the number is in the 32 bit register, you can convert it to a hexadecimal string with some or other built in conversion routine, or simply output it to the screen using:
``     stdout.put (edx);``

You can check out the source code for HLA at Webster for ideas on coding a conversion routine yourself.
Posted on 2005-01-11 19:57:43 by V Coder