From: Mark Sparshatt Date: 2004-04-03T21:49:15+09:00 Subject: Re: Learning Ruby, was a C geek... Nicholas Paul Johnson wrote: >Hello all, > >As I'm learning Ruby, I've decided to write a few programs in Ruby to get >a feel for the language. > >I've been a C programmer for a long time. As such, I worry that I'm >thinking too much like a C programmer, and not like a Ruby programmer... > >My first attempt has been an emulator for a cs-toy processor called IBCM >(itty-bitty computing machine). Its spec (only 5 pages) is available at: >http://www.cs.virginia.edu/~cs216/notes/ibcm-poo.pdf > >I've written it, and it works correctly when compared to my program >written in C, but at its core is a large case-when-end statement. >This is not `sexy.' I was wondering if there was a better way to do >this in Ruby, or if I'm just missing the point. > >My code is at: http://manjac.ath.cx/nick/ruby-ibcm.rb > >Thanks all, > > I haven't got any advice over what everyone else has suggested for the layout of your program but I have spotted a couple of bugs. Firstly the read word instruction doesn't work. It either doesn't take any input or if you run the program with the trace option it gives an error no such file or directory - -t changing the line accum = gets.to_i to accum = $stdin.gets.to_i fixes this The other problem is the JUMPL instruction jumps even if the acummalator isn't less than 0. The problem is in this line progcount = off if accum & 0x8000 since 0 is a true value in Ruby the part accum & 0x8000 is always true. You need to change it to progcount = off if (accum & ox8000 != 0) -- Mark Sparshatt