8 Replies - 3644 Views - Last Post: 27 June 2012 - 08:17 PM

#1 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

How to convert a char into int

Posted 27 June 2012 - 04:53 PM

#------------------------------------------------------------------------------
# PROJECT EULER
# PROBLEM 8
#------------------------------------------------------------------------------
#Find the greatest product of five consecutive digits in the 1000-digit number.
#------------------------------------------------------------------------------
# SOLUTION: 40824
#------------------------------------------------------------------------------
# as -32 8.s -o 8.o
# gcc -m32 8.o -o 8
#------------------------------------------------------------------------------

data:
	.string "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"

dec32_format:
	.string "%d\n"

.section .text
.globl main

main:
	xor	%edi, %edi
	call	main_loop

#------------------------------------------------------------------------------
main_exit:
	xor	%eax, %eax
	incl	%eax
	xor	%ebx, %ebx
	int	$0x80

#------------------------------------------------------------------------------
.type print32, @ function

print32:
	pushl	4(%esp)
	pushl	$dec32_format
	call	printf
	addl	$8, %esp
	ret

#------------------------------------------------------------------------------

main_loop:
	cmpl	$1000, %edi
	je	main_exit
	pushl	data(, %edi, 1)
	call	print32
	addl	$4, %esp
	incl	%edi
	jmp	main_loop



Basically I have a huge string with a 1000 digit number. I need to pick out each individual number from it in order to do some calculations. I get the pick out part, but I don't know how to turn it into a usable integer. If I change my printf string to "%c\n", it prints out each character correctly. But if I change it to "%d\n", which is what I'm looking for, I get this:

.
.
.

859321346
909326340
808858376
808465923
942682118
842543104
858929152
842215944
892482306
926233091
892810498
859125509
808662279
875574021
842280963
808596480
925905412
892809218
842348288
959591687
909718021
859191554
875771401
892613382
808793091
3159300
620769285
1680146432

So how do I convert it into a number?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How to convert a char into int

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: How to convert a char into int

Posted 27 June 2012 - 05:26 PM

Everything is just a number. All the way down here in our neck of the woods, once you realize that then you can do what you want.

Take a look at this ASCII table

See that, all letters (Characters) are just numbers.

The numbers 0 - 9 are 48 - 57 behind the scenes. Now you say you are going to loop through the string and grab each single character to do some calculations on it?

Ok, take your ASCII number and subtract 48 from it, now you have the "int" representation of that number character.

So you have the ASCII character "7" which internally is 55, subtract 48 from 55 and what do you get?

Simple no? :-)
Was This Post Helpful? 0
  • +
  • -

#3 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: How to convert a char into int

Posted 27 June 2012 - 05:54 PM

View PostGunnerInc, on 27 June 2012 - 05:26 PM, said:

Everything is just a number. All the way down here in our neck of the woods, once you realize that then you can do what you want.

Take a look at this ASCII table

See that, all letters (Characters) are just numbers.

The numbers 0 - 9 are 48 - 57 behind the scenes. Now you say you are going to loop through the string and grab each single character to do some calculations on it?

Ok, take your ASCII number and subtract 48 from it, now you have the "int" representation of that number character.

So you have the ASCII character "7" which internally is 55, subtract 48 from 55 and what do you get?

Simple no? :-)


Yes, except I've already tried that (I must have done this 1000 times in C++) and it doesn't seem to work.

main_loop:
	cmpl	$1000, %edi
	je	main_exit
	movl	data(, %edi, 1), %ebx
	addl	$-48, %ebx
	pushl	%ebx
	call	print32
	addl	$4, %esp
	incl	%edi
	jmp	main_loop



All I get is the same output minus 48.

Unless I'm doing something obviously wrong?
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: How to convert a char into int

Posted 27 June 2012 - 06:09 PM

Just wrote this to test and make sure I am not crazy (any more than I am)

It is MASM

.data
szString            db  "731671765313306249192", 0
szFmtDec    db  "%d", 13, 10 ,0

