theifyppl 0 Newbie Poster

Hey everyone. I'm using GAS assembler (AT&T syntax) under linux, and I'm trying to make a function that converts a string of numbers to an integer. I thought I had everything nailed down, but when I created a simple test program to test out the function, it caused a segmentation fault. I ran the program with gdb, and it told me the segmentation fault was occurring in the conversion_loop(). Also note that when I call exponent, I'm calling a function from a different file that I have. It is accounted for. Here is my code:

####FUNCTION: String2Int####
####
####
####ARGS: string to convert
####
####
####created by: theifyppl
.section .data

.equ ST_STRING2CONVERT, 8

.section .bss

.section .text
.globl String2Int
.type String2Int, @function
String2Int:

pushl %ebp
movl %esp, %ebp

xorl %ecx, %ecx
movl ST_STRING2CONVERT(%ebp), %eax

conversion_loop:
movb (%eax), %bl
cmpb $0, %bl
je end_conversion_loop
incl %ecx

subb $48, %bl
#value now correct, push onto stack
movzx %bl, %ebx
pushl %ebx
incl %eax

jmp conversion_loop

end_conversion_loop:
#pushl dummy for later use
pushl $0

loop_2:

#subtract 1 from counter
subl $1, %ecx

#compare counter to zero, if zero exit
cmpl $0, %ecx
je end_loop_2

#take the power of 10 based on the counter, counter is now 1
pushl $10
pushl %ecx
call exponent
addl $4, %esp

#first get back the counter, so popl back into ecx
popl %ecx

#add 4 to stack pointer
addl $4, %esp

#take out final answer
popl %ebx
#take the current number, multiply by the result of the power exponent 
#function, which is in %eax currently
popl %edi
imul %eax, %edi
#answer in edi, we are now at 60, now we add sixty to prev number and reloop
addl %edi, %ebx
#final answer in ebx, push it back onto stack
pushl %ebx

jmp loop_2

end_loop_2:
#take out final answer so far
popl %ebx
#should be one remaining number to add, take it out as well
popl %edi
#add
addl %edi, %ebx
#final answer FINALLY in %ebx, but in functions, the return value is in eax
movl %ebx, %eax

movl %ebp, %esp
popl %ebp
ret

Here is my test program I made:

.include "linux.s"

.section .data

.section .text
.globl _start
_start:
movl %esp, %ebp

.equ ST_ARG1, 16

movl ST_ARG1(%ebp), %eax
pushl %eax
call String2Int
#answer in %eax, exit with status code

movl %eax, %ebx
movl $SYS_EXIT, %eax
int $CALL_SYS

Please understand I'm quite new to assembly. This attempt was all mine, and I might have made multiple mistakes. Does anybody see where the segmentation fault is happening? I've been at this for hours.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.