Jump to content

Machine code: Difference between revisions

Added Quackery.
(Add Draco)
(Added Quackery.)
Line 1,117:
print(res)
</lang>
 
=={{header|Quackery}}==
 
This task is a bit of a stretch, but the Quackery engine is a virtual machine, so technically Quackery is an assembly language.
 
Specifically the engine is a stack based processor that does not have direct access to RAM; instead a co-processor mediates requests for dynamically allocated memory. More details in the About Quackery section of Quackery's Category page. (Click on the header for this task.)
 
So, with the limitations that using Hex numbers to indicate opcodes is not possible, and understanding that when a chunk of memory is requested it is addressed by an offset and a reference to the start of the allocated memory (the numerical address of which is not available to the programmer), this is a walkthrough of the process in the Quackery shell.
 
As it is a stack machine arguments are passed via the data stack, reducing the specified task to a single operation; the MOV is not required. Therefore I have substituted a different specification that requires two operators - it is "add two numbers and negate the result".
 
Numbers in Quackery are signed BigIntegers; there are no unsigned numbers, so that part of the task is omitted.
 
Please don't flag this as incorrect - it's the best you'll get.
 
<pre>/O> ( check that * and negate are operators (i.e. op-codes )
... ' + operator? ' negate operator? and if [ say "true"]
...
true
Stack empty.
 
/O> ( create a memory block large enough to hold them, )
... ( filled with zeros )
... 0 2 of
...
 
Stack: [ 0 0 ]
 
/O> ( poke the + operator into place )
... ' + swap 0 poke
...
 
Stack: [ + 0 ]
 
/O> ( poke the negate operator into place )
... ' negate swap 1 poke
...
 
Stack: [ + negate ]
 
/O> ( Using the phrase )
... ( )
... ( ' + ' negate join )
... ( )
... ( would be more idiomatic, but )
... ( the task specifies poking. )
 
Stack: [ + negate ]
 
/O> ( now put two numbers underneath it on the stack )
... 23 42 rot
...
 
Stack: 23 42 [ + negate ]
 
/O> ( and run the machine code )
... do
...
 
Stack: -65
 
/O> ( ta-da! )
</pre>
 
 
=={{header|Racket}}==
1,483

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.