0

I just started learning assembly I really need help I keep running into a segmentation fault.

I'm trying to make it so the name that is entered gets passed by reference to the subprogram (print_greeting) and then update it with the entered name so it prints the greeting. However, I keep getting a segmentation fault.

segment .data
    mesg: db "Please enter your first name: ", 0
    greeting: db "Hello, %s, welcome to Assembly World!", 10, 0
    intInput: db "%s",0

segment .bss
    name: resb 16

segment .text
    global asm_main
    extern printf, scanf

; Subprogram to print greeting
; Input: name (string)
print_greeting:
    mov rdi, greeting
    mov rsi, name
    call printf
    ret

asm_main:
    enter 0,0

    mov rdi, mesg
    call printf

    mov rdi, intInput
    mov rsi, name
    call scanf

    ; Call subprogram to print greeting
    mov rsi, name
    call print_greeting

    mov rax, 0
    leave
    ret

I tried doing it all in the main and it works but I need a subprogram where the string entered is being passed to the subprogram.

ecm
  • 2,583
  • 4
  • 21
  • 29
  • At a guess you're not aligning the stack for `printf` and you may need to pass `al` = 0 to `printf`. Try adding `enter 0,0` and `leave` to the subfunction similar to how they're used in `asm_main`, and `mov al, 0` before the `call printf` lines. – ecm Mar 21 '23 at 08:27

0 Answers0