linux nasm x64 fscanf
linux nasm x64 fscanf
我是 NASM 和 Assembly 的新手。我试图从 nasm 调用 fscanf,但它失败了。请告诉我,我哪里弄错了? (我正在使用 ubuntu 14.04 和英特尔处理器)
extern fscanf
extern fopen
SECTION .bss
n: resq 1
fi: resq 1
SECTION .data
input1: db "%d", 0
fi_name: db "input.txt", 0
mode_read: db "r", 0
SECTION .text
global main
main:
push rbp
mov rdi, fi_name
mov rsi, mode_read
call fopen
mov qword [fi], rax
;fscanf(fi, input1, var)
mov rdi, fi
mov rsi, input1
mov rdx, n
call fscanf ;<---program dies here
pop rbp
ret
在我看来你几乎是对的。我注意到的唯一问题是,在调用 fscanf
之前,您需要将 fi
括起来。事实上,我添加了以下几行:
extern printf ; at top
fmt: db "Result: %d", 10, 0 ; in .data
; following the fscanf call
mov rdi, fmt
mov rsi, [n] ; the integer parsed
xor eax, eax ; required by printf
call printf
这成功扫描并打印了来自 input.txt
的单个整数。
将您的代码更改为以下内容:
;fscanf(fi, input1, var)
mov rdi, [fi] ; <- Needs square brackets to derefrence.
mov rsi, input1
mov rdx, n
call fscanf
我是 NASM 和 Assembly 的新手。我试图从 nasm 调用 fscanf,但它失败了。请告诉我,我哪里弄错了? (我正在使用 ubuntu 14.04 和英特尔处理器)
extern fscanf
extern fopen
SECTION .bss
n: resq 1
fi: resq 1
SECTION .data
input1: db "%d", 0
fi_name: db "input.txt", 0
mode_read: db "r", 0
SECTION .text
global main
main:
push rbp
mov rdi, fi_name
mov rsi, mode_read
call fopen
mov qword [fi], rax
;fscanf(fi, input1, var)
mov rdi, fi
mov rsi, input1
mov rdx, n
call fscanf ;<---program dies here
pop rbp
ret
在我看来你几乎是对的。我注意到的唯一问题是,在调用 fscanf
之前,您需要将 fi
括起来。事实上,我添加了以下几行:
extern printf ; at top
fmt: db "Result: %d", 10, 0 ; in .data
; following the fscanf call
mov rdi, fmt
mov rsi, [n] ; the integer parsed
xor eax, eax ; required by printf
call printf
这成功扫描并打印了来自 input.txt
的单个整数。
将您的代码更改为以下内容:
;fscanf(fi, input1, var)
mov rdi, [fi] ; <- Needs square brackets to derefrence.
mov rsi, input1
mov rdx, n
call fscanf