开发者

calling fscanf in assembly using nasm

开发者 https://www.devze.com 2023-02-28 05:05 出处:网络
I am trying to read 3 values from a file with a format as integer character whitespace integer. For example:

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


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.

lea eax, xvalue
push eax
lea eax, yValue
push eax
lea eax, format
push eax
call _fscanf

Now you should have the values you want in xvalue and yvalue

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号