Assembly x86-64 | Segmentation fault

38 Views Asked by At

I have a task like this: Implement the function with the signature in x86-64 assembly language:

extern int * A;
extern void func(size_t N);

The function in the loop N times:

  1. Enters a signed 32-bit integer from the keyboard;
  2. Multiplies it by the current array element A;

At the end prints the result to the screen. I wrote this program:

.intel_syntax noprefix
.text
.global func
.global A

func:

    sub rsp, 16
        mov r8, 0    // r8 = sum = 0
    mov r9, rdi  // r9 -> N
    
loop:
    cmp r9, 0
    je end
    sub r9, 1
    
    mov rdi, scan_format
    mov rsi, var1
    call scanf
    
    mov rsi, var1
    mov rsi, [rsi]
    
    mov rcx, A
    
    mul rcx
    add r8, rcx
    jmp loop
    
end:
    mov    rdi, print_format
    mov    rsi, r8
    call printf

    add sp, 16
    ret
    
.data
var1:
    .word 0

scan_format:
    .string "%d"

print_format:
    .string "%d\n"

but I get a segmentation fault and don't understand why?

0

There are 0 best solutions below