Assembly 使用nasm在程序集中调用fscanf
我试图从一个文件中读取3个值,格式为整型字符空格整数。例如: 5,3 在精确的格式中,即:5后面没有空格,逗号后面有空格,3后面没有空格 我成功地调用fopen打开了该文件,并使用fgetc访问同一文件并打印其内容。现在我尝试使用fscanf() 我读到,要从汇编中调用C函数,必须将参数按相反顺序推送到堆栈上,下面是我的代码Assembly 使用nasm在程序集中调用fscanf,assembly,file-io,nasm,scanf,Assembly,File Io,Nasm,Scanf,我试图从一个文件中读取3个值,格式为整型字符空格整数。例如: 5,3 在精确的格式中,即:5后面没有空格,逗号后面有空格,3后面没有空格 我成功地调用fopen打开了该文件,并使用fgetc访问同一文件并打印其内容。现在我尝试使用fscanf() 我读到,要从汇编中调用C函数,必须将参数按相反顺序推送到堆栈上,下面是我的代码 lea eax, [xValue] push eax lea eax, [comma] push eax lea eax, [yVa
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
我认为您的scanf()格式字符串是错误的。应为“%d%c%d”。你为什么要关心逗号呢?为什么不直接使用“%d,%d”并放弃逗号变量呢
此外,如果您试图使用[format]的第一个字节中的值加载eax,则需要将指针推送到format
最后,您不希望内存地址周围有括号,除非您的汇编器很奇怪,因为您推错了地址
lea eax, xvalue
push eax
lea eax, yValue
push eax
lea eax, format
push eax
call _fscanf
现在您应该在xvalue和yvalue中获得所需的值了当我尝试lea eax时,xvalue nasm给了我以下错误“操作码和操作数的组合无效”,很好,按照汇编程序的工作方式,使用括号