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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

append characters

Status
Not open for further replies.

dsmiller

Programmer
Aug 13, 2002
2
US
This is my second week of assembly programming.
I am writing an inline asm function that converts a base 16 string to base 2, 8, 10, or 16 string.
I am able to convert the string into a base 10 integer and
I know how to convert to the desired base but I can't figure out how to append each succesive character.

This is what i am doing:[tt]

;eax contains a base 10 integer like 23
;all registers have been pushed onto stack & xor'd
;out_str is where I want to store my converted string
;in this case the result would be "10111"

mov bx, base ;desired base
lea ecx, out_str ;is this right??
conv_loop:
div bx ;convert to desired base
add dx, 48 ;convert from int to char
cmp dx, 57 ;check for hex
ja fix_for_hex ;fix hex number
return_:
mov [ecx+esi],dl ;shot in dark.no work
inc esi ;inc counter
xor edx, edx ;clear edx
cmp ax, bx ;<= 0?
jae conv_loop ;loop

fix_for_hex:
add dx, 7
jmp return_[/tt]

thanks
 
Better review the differences between a variable and a variable address.

'LEA' loads the effective address of a variable into a register or variable.

LEA thus loads the address of out_str into ecx.

'DIV BX' divides dx:ax by bx. BUT you haven't loaded anything into those yet. You could have loaded a byte by using 'mov al,[ecx]', and of course do adjustments.

However, to do ANY conversions between strings, you need to do:

number=stringtonumberfunction(string)
newstring=numbertostringfunction(number)

So first, convert the string to the number it represents, then convert that number to a string with the proper base. &quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
Thanks for the reply AmkG
The comments near the top of my code snippet indicate that eax is filled with an integer. This integer is ready to be converted to whatever base then returned as a *char.
The loop works as desired however I don't know how to _append characters_. I want to build a string from right to left, like filling an array backwards.
I am open to any suggestions for a better way of doing this.

Thanks again!
 
You just solved it yourself: filling an array backwards.

The maximum length of the output string is reached by using a base of 2 and the highest possible number, in this case, 11111111111111111111111111111111 (in binary). This is 32 characters. Use a 32-character output buffer, then *initialize esi to 32*. DEC esi *before* mov byte ptr [ecx][esi],dl and of course remove the inc esi.

After the loop, esi will point to the beginning of your string. So copy from {esi=esi up to esi=32} to the start of out_string.

mov edi,0
loopcopy:
mov al,byte ptr [ecx][esi]
mov byte ptr [ecx][edi],al
inc esi
inc edi
cmp esi,32
jb loopcopy

You do realize that ecx isn't needed if you use a static location buffer?

Also: it is better to divide by ebx since your source is EAX and not ax only. Just make sure to clear the higher word of ebx: and ebx,0ffffh.

&quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top