Frage

I'm currently trying to write to a file so far I have the following code to append to the file. Does anybody know why this isn't working? It runs fine but by the end nothing has changed.

filewritemode: .asciz "a"
filelocation: .asciz "/h/test.txt"

_main:  

push $filelocation
push $filewritemode
call _fopen
push $blabla
push %eax 
call _fprintf

push $result
call _printf
push $0
call _exit # exit the program

gcc is used in order to turn the source file into an .exe $blabla is currently the string with some random chars that are ment for testing

War es hilfreich?

Lösung

It doesn't work because you have pushed the parameters of fopen in the wrong order. Parameters must be pushed from last to first. Aside from that you are repeatedly pushing parameters, but you don't remove them again. In this case that works because you take a dive to exit, but if instead you would have returned with the ret instruction, you would have found that this would result in a crash as you would be jumping to one of the pushed parameters.

Andere Tipps

You are passing the arguments to fopen reversed and you are not checking for errors. In situations like this, ltrace may be your friend.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top