Dropping down in Assembly from Ruby by way of C?
-
20-12-2019 - |
Question
Since Ruby lets you drop down into C for any bits where performance is critical and plain Ruby is not up to the task, and since C lets you drop down into assembly for the same sort of circumstance, I've always wondered if it would be possible to embed assembly language inside your Ruby code by first dropping down to C and then from within the C code dropping down to assembly.
I've always hoped that was possible, but never until now found anything to confirm or deny it. Because of research on Qt (and therefore C++) for a side project, I happened upon a statement on ruby-lang.com's comparison of Ruby with C & C++ that in Ruby
- You cannot drop down to assembly
Does this mean simply that you cannot go directly to assembly, or that you cannot do it by way of C at all? What about if you're writing an extension to Ruby in C: can I assume that since that is just C (not embedded in Ruby) you can still use assembly there and Ruby can still use the C extension with assembly in it?
Solution
Given the context, it seems pretty clear that it means in Ruby, you can't just drop something crazy like:
def func1(arr)
arr.map { |v| func2(v) }
end
def func2(val)
asm do
mov ax, 1234h
mov bx, ax
end
end
Ruby can't stop you from using something like that in a linked C object.
But, having seen that as an example, it'd be a pretty cool library if someone made it. :)