3 Replies - 949 Views - Last Post: 18 April 2013 - 06:55 PM

#1 Garibanus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-April 13

x86 integer to ascii

Posted 17 April 2013 - 09:16 PM

Hi guys,
I convert int values to ascii for instance 221 should show up as 32 32 31 but my method pushes onto stack as 31 32 32 which is opposite. any idea what I am doing wrong?
Thanks,
Is This A Good Question/Topic? 0
  • +

Replies To: x86 integer to ascii

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 876
  • View blog
  • Posts: 2,312
  • Joined: 28-March 11

Re: x86 integer to ascii

Posted 17 April 2013 - 09:46 PM

no idea without seeing your code, my crystal ball is down... post the code you are using
Was This Post Helpful? 0
  • +
  • -

#3 Garibanus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-April 13

Re: x86 integer to ascii

Posted 17 April 2013 - 09:53 PM

Here is my code while loop is not logic but dont worry about it.
mov ebx,lpStringToStore							 ;ebx=addr. of the str.
	mov eax,val										 ;eax=val
	mov esi,10d										 ;esi=10 divisor
	cdq											 	 ;extend edx:eax
	.while eax!=-1									 ;dummy instruction to keep loop alive.
	.if eax<10									 	 ;single digit no need to be divided.
	add al,30h
	mov byte ptr [ebx], al							 ;save rem.
	inc ebx											 ;inc index
	jmp done
	.else											 ;eax>10
	cdq
	idiv esi										 ;eax/10
	add dl,30h
	mov byte ptr [ebx], dl							 ;save rem.
	inc ebx											 ;inc index
	.endif											 ;endif	
	.endw											 ;end of the loop	
done:												 ;done	


Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 876
  • View blog
  • Posts: 2,312
  • Joined: 28-March 11

Re: x86 integer to ascii

Posted 18 April 2013 - 06:55 PM

It has to do with the Endianness of the CPU. INTEL/AMD CPUs are "Little Endian", which means the least significant digit is on the left in the Register (Not how you see it on paper). If you do:
mov   eax, "ABCD"

It will be stored in eax as "DCBA"

"Big Endian" however, is stored in the CPU "as it is written on paper". So for a Big Endian CPU, "ABCD" will be stored as "ABCD". After your conversion, you need to reverse the string, this is how almost all DWORD to ASCII routines do it. Your code is basically correct, you just need to reverse the digits when done, and maybe take into account for signed/unsigned numbers.

Endianness on Wikipedia
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1