ports.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * Read a byte from the specified port
  3. */
  4. unsigned char port_byte_in (unsigned short port) {
  5. unsigned char result;
  6. /* Inline assembler syntax
  7. * !! Notice how the source and destination registers are switched from NASM !!
  8. *
  9. * '"=a" (result)'; set '=' the C variable '(result)' to the value of register e'a'x
  10. * '"d" (port)': map the C variable '(port)' into e'd'x register
  11. *
  12. * Inputs and outputs are separated by colons
  13. */
  14. __asm__("in %%dx, %%al" : "=a" (result) : "d" (port));
  15. return result;
  16. }
  17. void port_byte_out (unsigned short port, unsigned char data) {
  18. /* Notice how here both registers are mapped to C variables and
  19. * nothing is returned, thus, no equals '=' in the asm syntax
  20. * However we see a comma since there are two variables in the input area
  21. * and none in the 'return' area
  22. */
  23. __asm__("out %%al, %%dx" : : "a" (data), "d" (port));
  24. }
  25. unsigned short port_word_in (unsigned short port) {
  26. unsigned short result;
  27. __asm__("in %%dx, %%ax" : "=a" (result) : "d" (port));
  28. return result;
  29. }
  30. void port_word_out (unsigned short port, unsigned short data) {
  31. __asm__("out %%ax, %%dx" : : "a" (data), "d" (port));
  32. }