使用 nasm 在汇编中调用 fscanf
我正在尝试从格式为整数字符空白整数的文件中读取 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
I am trying to read 3 values from a file with a format as integer character whitespace integer. For example:
5, 3
In that exact format i.e: no whitespace after the 5, a whitespace after the comma and no whitespace after the 3.
I successfully called fopen to open the file and I used fgetc to access the same file and print its contents. Now I am trying to use fscanf()
I read that to call a C function from assembly you have to push the parameters in reverse order onto the stack, below is my code to do this
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
At this point I am assuming the above is equivalent to:
fscanf(fp, "%d %c %d", &yValue, &comma, &xValue);
If it is equivalent to the above, how do I access the values read? I know I'm accessing the file correctly since I was able to print out the individual characters by calling fgetc, but for clarity below is my code to open the file
mov eax, fileMode
push eax
mov eax, fileName
push eax
call _fopen
mov ebx, eax ;store file pointer
Any help/advice is much appreciated. Thanks.
Edited to add…
The answer provided the solution. Posting the code below for anyone else who has this problem.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的 scanf() 格式字符串是错误的。应为“%d%c %d”。无论如何,你为什么关心逗号?为什么不直接使用“%d, %d”并放弃逗号变量。
另外,您尝试使用 [format] 的第一个字节中的值加载 eax,您需要将指针推入 format。
最后,您不希望内存地址周围有括号,除非您的汇编器很奇怪,您推入了错误的地址。
现在您应该在 xvalue 和 yvalue 中获得您想要的值
I think your scanf() format string is wrong. Should be "%d%c %d". Why do you care about the comma anyway? Why not just use "%d, %d" and ditch the comma variable.
Also, you are trying to load eax with a value from the first bytes of [format], you need to push the pointer to format.
Finally, you don't want the brackets around the memory addresses, unless your assembler is weird you are pushing the wrong addresses.
Now you should have the values you want in xvalue and yvalue