0

I am making a pong game entirely in x86_64 NASM assembly. I have managed to capture keyboard input with Termios, however when reading from stdin, it waits until a character is pressed. This is a problem if i want the ball to move without caring about input. How do i make it only wait a small amount of time before proceeding onto the rest of the code? I have figured that the only way to do this is to set the VTIME part of the Termios struct array to a certain value and set VMIN to 0. Is there a better way? If not, how would you do this? I have taken some code that i found on here about turning canonical mode off, and it works, but i have no idea how to set other values.

ByteBox
  • 21
  • 10
  • I think you would normally do this with `select/poll`, not by using the specific timeout features of termios. The purpose of `select` and `poll` is to wait until input is ready on a file descriptor or until a given timeout expires. Another option is to set the file descriptor into non-blocking mode, so that if no data is available, `read` will return immediately. – Nate Eldredge Oct 02 '21 at 17:42
  • Oh, how do you set the file descriptor into non-blocking mode in assembly? That seems like an ideal solution. – ByteBox Oct 02 '21 at 17:47
  • With `fcntl(fd, F_SETFL, O_NONBLOCK);`. Translating the C system call into assembly is an exercise in which surely you've had lots of practice by now. – Nate Eldredge Oct 02 '21 at 17:49
  • If you don't know, then how would you implement `select` or `poll` in assembly? – ByteBox Oct 02 '21 at 17:49
  • 1
    Same deal, read the documentation for the C function and translate into assembly. – Nate Eldredge Oct 02 '21 at 17:50
  • Alright, ill try those solutions out. – ByteBox Oct 02 '21 at 17:51
  • Im kinda new to assembly, and translating from documentation to assembly is a challenge. Is there anything to point me in the right direction? – ByteBox Oct 02 '21 at 17:54
  • Check out https://stackoverflow.com/questions/2535989/what-are-the-calling-conventions-for-unix-linux-system-calls-and-user-space-f, and the items about system calls in https://stackoverflow.com/tags/x86/info. – Nate Eldredge Oct 02 '21 at 17:57
  • I made `read` return if there is no data found. Thank you so much! – ByteBox Oct 02 '21 at 18:18
  • One small question: How should i handle the syscalls that have C structs as arguments? – ByteBox Oct 02 '21 at 18:20
  • 1
    You have to read through the appropriate system headers to see how those structs are defined, work out how they are laid out in memory (with reference to the ABI if needed), and create and populate an area of memory with that layout in your own program, then pass its address to the system call. Yes, it is rather tedious. There is a reason why compilers were invented! – Nate Eldredge Oct 02 '21 at 19:10
  • You normally still want to use `select` or `poll` with a timeout, unless you have some other timing mechanism that makes your process do short sleeps, not just calling non-blocking `read` in a tight loop. (Polling wastes CPU time, design your programs to not do it.) – Peter Cordes Oct 02 '21 at 19:40

0 Answers0