Йордан обнови решението на 12.01.2014 14:56 (преди почти 11 години)
+#без jump-овете и label
+module Asm
+
+ class Inline_assembler
+ attr :ax , :bx , :cx , :dx
+ attr_reader :registers
+ def initialize()
+
+ @registers={:ax=>{:val=>0},:bx=>{:val=>0},:cx =>{:val=>0},:dx=>{:val=>0}}
+ @ax = @registers[:ax]
+ @bx = @registers[:bx]
+ @cx = @registers[:cx]
+ @dx = @registers[:dx]
+ @last_cmp = 0
+ end
+
+ method_to_define = {
+ je: :'==',
+ jne: :'!=',
+ jl: :'<',
+ jle: :'<=',
+ jg: :'>',
+ jge: :'>=',
+
+ }
+
+ method_to_define.each do |method_name, operation|
+ define_method method_name do |argument|
+ jmp(argument) if @last_cmp.send(operation,0)
+ end
+ end
+
+ def mov(x,y)
+ ( y.is_a? Numeric )? x[:val] = y : x[:val] = y[:val]
+ self
+ end
+ def inc(x,y=1)
+ ( y.is_a? Numeric )? x[:val] += y : x[:val] += y[:val]
+ self
+ end
+ def dec(x,y=1)
+ ( y.is_a? Numeric )? x[:val] -= y : x[:val] -= y[:val]
+ self
+ end
+ def cmp (x,y)
+ @last_cmp = ( y.is_a? Numeric )? x[:val] <=> y : x[:val] <=> y[:val]
+ self
+ end
+ def label (label_name)
+ end
+ def jmp(where)
+ puts "GoGoGo"
+ end
+
+
+
+ end
+
+ def self.asm(&block)
+ asm =Inline_assembler.new
+ asm.instance_eval(&block)
+ return [asm.ax[:val] , asm.bx[:val] , asm.cx[:val] , asm.dx[:val]]
+ end
+end