Thread: Recursion && given stack memory challenge.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    I have never done any stack tracking, using the current program stack, though there may be a way to do it.

    As for converting code to assembly, you can do it with "gcc -S hello.c". You can probably do this in any compiler. You would need to read the documentation for the compiler and/or IDE.

    Source hello.c:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      printf("Hello World!\n");
    
      return 0;
    }
    Assembly hello.s:
    Code:
           .file   "hello.c"
            .text
            .section        .rodata
    .LC0:
            .string "Hello World!"
            .text
            .globl  main
            .type   main, @function
    main:
    .LFB0:
            .cfi_startproc
            pushq   %rbp
            .cfi_def_cfa_offset 16
            .cfi_offset 6, -16
            movq    %rsp, %rbp
            .cfi_def_cfa_register 6
            leaq    .LC0(%rip), %rax
            movq    %rax, %rdi
            call    puts@PLT
            movl    $0, %eax
            popq    %rbp
            .cfi_def_cfa 7, 8
            ret
            .cfi_endproc
    .LFE0:
            .size   main, .-main
            .ident  "GCC: (Debian 13.2.0-10) 13.2.0"
            .section        .note.GNU-stack,"",@progbits
    Last edited by rstanley; 01-25-2024 at 06:44 AM.

  2. #2
    Registered User
    Join Date
    Jan 2024
    Posts
    25
    Quote Originally Posted by rstanley View Post
    I have never done any stack tracking, using the current program stack, though there may be a way to do it.

    As for converting code to assembly, you can do it with "gcc -S hello.c". You can probably do this in any compiler. You would need to read the documentation for the compiler and/or IDE.

    Source hello.c:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      printf("Hello World!\n");
    
      return 0;
    }
    Assembly hello.s:
    Code:
           .file   "hello.c"
            .text
            .section        .rodata
    .LC0:
            .string "Hello World!"
            .text
            .globl  main
            .type   main, @function
    main:
    .LFB0:
            .cfi_startproc
            pushq   %rbp
            .cfi_def_cfa_offset 16
            .cfi_offset 6, -16
            movq    %rsp, %rbp
            .cfi_def_cfa_register 6
            leaq    .LC0(%rip), %rax
            movq    %rax, %rdi
            call    puts@PLT
            movl    $0, %eax
            popq    %rbp
            .cfi_def_cfa 7, 8
            ret
            .cfi_endproc
    .LFE0:
            .size   main, .-main
            .ident  "GCC: (Debian 13.2.0-10) 13.2.0"
            .section        .note.GNU-stack,"",@progbits

    I understand , thanks.
    Assume that you given this challenge how you would tackle it? .. can we on C programming do like okay we already know we have only 100 bytes (stackmemory 100) so what about each function to have its addresss? got my point? like lets say f3 to have its pointer saved aside on the function itself and ask myself okay did we arrive pointer < pointer of array + 100 ? if yes then no need to call next recursion again if not then call recursive function again.

    now how I can add a code for saving the pointer of the called function? like lets say I called f1 on first recursive call so I want to save function pointer itself on *p = &f1() on the caller function itself .... and so on ..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursion and stack - trying to understand
    By bos1234 in forum C Programming
    Replies: 2
    Last Post: 09-01-2013, 01:58 AM
  2. Replies: 9
    Last Post: 04-29-2011, 09:26 AM
  3. Stack overflow in recursion
    By Nom1fan in forum C Programming
    Replies: 4
    Last Post: 11-24-2010, 12:51 PM
  4. Callback recursion: stack overflow
    By nicoqwertyu in forum C++ Programming
    Replies: 8
    Last Post: 03-16-2010, 11:09 AM
  5. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM

Tags for this Thread