0

The problem I am facing right now is reversing my stack.

I was able to change symbols that I wanted but I'm stuck on reversing my stack.

This is my code so far:

section .text
    global do_Str
    extern printf

do_Str:

    push rbp
    mov  rbp, rsp
    mov  rcx, rdi
    mov  rbx, rdi
    mov  rdx, rcx
    
   

    mov r10, 0
    mov r9, 0

    label:
    
    cmp byte[rcx], 'a'
    jl cont
    cmp byte[rcx], 'z'
    jg cont
    and byte [rcx], 11011111b
    cont:
    
    cmp byte[rcx], '('
    jne contP
    mov byte[rcx], '<'
    contP:
    
    cmp byte[rcx], ')'
    jne contR
    mov byte[rcx], '>'
    contR:

    cmp byte[rcx],'A'
    jl contL
    cmp byte[rcx], 'Z'
    jg contL
    inc r9
    contL:

    _push_loop:
    mov al,[rdx]
    cmp al, 0
    jz _pop_loop

    push ax
    inc rdx
    jmp _push_loop

    _pop_loop:
    mov al,[rbx]
    cmp al,0
    jz _done

    pop ax
    mov [rdx], al
    inc rbx
    jmp _push_loop

    _done:



    inc r10
    inc rcx
    cmp byte [rcx], 0
    jnz label






    sub r10, r9
    mov rax, r10
    mov rsp, rbp
    pop rbp
    ret

I have tried changing my registers and pointers use but all of them resulted to the same error:

Segmentation fault (core dumped)

I am not allowed to create a new function in my C file

This is my C code :

#include <stdio.h>
#define MAX_LEN 100

extern int do_Str (char*);

int main(void){
    char str_Buf[MAX_LEN];
    int counter = 0;

    fgets(str_Buf,MAX_LEN, stdin);

    counter = do_Str (str_Buf);

    printf("%s%d\n",str_Buf,counter);
    
    return 0;
}

I have searched it on Stack Overflow and didn't find a solution for my problem.

The full assignment was : 1. change lower case letters to uppercase from the input 2. count how many chars are not letters 3.reverse the input - I am stuck here and I think I don’t fully realize the flow of the stack.

EliKnaffo
  • 354
  • 5
  • 17
  • 2
    Do you mean reverse your **string** not stack? You can just swap the last character with the first and proceed toward the middle. – Jester Apr 21 '21 at 12:09
  • @Jester I get an input from the user - a string and as you can see I push it to a stack. I don't know how to change the places if you can help me figure that out that would be helpful. – EliKnaffo Apr 21 '21 at 12:18
  • 1
    `mov rbx, rdi` - that's not safe without saving it first, the caller can assume that RBX is unmodified after calling your function, just like RBP. You have enough other registers; you can avoid RBX and other call-preserved registers. [What registers are preserved through a linux x86-64 function call](https://stackoverflow.com/q/18024672). In fact you're not even using RDI, just modify it instead of RBX. – Peter Cordes Apr 21 '21 at 19:21
  • I have updated the question. – EliKnaffo Apr 21 '21 at 22:24

0 Answers0