Nitta
Nitta

Reputation: 9

How to check if string is palindrome in Assembly? [code help]

I never did Assembly and we just started it in school, professor gave us this code, but I didn't get something right and I don't know what.. can you see the problem?

.section .data
    status: .byte 1
    string_A: .ascii "kapak\0"
.section .text
.globl main
main:
    movl $0, %eax
    pushl $string_A
    call string
    addl $8,%esp

.type string, @function
string:
    pushl %ebp
    movl %esp, %ebp
    movl 8(%ebp), %esi
    movl %esi, %edi

    loop:
        movl $4, %esi
        cmpl $0, %esi
        jg petlja
    again:
        movb (%esi), %al
        cmpb (%edi), %al
        je uvecaj
        jne exit
    increase:
        addl $1, %edi
        addl $-1, %esi
        jmp again

    movl %ebp, %esp
    popl %ebp
    ret

exit:
    movl $1,%eax
    movl $0,%ebx
    int $0x80

Upvotes: -1

Views: 1804

Answers (1)

user3185968
user3185968

Reputation:

You should give a bit more info than that for us:

You need help with "assembly". Well, what assembly? I can guess you're writing for x86, but you might want to make that explicit in your question. Also, comment your code, especially in assembly. It's not made for people to read easily. Also, when you translate your code into English, translate all of it. Your jump-targets don't exist in your code.

Anyway, you're in luck because this program is simple enough to read some of it anyway:

In your main()-function, you call string() (which is a bad name...), but after string() returns, what happens? Right, the stack-pointer is increased by eight. Wait, 8? But you only pushed a pointer in pushl $string_A! That's only 32-bits (or 4 bytes) on this architecture! -> Fail.

Even if that didn't blow up, after that you don't return or exit. You simply keep executing linearly... right into string(). So now string reads its pointer some 8 + 4 = 12 bytes from where it should be. That pointer could point anywhere, if the page access violation didn't already segfault.

I could try to reverse engineer where your jumps in string() should go, but you should really post readable code, so I won't.

Upvotes: 1

Related Questions