0

im trying to receive two strings and their length and after that another number. like this: 5 hello 2 hi 8. i succeed at receiving both strings and their length but i can`t receive the last number. (The reason for allocating a lot of space is because i assume the strings length is 255 at max) i get the error "Program received signal SIGSEGV, Segmentation fault."

.data

.section    .rodata         #read only data section
_scanf_check:   .string "%d" #in order to check output
_scanf_length1: .string "%d"
_scanf_length2: .string "%d"    
_scanf_str1:    .string "%s"    
_scanf_str2:    .string "%s"    
_scanf_option:  .string "%d"


.text
.global main
.type   main, @function
main:
    movq %rsp, %rbp #for correct debugging
    pushq %rbp      #save the old frame pointer
    movq    %rsp, %rbp  #create the new frame pointer
    #########################################################
    #receiving the length of first pstring.
    leaq -524(%rbp),%rsi    #allocate 524 bytes on the stack.
    movq $_scanf_length1,%rdi
    xor %rax,%rax
    call scanf
    #########################################################
    #receiving the string of first pstring.
    leaq -520(%rbp),%rsi
    movq $_scanf_str1,%rdi
    xor %rax,%rax
    call scanf
    #########################################################
    #receiving the length of second pstring.
    leaq -264(%rbp),%rsi
    movq $_scanf_length2,%rdi
    xor %rax,%rax
    call scanf
    #########################################################
    #receiving the string of first pstring.
    leaq -260(%rbp),%rsi
    movq $_scanf_str2,%rdi
    xor %rax,%rax
    call scanf
    #########################################################
    #receiving the option from the menu.
    leaq -4(%rbp),%rsi
    movq $_scanf_option,%rdi
    xor %rax,%rax
    call scanf
    #########################################################
    #move back the %rdi-first arg to point on the length of first pstring
    #and %rsi-second arg to point on the length of second pstring also
    #move back the %rdx-third arg to point on the number of option.
    leaq -524(%rbp),%rdi
    leaq -264(%rbp),%rsi
    leaq -4(%rbp),%rdx
    .char_pstrlength:
   movq %rdx,%rsi #reload the address of the last number to %rsi
    movq (%rsi),%rsi #insert the value to rsi.
    movq $_scanf_check,%rdi
    xor %rax,%rax
    call printf
    movq    %rbp, %rsp  #restore the old stack pointer - release all used memory.
    popq    %rbp        #restore old frame pointer (the caller function frame)
    ret
    ```

Jester
  • 56,577
  • 4
  • 81
  • 125
ilan
  • 1
  • 2
  • 1
    For starters, you forgot to allocate space on the stack. The red zone is only 128 bytes. Also, the "for correct debugging" is wrong. You are not supposed to change `rbp` before saving it for obvious reasons. – Jester Dec 23 '22 at 18:11
  • i agree with you about the line "for correct debugging" but when i debug without this line the SASM add it automatically. what do you mean by the "red zone" – ilan Dec 23 '22 at 18:17
  • Red zone is 128 bytes you can use without allocating. You use way more than that so no wonder you get a fault. Allocate the space needed by adjusting the stack pointer. – Jester Dec 23 '22 at 18:24
  • if so, then how did i manage to allocate enough space for the two strings and their length,but only for the last 4 bytes or so of the last number, an error occurred? – ilan Dec 23 '22 at 18:31
  • 1
    The OS does not detect the fault immediately, only if you cross a page boundary (and even then only if you go into unmapped area). Just because something happens to work sometimes does not make it correct. – Jester Dec 23 '22 at 18:46
  • If you only reserve space for up to 255 bytes including the terminating `0`, use `%254s` as your format string. And like Jester said, `sub $528, %rsp` first or something, otherwise your buffer overlaps with scanf's stack frame! – Peter Cordes Dec 23 '22 at 22:29
  • 1
    I think [this person](https://stackoverflow.com/questions/74894123/compiled-assembly-code-produce-non-deterministic-results) must be working through the same exercise as you. The two of you should talk, maybe you can help each other. – Nate Eldredge Dec 23 '22 at 23:03

0 Answers0