1 # Helper to dynamically allocate memory on the heap. 2 # 3 # We'd like to be able to write tests for functions that allocate memory, 4 # making assertions on the precise addresses used. To achieve this we'll pass 5 # in an *allocation descriptor* to allocate from. 6 # 7 # Allocation descriptors are also useful outside of tests. Assembly and machine 8 # code are of necessity unsafe languages, and one of the most insidious kinds 9 # of bugs unsafe languages expose us to are dangling pointers to memory that 10 # has been freed and potentially even reused for something totally different. 11 # To reduce the odds of such "use after free" errors, SubX programs tend to not 12 # reclaim and reuse dynamically allocated memory. (Running out of memory is far 13 # easier to debug.) Long-running programs that want to reuse memory are mostly 14 # on their own to be careful. However, they do get one bit of help: they can 15 # carve out chunks of memory and then allocate from them manually using this 16 # very same 'allocate' helper. They just need a new allocation descriptor for 17 # their book-keeping. 18 19 == data 20 21 # The 'global' allocation descriptor. Pass this into 'allocate' to claim a 22 # hitherto unused bit of memory. 23 Heap: 24 Start-of-heap/imm32 # curr 25 00 00 00 0b # limit = 0x0b000000; keep sync'd with DATA_SEGMENT + SEGMENT_ALIGNMENT 26 27 == code 28<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Mu - 086scenario_console_test.mu</title> <meta name="Generator" content="Vim/7.4"> <meta name="plugin-version" content="vim7.4_v1"> <meta name="syntax" content="none"> <meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy="> <meta name="colorscheme" content="minimal"> <style type="text/css"> <!-- pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; } body { font-family: monospace; color: #eeeeee; background-color: #080808; } * { font-size: 1.05em; } .muScenario { color: #00af00; } .Comment { color: #9090ff; } .Constant { color: #00a0a0; } .Special { color: #ff6060; } --> </style> <script type='text/javascript'> <!-- --> </script> </head> <body> <pre id='vimCodeElement'> <span class="Comment"># To check our support for consoles in scenarios, rewrite tests from</span> <span class="Comment"># scenario_console.mu</span> <span class="Comment"># Tests for console interface.</span> <span class="muScenario">scenario</span> read-key-in-mu [ assume-console [ type <span class="Constant">[abc]</span> ] run [ <span class="Constant">1</span>:character, console:address:console, <span class="Constant">2</span>:boolean<span class="Special"> <- </span>read-key console:address:console <span class="Constant">3</span>:character, console:address:console, <span class="Constant">4</span>:boolean<span class="Special"> <- </span>read-key console:address:console <span class="Constant">5</span>:character, console:address:console, <span class="Constant">6</span>:boolean<span class="Special"> <- </span>read-key console:address:console <span class="Constant">7</span>:character, console:address:console, <span class="Constant">8</span>:boolean<span class="Special"> <- </span>read-key console:address:console ] memory-should-contain [ <span class="Constant">1</span><span class="Special"> <- </span><span class="Constant">97</span> <span class="Comment"># 'a'</span> <span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">1</span> <span class="Constant&qu