3

So i am trying to write basic string to integer function in x86 asm. I know there is a problem in my function str2int but i don't know which state causes error.

#string2integer.s
.data
number: .long 0

.globl str2int
.type str2int, @function

# eax -> Current char to int value
# ebx -> Multiplier value(starting from 1)
# ecx -> counter
# edi -> address value
str2int:
    pushl %ebp
    movl %esp, %ebp

    # address of base char*
    movl 8(%ebp), %edi

    # calculate the last index of char*
    pushl 8(%ebp)
    call count_chars
    addl $4, %esp

    # load last index to register ecx
    movl %eax, %ecx
    decl %ecx

    # set up ebx for base 1
    movl $1, %ebx

str2int_lp:
    cmpl $0, %ecx
    jz str2int_end

    movzbl (%edi,%ecx,1), %eax
    subl $'0', %eax

    # 
    imull %ebx, %eax
    addl %eax, number
    
    # set up registers for next step
    decl %ecx
    imull $10, %ebx
    jmp str2int_lp

str2int_end:
    movl number, %eax
    movl %ebp, %esp
    popl %ebp
    ret

and this is my test.c file

#include <stdio.h>
#include <stdlib.h>

extern int str2int(char*);

int main() {
    char str[] = "331";
    printf("%d\n", str2int(str));

    return 0;
}

This is the info registers output from GDB before running the str2int function

eax            0x0                 0
ecx            0xffffc1d0          -15920
edx            0xffffc1f0          -15888
ebx            0x56558fd4          1448447956
esp            0xffffc1a0          0xffffc1a0
ebp            0xffffc1b8          0xffffc1b8
esi            0xffffc28c          -15732
edi            0xf7ffcb60          -134231200
eip            0x565561e6          0x565561e6 <main+47>
eflags         0x246               [ PF ZF IF ]
cs             0x23                35
ss             0x2b                43
ds             0x2b                43
es             0x2b                43
fs             0x0                 0
gs             0x63                99
k0             0x0                 0
k1             0x0                 0
k2             0x0                 0
k3             0x0                 0
k4             0x0                 0
k5             0x0                 0
k6             0x0                 0
k7             0x0                 0

And after

Program received signal SIGSEGV, Segmentation fault.
0x5655900c in str2int ()
eax            0xffffc1a8          -15960
ecx            0xffffc1d0          -15920
edx            0xffffc1f0          -15888
ebx            0x56558fd4          1448447956
esp            0xffffc18c          0xffffc18c
ebp            0xffffc1b8          0xffffc1b8
esi            0xffffc28c          -15732
edi            0xf7ffcb60          -134231200
eip            0x5655900c          0x5655900c
eflags         0x10292             [ AF SF IF RF ]
cs             0x23                35
ss             0x2b                43
ds             0x2b                43
es             0x2b                43
fs             0x0                 0
gs             0x63                99
k0             0x0                 0
k1             0x0                 0
k2             0x0                 0
k3             0x0                 0
k4             0x0                 0
k5             0x0                 0
k6             0x0                 0
k7             0x0                 0

count_chars function:

.globl count_chars
.type count_chars, @function

count_chars:
    pushl %ebp
    movl %esp, %ebp

    pushl %ebx

    movl 8(%ebp), %ebx
    xorl %ecx, %ecx
count_chars_lp1:
    movb (%ebx), %al

    cmpb $0, %al
    je count_chars_end

    incl %ebx
    incl %ecx
    jmp count_chars_lp1

count_chars_end:
    movl %ecx, %eax
    popl %ebx
    movl %ebp, %esp
    popl %ebp
    ret

I am still trying to learn gdb. So, if you need me to show anything else, just let me know please.

I’ve already tried to fix it with ChatGPT, but its comments and suggestions don’t make sense. It keeps telling me to compile my code for a 32-bit system, but I’ve already done that.

6
  • 1
    "I am still trying to learn gdb" - a sensible decision, see What is a debugger and how can it help me diagnose problems? Commented Sep 17 at 8:05
  • 1
    I cannot assemble and test your code as the count_chars function is missing. Please provide your full code or a minimal reproducible example. Commented Sep 17 at 8:43
  • I am adding count_chars function right now. But i've already testing i didn't see any error. Commented Sep 17 at 8:44
  • @Friedrich: For assembly, see also the bottom section of stackoverflow.com/tags/x86/info . It's even more important for assembly than for most languages, because so many different problems can give the same symptom (segfault). Single-stepping and displaying registers / memory is most of what you need. Commented Sep 17 at 8:58
  • ChatGPT is terrible at assembly language; don't waste your time with it. e.g. see comments on Does this ChatGPT "swap" snippet do anything? for the kind of bug-ridden nonsense it spits out. Commented Sep 17 at 9:00

1 Answer 1

4

Your main issue is that you forgot to switch back to .text before issuing the text of your function. This means that the function ends up being placed in the data section .data, which is not executable.

Fixing that, your code outputs 31, not 331. This is due to an off-by-one-error in your code where you break when ecx is zero (i.e. at the beginning of the string), but do not actually process the first character. Fix this by moving the “ecx is zero” check to the end of the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot sir i fixed the way you said and that's perfect now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.