2

I have a task to write a prime numbers in text file, but I know how to convert a integer to string, and string to integer, but I think that is just a huge work, then I remember that I have a function fprintf that can help me, but I do not know why I getting a segmentation fault, maybe my arguments are not so good, can you help me? I appreciate that.

section .data
content db '%d',10,0
mod db 'w',0
fail db 'Marko.txt',0

section .text
extern printf,fopen,fclose,fprintf
global main
main:
push rbp
mov rbp,rsp
push rdi
push rsi
push rbx
mov rdx,0

mov rdi,fail
mov rsi,mod
call fopen

push rax
mov rsi,rax
mov rdi,content
mov rdx,5
call fprintf

pop rdi
close fclose

pop rbx
pop rsi
pop rdi
mov rsp,rbp
pop rbp
ret
2
  • 1
    Please don't repost questions. Edit it if you have additional information. Anyway, as I said in your original one, you have not zeroed al which is required by the sysv abi that you seem to be following (but you have not specified OS) and you misaligned the stack. Commented Dec 27, 2018 at 17:27
  • how to fix that ok, I need mov rax,0, then what? Commented Dec 27, 2018 at 17:33

1 Answer 1

2

The first parameter to fprintf is the FILE.

Sign up to request clarification or add additional context in comments.

4 Comments

I know that, so that I have rsi,rax
@MarkoŠkorić except the first argument is in rdi.
I know that when i have fputs i have argument file, and string, then I use mov rsi,rax and mov rdi,string and call fputs and that is work, but only different here is that I have more arguments that can be put in string, so I need to say rdi,... what? @Jester
For fputs, the first parameter is the string and the second parameter is the FILE. For fprintf, the first parameter is the FILE, the second parameter is the format, and the remaining parameters are controlled by the format string. If you don’t know the order of the parameters to any library function, you should look them up before asking here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.