我正在尝试从格式为整数字符空白整数的文件中读取3个值。例如:
5,3
以精确格式表示,即:5之后没有空格,逗号后面有空格,3之后没有空格。
我成功调用fopen打开文件,我使用fgetc访问同一个文件并打印其内容。现在我正在尝试使用fscanf()
我读到要从汇编中调用C函数,你必须以相反的顺序将参数推入堆栈,下面是我的代码来执行此操作
lea eax, [xValue]
push eax
lea eax, [comma]
push eax
lea eax, [yValue]
push eax
mov eax, [format] ;defined as [format db "%d %c %d", 0] in the data section
push eax
mov eax, ebx ; move handle to file into eax
push eax
call _fscanf
此时我假设上述内容相当于:
fscanf(fp, "%d %c %d", &yValue, &comma, &xValue);
如果它与上述相同,我如何访问读取的值?我知道我正在正确访问该文件,因为我能够通过调用fgetc打印出单个字符,但为了清楚起见,下面是我打开文件的代码
mov eax, fileMode
push eax
mov eax, fileName
push eax
call _fopen
mov ebx, eax ;store file pointer
非常感谢任何帮助/建议。感谢。
编辑添加...
答案提供了解决方案。为其他有此问题的人发布以下代码。
section .data
fname db "data.txt",0
mode db "r",0 ;;set file mode for reading
format db "%d%c %d", 0
;;--- end of the data section -----------------------------------------------;;
section .bss
c resd 1
y resd 1
x resd 1
fp resb 1
section .text
extern _fopen
global _main
_main:
push ebp
mov ebp,esp
mov eax, mode
push eax
mov eax, fname
push eax
call _fopen
mov [fp] eax ;store file pointer
lea eax, [y]
push eax
lea eax, [c]
push eax
lea eax, [x]
push eax
lea eax, [format]
push eax
mov eax, [fp]
push eax
call _fscanf
;at this point x, y and c has the data
mov eax,0
mov esp,ebp
pop ebp
ret
答案 0 :(得分:3)
我认为你的scanf()格式字符串是错误的。应为“%d%c%d”。你为什么还要关心逗号呢?为什么不直接使用“%d,%d”并抛弃逗号变量。
此外,您正在尝试使用[format]的第一个字节中的值加载eax,您需要将指针推送到格式。
最后,你不希望内存地址周围的括号,除非你的汇编程序很奇怪你推错了地址。
lea eax, xvalue
push eax
lea eax, yValue
push eax
lea eax, format
push eax
call _fscanf
现在你应该在xvalue和yvalue
中拥有你想要的值