home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / machine.e < prev    next >
Text File  |  1994-01-31  |  2KB  |  72 lines

  1.         ----------------------------------------
  2.         -- Machine Level Programming for 386+ --
  3.         ----------------------------------------
  4.  
  5. -- Warning: These routines are not safe. They require a knowledge of 
  6. -- machine-level programming. You can easily crash your system.
  7. -- Only superficial checking of argument values is provided.
  8.  
  9. -- These routines, along with peek(), poke() and call(), let you access all 
  10. -- of the features of your computer.  You can read and write to any memory 
  11. -- location, and you can create and execute machine code subroutines.
  12.  
  13. -- Writing characters to screen memory with poke() is much faster than  
  14. -- using puts().
  15. -- address of start of screen memory 
  16. --     mono monitor: #B0000
  17. --    color monitor: #B8000
  18.  
  19. -- see demo\callmach.ex for an example of calling a machine language routine
  20.  
  21. constant M_ALLOC = 16,
  22.      M_FREE = 17
  23.  
  24. -- biggest address on a 32-bit machine
  25. constant MAX_ADDR = power(2, 32)-1
  26.  
  27. type machine_addr(atom a)
  28. -- a legal machine address 
  29.     return a > 0 and a <= MAX_ADDR and floor(a) = a
  30. end type
  31.  
  32. global function allocate(integer n)
  33. -- allocate n bytes of memory and return the address
  34.     return machine_func(M_ALLOC, n)
  35. end function
  36.  
  37. global procedure free(machine_addr a)
  38. -- free the memory at address a
  39.     machine_proc(M_FREE, a)
  40. end procedure
  41.  
  42. global function int_to_bytes(atom x)
  43. -- returns value of x as a sequence of 4 bytes: 
  44. --    {bits 0-7,  (least significant)
  45. --     bits 8-15,
  46. --     bits 16-23,
  47. --     bits 24-31} (most significant)
  48. -- This is the order of bytes in memory on 386+ machines.
  49. -- This routine is useful when you need to poke numbers 
  50. -- greater than one byte into memory.
  51.     sequence result
  52.  
  53.     result = {0,0,0,0}
  54.     result[1] = remainder(x, #100)
  55.     x = floor(x / #100)
  56.     result[2] = remainder(x, #100)
  57.     x = floor(x / #100)
  58.     result[3] = remainder(x, #100)
  59.     x = floor(x / #100)
  60.     result[4] = remainder(x, #100)
  61.     return result
  62. end function
  63.  
  64. global function bytes_to_int(sequence s)
  65. -- converts 4-byte sequence back into a single integer value
  66.     return s[1] + 
  67.        s[2] * #100 + 
  68.        s[3] * #10000 + 
  69.        s[4] * #1000000
  70. end function
  71.  
  72.