.code
start:         

    lea     esi, szString
    
NextChar:
    movzx   eax, byte ptr [esi] ; mov current char at esi pointer to eax, zero extending it
    test    eax, eax            ; are we at the end of string?
    jz      Done                ; hit null, end of string we are done
    
    sub     eax, 48             ; take current ascii value and subtract 48
    push    eax                 ; pass it to out print function
    call    PrintIt             ;
    inc     esi                 ; increase our pointer
    jmp     NextChar
    
Done:
    inkey
    ret
    
PrintIt:
    enter   0, 0                ; lazy was of setting up stack frame
    
    push    dword ptr [ebp + 8] ; number passed to our function
    push    offset szFmtDec     ; format string
    call    crt_printf
    add     esp, 4 * 2          ; 2 params passed to printf, clean up stack
    
    leave                       ; restore stack
    ret     4 * 1               ; 1 param passed to our function, clean it up.
    
end start


And the result:
Attached Image
Was This Post Helpful? 0
  • +
  • -

#5 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: How to convert a char into int

Posted 27 June 2012 - 06:20 PM

edit: Never mind what I had here before.

I looked at your code a bit closer and figured out where I went wrong.

I was printing the memory address, not the value. I had never used movzx before.

Now time to actually work on the problem. :ninja:

Thank you again.


main_loop:
	cmpl	$1000, %edi
	je	main_exit
	movzx	data(, %edi, 1), %ebx
	addl	$-48, %ebx
	pushl	%ebx
	call	print32
	addl	$4, %esp
	incl	%edi


This post has been edited by carnivroar: 27 June 2012 - 06:25 PM

Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: How to convert a char into int

Posted 27 June 2012 - 06:30 PM

See the code, I put the address of the string in esi with lea, now esi contains the pointer to the string.

This:
movzx   eax, byte ptr [esi]

moves the current byte at the current pointer to eax
so taking your output above the pointer would be 134514611 on the first run, then
inc esi would make it 134514612 so the next time we do
movzx   eax, byte ptr [esi]
it will get the next byte.

Maybe this would be more clear. We will use ecx as the index for our pointer.
    lea     esi, szString
    xor     ecx, ecx
    
NextChar:
    movzx   eax, byte ptr [esi + ecx] ; mov current char at esi pointer index ecx
    test    eax, eax            ; are we at the end of string?
    jz      Done                ; hit null, end of string we are done
    
    push    ecx
    
    sub     eax, 48             ; take current ascii value and subtract 48
    push    eax                 ; pass it to out print function
    call    PrintIt             ;
    
    pop     ecx
    inc     ecx                 ; #### increase our index pointer
    jmp     NextChar



I think this:
movzx   eax, byte ptr [esi + ecx]

in NASM would be:
movzx   eax, byte [esi + ecx]

Was This Post Helpful? 1
  • +
  • -

#7 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: How to convert a char into int

Posted 27 June 2012 - 07:45 PM

Yes, makes sense. All I needed was movxz.

Anyways, SOLVED the problem. :gun_bandana:

I'll use this thread from now on to ask small questions so I don't make a new thread all the time, okay?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 856
  • View blog
  • Posts: 2,246
  • Joined: 28-March 11

Re: How to convert a char into int

Posted 27 June 2012 - 07:58 PM

Glad ya got it working! Movzx does the same as mov, but it Zero Extends the upper unused bytes of the register so it wont contain garbage.

Create a new thread for each new question, easier for folks to search for.
Was This Post Helpful? 0
  • +
  • -

#9 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: How to convert a char into int

Posted 27 June 2012 - 08:17 PM

View PostGunnerInc, on 27 June 2012 - 07:58 PM, said:

Glad ya got it working! Movzx does the same as mov, but it Zero Extends the upper unused bytes of the register so it wont contain garbage.

Create a new thread for each new question, easier for folks to search for.


Okay, great. I'll probably get stuck when I need to implement arrays and read from text files. I'll try my best to find a solution first.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1