Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for [SP] and friends

  • Loading branch information...
commit 936f67c032639c629505e61a191c59cd1b666117 1 parent 60e375e
@judofyr authored
Showing with 42 additions and 1 deletion.
  1. +28 −0 examples/strlen.s
  2. +14 −1 lib/rcpu/asm.rb
View
28 examples/strlen.s
@@ -0,0 +1,28 @@
+; by Tobba
+SET A, 0x0000
+JSR strlen
+SUB PC, 1
+
+:strlen
+ SET B, SP
+ SET SP, A
+ :loop
+ IFE [SP++], 0
+ SET PC, end
+
+ IFE [SP++], 0
+ SET PC, end
+
+ IFE [SP++], 0
+ SET PC, end
+
+ IFE [SP++], 0
+ SET PC, end
+
+ SET PC, loop
+ :end
+ SET C, PUSH ; Decrease SP one
+ SUB SP, A
+ SET A, SP
+ SET SP, B
+ SET PC, POP
View
15 lib/rcpu/asm.rb
@@ -3,12 +3,23 @@
module RCPU
class ASMParser
def self.u(arr)
- Regexp.new('('+arr.map(&:to_s)*'|'+')\b', 'i')
+ Regexp.new('('+arr.map{|x|Regexp.escape(x.to_s)}*'|'+')\b', 'i')
+ end
+
+ def self.b(arr)
+ Regexp.new('('+arr.map{|x|Regexp.escape(x.to_s)}*'|'+')', 'i')
end
BASIC = u(BasicInstruction::ALL)
NONBASIC = u(NonBasicInstruction::ALL)
REGS = u(Register::ALL)
+ ALIAS = {
+ "[SP++]" => :POP,
+ "[SP]" => :PEEK,
+ "[--SP]" => :PUSH
+ }
+ ISH = b(ALIAS.keys)
+
SPACE = /[ \t]*/
NUMBER = /(0x[0-9a-fA-F]{1,4})|\d+/
@@ -102,6 +113,8 @@ def parse_value(indirect = false)
@str.skip(/ */)
res = if reg = @str.scan(REGS)
Register.new(reg.upcase.to_sym)
+ elsif ish = @str.scan(ISH)
+ Register.new(ALIAS[ish.upcase])
elsif num = @str.scan(NUMBER)
Literal.new(Integer(num))
elsif @str.scan(/\[/)
Please sign in to comment.
Something went wrong with that request. Please try again.